What's different between “def apply[T](c:T)” and “type T;def apply(c:T)”

℡╲_俬逩灬. 提交于 2020-11-29 21:17:37

问题


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!


回答1:


The type of a block is the type of the last expression on the block. So

{ println(...); 2 } 

has type Int.

Difference between B and C is difference in type inference between type members and type parameters (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};

In C type T remains abstract

Use 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 specify T

val e: Unit = C.asInstanceOf[C.type{type T = Int}]{println(1);2}


来源:https://stackoverflow.com/questions/59148665/whats-different-between-def-applytct-and-type-tdef-applyct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!