2010-08-27 93 views
23

我有一些Scala代码,使相当大量的泛型使用,我从文档中收集在参数化约束中使用清单可以帮助我解决类型擦除问题(例如,我想实例化一个新的对象泛型类型)。只是,我想了解更多关于这是如何工作的。它几乎感觉就像某种散列表,它为每个调用网站获得一个条目......任何人都可以在这里详细说明吗?Scala的(2.8)Manifest如何工作?

class Image[T <: Pixel[T] : Manifest](fun() => T, size: Array[Int], data: Array[T]) { 
    def this(fun:() => T, size: Array[T]) { 
     this(fun, size, new Array[T](size(0) * size(1)); 
    } 
} 

这件事似乎没有任何,我在网站上发现的文件被覆盖,并在谷歌我大部分得到具有非常不同的语法旧帖子,并自2.8似乎有很多事情发生了变化,我不确定这些事情是否准确。

+0

可能重复(http://stackoverflow.com/问题/ 3213510 /什么是一个清单在scala和什么时候你需要它) – 2010-08-27 19:38:35

+1

我不确定那些答案真的回答它的工作原理,但?至少它可以更清楚。编译器为方法/函数添加一个额外参数以保存泛型参数的具体类的意义何在?无论如何,<:<运算符是什么? – djc 2010-08-27 19:42:55

+0

看看这里:http://debasishg.blogspot.com/2010/08/using-generalized-type-constraints-how.html – 2010-08-27 19:47:31

回答

35

它已经一段时间,因为我经历了Scala的源代码在寻求回答同样的问题......但简短的回答挖,我记得 -

清单是个骗子代码,让编译器绕过类型擦除(它不是在运行时使用)。它会在编译时为匹配清单的可能输入类型生成多个代码路径。

Manifest是隐式解析的,但是如果在编译时有关Manifest类型的含义有任何歧义,编译器将停止。

随着Manifest的副本,你有几件事情可用。您通常希望主要的事情是,要么经erasure擦除java.lang.Class

class BoundedManifest[T <: Any : Manifest](value: T) { 
    val m = manifest[T] 
    m.erasure.toString match { 
    case "class java.lang.String" => println("String") 
    case "double" | "int" => println("Numeric value.") 
    case x => println("WTF is a '%s'?".format(x)) 
    } 
} 

class ImplicitManifest[T <: Any](value: T)(implicit m: Manifest[T]) { 
    m.erasure.toString match { 
    case "class java.lang.String" => println("String") 
    case "double" | "int" => println("Numeric value.") 
    case x => println("WTF is a '%s'?".format(x)) 
    } 
} 

new BoundedManifest("Foo Bar!") 
// String 
new BoundedManifest(5) 
// Numeric value. 
new BoundedManifest(5.2) 
// Numeric value. 
new BoundedManifest(BigDecimal("8.62234525")) 
// WTF is a 'class scala.math.BigDecimal'? 
new ImplicitManifest("Foo Bar!") 
// String 
new ImplicitManifest(5) 
// Numeric value. 
new ImplicitManifest(5.2) 
// Numeric value. 
new ImplicitManifest(BigDecimal("8.62234525")) 
// WTF is a 'class scala.math.BigDecimal'? 

这是一个相当靠不住的例子,但说明是怎么回事。我在Scala 2.8上运行该输出以及FWIW。

[T ... : Manifest]边界在Scala 2.8中是新的......你以前必须隐式地抓取清单,如ImplicitManifest所示。你实际上并没有获得Manifest的副本。但是,您可以通过说val m = manifest[T] ... manifest[_]Predef上定义,并且可以在限制块内找到正确的清单类型,从而在代码中获取一个。

您从Manifest获得的另外两个主要项目是<:<>:>,它测试一个清单的子类型/超类型与另一个清单。如果我正确记得这些是非常天真的执行方式,并不总是匹配,但我有一堆生产代码使用它们来测试一些可能的擦除输入。

class BoundedManifestCheck[T <: Any : Manifest](value: T) { 
    val m = manifest[T] 
    if (m <:< manifest[AnyVal]) { 
    println("AnyVal (primitive)") 
    } else if (m <:< manifest[AnyRef]) { 
    println("AnyRef") 
    } else { 
    println("Not sure what the base type of manifest '%s' is.".format(m.erasure)) 
    } 
} 


new BoundedManifestCheck("Foo Bar!") 
// AnyRef 
new BoundedManifestCheck(5) 
// AnyVal (primitive) 
new BoundedManifestCheck(5.2)  
// AnyVal (primitive) 
new BoundedManifestCheck(BigDecimal("8.62234525")) 
// AnyRef 

豪尔赫·奥尔蒂斯有一个伟大的博客文章(虽然是旧)本:http://www.scala-blogs.org/2008/10/manifests-reified-types.html

编辑:对另一清单检查的一个简单的例子

实际上,你可以看到Scala是什么通过要求它打印出擦除编译器阶段的结果。

运行,在我的最后一个例子以上scala -Xprint:erasure test.scala产生以下结果:?什么是Scala中的一个清单,当你需要它]的

final class Main extends java.lang.Object with ScalaObject { 
    def this(): object Main = { 
    Main.super.this(); 
    () 
    }; 
    def main(argv: Array[java.lang.String]): Unit = { 
    val args: Array[java.lang.String] = argv; 
    { 
     final class $anon extends java.lang.Object { 
     def this(): anonymous class $anon = { 
      $anon.super.this(); 
     () 
     }; 
     class BoundedManifestCheck extends java.lang.Object with ScalaObject { 
      <paramaccessor> private[this] val value: java.lang.Object = _; 
      implicit <paramaccessor> private[this] val evidence$1: scala.reflect.Manifest = _; 
      def this($outer: anonymous class $anon, value: java.lang.Object, evidence$1: scala.reflect.Manifest): BoundedManifestCheck = { 
      BoundedManifestCheck.super.this(); 
      () 
      }; 
      private[this] val m: scala.reflect.Manifest = scala.this.Predef.manifest(BoundedManifestCheck.this.evidence$1); 
      <stable> <accessor> def m(): scala.reflect.Manifest = BoundedManifestCheck.this.m; 
      if (BoundedManifestCheck.this.m().<:<(scala.this.Predef.manifest(reflect.this.Manifest.AnyVal()))) 
      scala.this.Predef.println("AnyVal (primitive)") 
      else 
      if (BoundedManifestCheck.this.m().<:<(scala.this.Predef.manifest(reflect.this.Manifest.Object()))) 
       scala.this.Predef.println("AnyRef") 
      else 
       scala.this.Predef.println(scala.this.Predef.augmentString("Not sure what the base type of manifest '%s' is.").format(scala.this.Predef.genericWrapArray(Array[java.lang.Object]{BoundedManifestCheck.this.m().erasure()}))); 
      protected <synthetic> <paramaccessor> val $outer: anonymous class $anon = _; 
      <synthetic> <stable> def Main$$anon$BoundedManifestCheck$$$outer(): anonymous class $anon = BoundedManifestCheck.this.$outer 
     }; 
     new BoundedManifestCheck($anon.this, "Foo Bar!", reflect.this.Manifest.classType(classOf[java.lang.String])); 
     new BoundedManifestCheck($anon.this, scala.Int.box(5), reflect.this.Manifest.Int()); 
     new BoundedManifestCheck($anon.this, scala.Double.box(5.2), reflect.this.Manifest.Double()); 
     new BoundedManifestCheck($anon.this, scala.package.BigDecimal().apply("8.62234525"), reflect.this.Manifest.classType(classOf[scala.math.BigDecimal])) 
     }; 
     { 
     new anonymous class $anon(); 
     () 
     } 
    } 
    } 
} 
+1

另外值得注意的是,从最后一个例子中删除':Manifest'会导致编译失败 - 错误:当您调用val m = manifest [T]'时,找不到参数m:Manifest [T]的隐式值。上下文边界提供'manifest [_]'调用需要操作的信息,所以它不是可选的。 – 2010-08-27 23:17:29

4

“上下文绑定”T ... : Manifest是隐含参数的语法糖:(implicit man: Manifest[T])。因此,在由class Image指定的类型构造函数的实例化处,编译器查找/提供用于类型参数T的实际类型的Manifest,并且该值“坚持”整个存在的结果类实例和每个“锚” Image[Something]的特定实例为其清单T

+0

谢谢,这有帮助,但我会有兴趣知道价值如何与实例挂钩,即它保存的位置。 – djc 2010-08-27 21:17:00

+0

与任何构造函数参数(在构造函数外引用)相同:在一个字段中。 – 2010-08-27 21:24:05

+0

编辑我的答案,包括擦除阶段的输出。实际上你可以让Scalac向编译器/运行时传递'-Xprint:erasure'参数来展示它的生成过程。这将在擦除阶段运行后打印出您的Scala代码的状态。 – 2010-08-27 21:27:41