Assume I have the following code:
trait Column[T,U <: Units]
case class Ints[U <: Units]() extends Column[Int, U]
case class Floats[U <: Units]() extends Column[Float, U]
case class Strings[U <: Units]() extends Column[String, U]
object Columns:
transparent inline def make0[N<:String with Singleton,T,U <: Units]: (N,Any) =
// val name: N = summon[sourcecode.Name].value
val name = summon[sourcecode.Name].value
inline erasedValue[T] match
case _: Int => (name, Ints[U]())
case _: String => (name, Strings[U]())
case _ => error("Column type not accepted: " + codeOf(erasedValue[T]))
transparent inline def make[N<:String with Singleton,T,U <: Units]: (N,Any) =
inline erasedValue[T] match
case _: Int => (constValue[N], Ints[U]())
case _: String => (constValue[N], Strings[U]())
case _ => error("Column type not accepted: " + codeOf(erasedValue[T]))
transparent inline def make[N<:String with Singleton,T]: (N,Any) =
make[N,T,one]
end Columns
The following code works:
val name: ("name", Strings[one]) = Columns.make["name",String,one]
val age: ("age", Ints[one]) = Columns.make["age", Int]
Note that adding the first type parameter results in a Singleton
. I would like to avoid the user having to explicitly name the column by extracting that name via a macro.
EDIT: correction to refer to make0
.
I have tried to use the sourcecode library to extract the name in match0
. I have 2 issues here. First, code of the function make0
does not compile because the summons returns a String
that is not a singleton. The second is that to have the singleton visible, I need to infer N
. I think the latter can be circumvented with named type arguments.
Does anyone know how to do this without using a full blown macro.
TIA.