Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何区分参数类型?_Scala_Generics_Reflection_Scala Reflect_Erasure - Fatal编程技术网

Scala 如何区分参数类型?

Scala 如何区分参数类型?,scala,generics,reflection,scala-reflect,erasure,Scala,Generics,Reflection,Scala Reflect,Erasure,我对Scala中的子类型感到困惑。我的主要问题是如何区分C[T1]和C[T2]。有两种情况: C[T1]“等于”C[T2],因为它们都是C的子类型 C[T1]不等于C[T2],因为C[T1]和C[T2]最终是不同的类型 我尝试过一些方法,比如.getClass,但似乎这种策略不起作用,因为我们有基本类型 println(List[Int](1).getClass == List[Double](1.0).getClass) // True println(List[Int](1).getClas

我对Scala中的子类型感到困惑。我的主要问题是如何区分
C[T1]
C[T2]
。有两种情况:

  • C[T1]
    “等于”
    C[T2]
    ,因为它们都是
    C
    的子类型
  • C[T1]
    不等于
    C[T2]
    ,因为
    C[T1]
    C[T2]
    最终是不同的类型
  • 我尝试过一些方法,比如
    .getClass
    ,但似乎这种策略不起作用,因为我们有基本类型

    println(List[Int](1).getClass == List[Double](1.0).getClass) // True
    println(List[Int](1).getClass.getCanonicalName) // scala.collection.immutable.$colon$colon
    println(Array[Int](1).getClass == Array[Double](1.0).getClass) // False
    println(Array[Int](1).getClass.getCanonicalName) // int[]
    

    我现在想知道有什么方法可以做到这一点吗?

    列表[Int]
    列表[Double]
    具有相同的类,但不同的类型

    import scala.reflect.runtime.universe._
    
    println(typeOf[List[Int]] =:= typeOf[List[Double]])//false
    println(typeOf[List[Int]].typeConstructor =:= typeOf[List[Double]].typeConstructor)//true
    println(typeOf[List[Int]])//List[Int]
    println(showRaw(typeOf[List[Int]]))//TypeRef(SingleType(SingleType(ThisType(<root>), scala), scala.package), TypeName("List"), List(TypeRef(ThisType(scala), scala.Int, List())))
    
    println(classOf[List[Int]] == classOf[List[Double]])//true
    println(classOf[List[Int]])//class scala.collection.immutable.List
    println(classOf[List[Int]].getCanonicalName)//scala.collection.immutable.List
    

    C[T1]
    “等于”
    C[T2]
    ,因为它们都是
    C
    的子类型

    它们不是子类型

    import scala.reflect.runtime.universe._
    
    println(typeOf[List[Int]] =:= typeOf[List[Double]])//false
    println(typeOf[List[Int]].typeConstructor =:= typeOf[List[Double]].typeConstructor)//true
    println(typeOf[List[Int]])//List[Int]
    println(showRaw(typeOf[List[Int]]))//TypeRef(SingleType(SingleType(ThisType(<root>), scala), scala.package), TypeName("List"), List(TypeRef(ThisType(scala), scala.Int, List())))
    
    println(classOf[List[Int]] == classOf[List[Double]])//true
    println(classOf[List[Int]])//class scala.collection.immutable.List
    println(classOf[List[Int]].getCanonicalName)//scala.collection.immutable.List
    


    Scala中的子类型:什么是“X型
    列表[Int]
    列表[Double]
    具有相同的,但不同的类型

    import scala.reflect.runtime.universe._
    
    println(typeOf[List[Int]] =:= typeOf[List[Double]])//false
    println(typeOf[List[Int]].typeConstructor =:= typeOf[List[Double]].typeConstructor)//true
    println(typeOf[List[Int]])//List[Int]
    println(showRaw(typeOf[List[Int]]))//TypeRef(SingleType(SingleType(ThisType(<root>), scala), scala.package), TypeName("List"), List(TypeRef(ThisType(scala), scala.Int, List())))
    
    println(classOf[List[Int]] == classOf[List[Double]])//true
    println(classOf[List[Int]])//class scala.collection.immutable.List
    println(classOf[List[Int]].getCanonicalName)//scala.collection.immutable.List
    

    C[T1]
    “等于”
    C[T2]
    ,因为它们都是
    C
    的子类型

    它们不是子类型

    import scala.reflect.runtime.universe._
    
    println(typeOf[List[Int]] =:= typeOf[List[Double]])//false
    println(typeOf[List[Int]].typeConstructor =:= typeOf[List[Double]].typeConstructor)//true
    println(typeOf[List[Int]])//List[Int]
    println(showRaw(typeOf[List[Int]]))//TypeRef(SingleType(SingleType(ThisType(<root>), scala), scala.package), TypeName("List"), List(TypeRef(ThisType(scala), scala.Int, List())))
    
    println(classOf[List[Int]] == classOf[List[Double]])//true
    println(classOf[List[Int]])//class scala.collection.immutable.List
    println(classOf[List[Int]].getCanonicalName)//scala.collection.immutable.List
    


    Scala中的子类型:什么是“X类型查看Scala中的类型擦除:您试图解决的元问题是什么?上一个OP的问题在这里:查看Scala中的类型擦除:您试图解决的元问题是什么?上一个OP的问题在这里: