I have this program:
object B{
def apply[T](c:T)={}
}
object C{
type T
def apply(c:T)={}
}
object A extends App{
val d=B{println(1);2}
val e=C{println(1);2}
}
the line
val e = C{println(1);2}
told me error:Type mismatch,expected C.T,actual:2
so why can't I write
type T
def apply(c:T)
it seems the same as
apply[T](c:T)
and what's the type of T when I write
val d=B{println(1);2}
I can write many lines here!
because T means generic,so it can be Int,String,user defined class Apple,Orange...
and what's type of
println(1);2
is there a type "lines of codes"?
Thanks!
The type of a block is the type of the last expression on the block. So
has type
Int
.Difference between
B
andC
is difference in type inference between type members and type parameters (1, 2).In
C
typeT
remains abstractUse of abstract type in a concrete class?
Concrete classes with abstract type members
There is thesis about type inference in Scala:
Plociniczak, Hubert ; Odersky, Martin. Decrypting Local Type Inference https://infoscience.epfl.ch/record/214757
If you want
e
to compile you can specifyT