Encoding polymorphic function like id[T] using type member, but how to call it?


Cited the following from the thesis titled “A PATH TO DOT formalizing scala with dependent object types” by Marianna Rapoport:

“type parameters for polymorphic functions such as def idT = x can be encoded using type members as follows: def id(y: { type T })(x: y.T) = x”

My question is how to call the function id(y: { type T })(x: y.T)?



You have a few different options:

def id(y: { type T })(x: y.T) = x

trait A { type T }
class B[T0]{ type T = T0 }
type C = Selectable

id(new A {type T = Int})(42)
id(new C {type T = Int})(42)

In Scala 2 id(new {type T = Int})(42) also just works, but in Scala 3 you need to use Selectable.

I Just got it.

“{type T}” in “y:{type T}” should be a structural type, so your cases with A and B works. But Selectable has no type member T, maybe as a marker trait, it was treated by Scala 3 compiler specially.

Thanks for your help!