Example:
type T = MyClass {def someMethod:String}
Does it mean for compiler creation of trait like "trait AnonTrait extends MyClass {def someMethod:String}
"? Or it is done via other compiler mechanism?
My question was what is actually hidden by this Type syntax.
It doesn't hide the creation of a type. Basically it masks use of reflection to check your structural constraints at compile time and to invoke someMethod at runtime.
For instance, if you have:
class Foo(t: MyClass {def someMethod:String}) {
def CallSomeMethod = t.someMethod
}
this means the constructor for your Foo class accepts a t of type MyClass that also has a someMethod (where the someMethod could be mixed into MyClass via a trait). You could have:
class MyClass {}
trait WithSomeMethod {def someMethod = "hello"}
and you could then create Foo like this:
val mc = new MyClass with WithSomeMethod
val foo = new Foo(mc)
println(foo.CallSomeMethod) // prints "hello"
now, when you create new Foo(mc)
the compiler uses reflection to check that mc is a MyClass that also has a someMethod. The actual call foo.CallSomeMethod
works via reflection as well.
Now (bear with me, I'm getting to your actual question...) doing as you did:
type T = MyClass {def someMethod:String}
is only creating a type alias, not a concrete type. Once you have T defined that way, you can define Foo as:
class Foo(t: T) {
def CallSomeMethod = t.someMethod
}
this is equivalent to the definition of Foo given previously. You've just created an alias T that can be reused in other places where you might have used MyClass {def someMethod:String}
. No actual T type is generated and the compiler still uses reflection whenever you refer to T, to check the structural constraint that it does have a someMethod defined, and still generates code based on reflection to invoke someMethod.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With