Created
          May 26, 2011 04:47 
        
      - 
      
- 
        Save yuroyoro/992568 to your computer and use it in GitHub Desktop. 
    OptManifest[A]とかNoManifestとかの話をしよう
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /* | |
| OptManifest[A]とかNoManifestとかの話をしよう | |
| OptManifest[A]ってのは、Manifestがあるかないか分からない場合に使う。 | |
| 値があるかないかをOption[A]で表すように、 | |
| 型パラメータAに対するManifestがあるかないかわからない場合はOptManifest[A]をつかう | |
| Manifestの継承関係は以下の通り。OptManifestは全てのManifestの親traitになっている。 | |
| trait OptManifest [+T] | |
| object NoManifest extends OptManifest[Nothing] | |
| trait ClassManifest [T] extends OptManifest[T] | |
| trait Manifest [T] extends ClassManifest[T] | |
| ManifestをどのようにScalaコンパイラが決定するかは、言語仕様の7.5 Manifestsに記載されている。 | |
| http://www.scala-lang.org/docu/files/ScalaReference.pdf | |
| http://www29.atwiki.jp/tmiya/pages/108.html | |
| ようは、型パラメータTをscalaコンパイラが特定できなかった場合にNoManifestが渡される。 | |
| 通常は型パラメータをScalaコンパイラが特定できない局面は存在しないが、Javaと連携する際に、 | |
| ワイルドカードパターンで表現されたコンテナ型をもらった場合などは、NoManifestが渡される。 | |
| 正直、どういうときに使うと有効なのかわからない。型レベルプログラミングとかで | |
| 有効に使える局面があるのだろうか...? | |
| */ | |
| scala> def f[A](implicit optmf:OptManifest[A]) = optmf match { | |
| | case NoManifest => println("type prameter A is not given. %s" format optmf) | |
| | case mf:ClassManifest[_] => println("type parameter A is %s" format mf) | |
| | } | |
| f: [A](implicit optmf: OptManifest[A])Unit | |
| scala> | |
| scala> f[String] | |
| type parameter A is java.lang.String | |
| scala> f[T forSome{type T}] | |
| type prameter A is not given. <?> | |
| scala> f[Nothing] | |
| type parameter A is Nothing | |
| scala> f[Array[Int]] | |
| type parameter A is Array[Int] | |
| scala> f[Array[_]] | |
| type prameter A is not given. <?> | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment