Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is actuall class for structural type scala syntax definition?

Tags:

scala

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.

like image 539
yura Avatar asked Aug 31 '11 06:08

yura


1 Answers

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.

like image 60
Paolo Falabella Avatar answered Nov 15 '22 05:11

Paolo Falabella