Created
September 19, 2012 03:26
-
-
Save choplin/3747493 to your computer and use it in GitHub Desktop.
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
| job.setOutputFormatClass(classOf[TextOutputFormat]) // エラー | |
| job.setOutputFormatClass(classOf[TextOutputFormat[Text, Text]]) // OK |
そもそも java.lang.Class 型のオブジェクトって以下のようになるので
Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_35).
Type in expressions to have them evaluated.
Type :help for more information.
scala> classOf[Option[Int]]
res0: java.lang.Class[Option[Int]] = class scala.Option
scala> classOf[Option[String]]
res1: java.lang.Class[Option[String]] = class scala.Option
scala> res0 == res1
res2: Boolean = trueclassOfに渡す型に型パラメータを渡しても意味がなかった(なので結局 classOf[Option[_]] って書けばいい )気がするんですが、違いましたっけ?
あ、でもとりだそうと思えばできますね
scala> res0.getTypeParameters
res3: Array[java.lang.reflect.TypeVariable[java.lang.Class[Option[Int]]]] = Array(A)
scala> res1.getTypeParameters
res4: Array[java.lang.reflect.TypeVariable[java.lang.Class[Option[String]]]] = Array(A)ただ、その情報を内部で使ってないなら classOf[Option[_]] とかでいい気がします
Author
job.setOutputFormatClass(classOf[TextOutputFormat[_,_]])で動いたので内部では型パラメータは使っていないようです。
これでHadoop MRは大分シンプルに書けそうです。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
長い型を書くのがタルいんだったらtype aliasを使えばよいと思うマス
genericなclassOfが欲しいなら、こんな感じでユーティリティな関数を用意しておくくらいでしょうか。
2.10だったら、ManifestはdeplicatedになるのでTypeTagやClassTagを使って下さい。
可変長型引数に対応してないので、kindの数に応じてgenClassOf2とか作るしかないです。
多分2.10になってマクロ使えたらもっと綺麗に書ける。
ここまで書いて、結局タルいのあんまり解決してないのに気がついた……