"def apply[T](c:T)" 和 "type T;def apply(c:T)" 有什么区别
What's different between "def apply[T](c:T)" and "type T;def apply(c:T)"
我有这个程序:
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}
}
行
val e = C{println(1);2}
告诉我 error:Type 不匹配,预期 C.T,实际:2
所以为什么我不能写
type T
def apply(c:T)
好像和
一样
apply[T](c:T)
我写
时T的类型是什么
val d=B{println(1);2}
我可以在这里写很多行!
因为T表示泛型,所以可以是Int,String,user defined classApple,Orange...
以及
的类型
println(1);2
有类型"lines of codes"吗?
谢谢!
块的类型是块上最后一个表达式的类型。所以
{ println(...); 2 }
类型为 Int
.
B
和 C
之间的区别在于类型成员和类型参数 (1, 2) 之间类型推断的区别。
object B{
def apply[T](c:T)={}
}
object C{
type T
def apply(c:T)={}
}
class C1[T]{
def apply(c:T)={}
}
val d: Unit = B{println(1);2}
// val e: Unit = C{println(1);2} // doesn't compile
val e1: Unit = (new C1){println(1);2}
// scalacOptions ++= Seq("-Xprint:typer", "-Xprint-types")
// val d: Unit = A.this{A.type}.B.apply{[T](c: T)Unit}[Int]{(c: Int)Unit}({
// scala.Predef.println{(x: Any)Unit}(1{Int(1)}){Unit};
// 2{Int(2)}
// }{2}){Unit};
// val e: Unit = A.this{A.type}.C.apply{(c: A.C.T)Unit}({
// println{<null>}(1{Int(1)}){<null>};
// 2{Int(2)}
// }{<null>}){<error>};
// val e1: Unit = new A.C1[Int]{A.C1[Int]}{()A.C1[Int]}(){A.C1[Int]}.apply{(c: Int)Unit}({
// scala.Predef.println{(x: Any)Unit}(1{Int(1)}){Unit};
// 2{Int(2)}
// }{2}){Unit};
在 C
中类型 T
仍然是抽象的
Use of abstract type in a concrete class?
Concrete classes with abstract type members
Scala 中有一篇关于类型推断的论文:
Plociniczak, 休伯特;奥德斯基,马丁。解密本地类型推断 https://infoscience.epfl.ch/record/214757
如果要e
编译可以指定T
val e: Unit = C.asInstanceOf[C.type{type T = Int}]{println(1);2}
我有这个程序:
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}
}
行
val e = C{println(1);2}
告诉我 error:Type 不匹配,预期 C.T,实际:2
所以为什么我不能写
type T
def apply(c:T)
好像和
一样apply[T](c:T)
我写
时T的类型是什么val d=B{println(1);2}
我可以在这里写很多行!
因为T表示泛型,所以可以是Int,String,user defined classApple,Orange...
以及
的类型println(1);2
有类型"lines of codes"吗?
谢谢!
块的类型是块上最后一个表达式的类型。所以
{ println(...); 2 }
类型为 Int
.
B
和 C
之间的区别在于类型成员和类型参数 (1, 2) 之间类型推断的区别。
object B{
def apply[T](c:T)={}
}
object C{
type T
def apply(c:T)={}
}
class C1[T]{
def apply(c:T)={}
}
val d: Unit = B{println(1);2}
// val e: Unit = C{println(1);2} // doesn't compile
val e1: Unit = (new C1){println(1);2}
// scalacOptions ++= Seq("-Xprint:typer", "-Xprint-types")
// val d: Unit = A.this{A.type}.B.apply{[T](c: T)Unit}[Int]{(c: Int)Unit}({
// scala.Predef.println{(x: Any)Unit}(1{Int(1)}){Unit};
// 2{Int(2)}
// }{2}){Unit};
// val e: Unit = A.this{A.type}.C.apply{(c: A.C.T)Unit}({
// println{<null>}(1{Int(1)}){<null>};
// 2{Int(2)}
// }{<null>}){<error>};
// val e1: Unit = new A.C1[Int]{A.C1[Int]}{()A.C1[Int]}(){A.C1[Int]}.apply{(c: Int)Unit}({
// scala.Predef.println{(x: Any)Unit}(1{Int(1)}){Unit};
// 2{Int(2)}
// }{2}){Unit};
在 C
中类型 T
仍然是抽象的
Use of abstract type in a concrete class?
Concrete classes with abstract type members
Scala 中有一篇关于类型推断的论文:
Plociniczak, 休伯特;奥德斯基,马丁。解密本地类型推断 https://infoscience.epfl.ch/record/214757
如果要e
编译可以指定T
val e: Unit = C.asInstanceOf[C.type{type T = Int}]{println(1);2}