LoginSignup
0
0

More than 5 years have passed since last update.

CaseClassかどうか判定する

Last updated at Posted at 2015-03-02

CaseClassかどうか判定する

import scala.reflect.runtime.universe._

def isCaseClass(typ: Type): Boolean =
  if (typ <:< typeOf[Product])
    ! typ.typeSymbol.fullName.startsWith("scala.")
  else
    false

判定基準は以下2点。

  • Product であること
  • fullName が "scala." から始まってないこと

FQCN文字列で切り分けるとか方法が原始的(?)だが、ワリとまともに動くはず。


以下、テスト。

case class Sample(text: String)

def test(typ: Type) {
  println(s"${typ} is CaseClass ? ${isCaseClass(typ)}")
}

test(typeOf[Sample])
// => Sample is CaseClass ? true

test(typeOf[List[Any]])
// => scala.List[Any] is CaseClass ? false

test(typeOf[::[Any]])
// => scala.::[Any] is CaseClass ? false

test(typeOf[Option[Any]])
// => scala.Option[Any] is CaseClass ? false

test(typeOf[Tuple2[Any,Any]])
// => (Any, Any) is CaseClass ? false

test(typeOf[util.Success[Any]])
// => scala.util.Success[Any] is CaseClass ? false

test(typeOf[util.Right[Any,Any]])
// => scala.util.Right[Any,Any] is CaseClass ? false
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0