scala 3.3.0
type ValuesOf[T<:{type Values}] = T#Values
T#Values cant compile
T is not a legal path
since it is not a concrete type
is that bug? how i get the type T#Values now
scala 3.3.0
type ValuesOf[T<:{type Values}] = T#Values
T#Values cant compile
T is not a legal path
since it is not a concrete type
is that bug? how i get the type T#Values now
This isn’t a bug but a type safety improvement in Scala 3 (see the docs)
Either T
needs to have a class/trait/enum/object as its upper bound, or you need to have a term parameter.
e.g.
// option 1
class Foo { type Values }
type ValuesOf[T <: Foo] = T#Values
or
// option 2
def valuesOf[T<:{type Values}](t: T): t.Values = ???
Option 1 still doesn’t work for me.
-- Error: ----------------------------------------------------------------------
1 |type ValuesOf[T <: Foo] = T#Values
| ^
| T is not a legal path
| since it is not a concrete type
oh sorry! it only works directly from a class then
type T = Foo#Values
the main restriction is that the compiler needs a proof that if a value of the type exists then its safe to access the type - this is trivial for class types as they are concrete. I think the explanation for parameters is because if the method gets called, then the types of its arguments were checked for soundness.
Option 1 still doesn’t work for me too ,unless’ - source: 3.0 migration 'is added
I’m not sure if the grammar is properly designed, but I will have a lot of code to modify and adjust
The current compatibility approach is like this
type ValOf[T <: {type Values} ] = T match
case JString => JString#Values
case JInt => JInt#Values
//case [t]:T => [t]#Values
//case {type Values} => T#Values
case _ => NotImplementedError
There is no scalability in writing like this. Do you have any other good ideas to solve this problem
Are you porting code from scala 2? or starting from scratch? It seems like you need a different design but I can’t suggest much unless I know the purpose of type Values
, and the API you want to make