Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions library/src/scala/compiletime/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,15 @@ def byName[T](x: => T): T = x
*/
extension [T](x: T)
transparent inline def asMatchable: x.type & Matchable = x.asInstanceOf[x.type & Matchable]

extension (inline self: Any)
/**
* TODO: Write the scaladoc here and how it works
* ???
* @param self ???
* @param T ???
* @return ???
*/
transparent inline def as[T]: T = inline self match
Copy link
Member Author

@hamzaremmal hamzaremmal Nov 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is not set in stone. I like as since it is short, to the point and it is not available unless we import it from the scala.compiletime. A suggestion during the LAMP meeting was to call it inlineAs.

case self: T => self
case _ => error("Cannot statically prove the correctness of the cast")
4 changes: 4 additions & 0 deletions tests/neg/static-cast.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg/static-cast.scala:11:18 ----------------------------------------------------------------------------
11 |val _ = singletons[B, (A.A1.type, A.A2.type, A.A3.type)] // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Cannot statically prove the correctness of the cast
11 changes: 11 additions & 0 deletions tests/neg/static-cast.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.compiletime.*

trait B
enum A { case A1, A2, A3 }

private inline def singletons[T, Elem <: Tuple]: Seq[T] =
inline erasedValue[Elem] match
case _: EmptyTuple => Seq.empty
case _: (h *: t) => valueOf[h](using summonInline).as[T] +: singletons[T, t]

val _ = singletons[B, (A.A1.type, A.A2.type, A.A3.type)] // error
10 changes: 10 additions & 0 deletions tests/pos/static-cast.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import scala.compiletime.*

enum A { case A1, A2, A3 }

private inline def singletons[T, Elem <: Tuple]: Seq[T] =
inline erasedValue[Elem] match
case _: EmptyTuple => Seq.empty
case _: (h *: t) => valueOf[h](using summonInline).as[T] +: singletons[T, t]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After #23773, we should be able to write valueOf[h].as[T]


val x = singletons[A, (A.A1.type, A.A2.type, A.A3.type)]
Loading