Showing posts with label Enumeration. Show all posts
Showing posts with label Enumeration. Show all posts
Friday, January 29, 2010
Overcoming Type Erasure in Matching 2 (Variance)
A commenter (Brian Howard) on the post Overcoming Type Erasure in Matching 1 made a very good point:
There is a way to do this, however the information is not captured in the Manifest. A manifest is not designed to do full reflection it is by design very light and has little impact on performance. So to provide the functionality requested by Brian one needs to add that information to the Extractor Definition. Have have a possible solution below.
Is there a way to deal with some type arguments being contravariant? Try the following:
class A
class B extends A
val AAFunction = new Def[Function1[A,A]]
((a:A) => a) match {case AAFunction(f) => Some(f(new A)); case _ => None} // this is OK
((a:A) => new B) match {case AAFunction(f) => Some(f(new A)); case _ => None} // this is OK
((b:B) => b) match {case AAFunction(f) => Some(f(new A)); case _ => None} // gives a ClassCastException, since new A is not a B
There is a way to do this, however the information is not captured in the Manifest. A manifest is not designed to do full reflection it is by design very light and has little impact on performance. So to provide the functionality requested by Brian one needs to add that information to the Extractor Definition. Have have a possible solution below.
- scala> class A
- defined class A
- scala> class B extends A
- defined class B
- scala> object Variance extends Enumeration {
- | val Co, Contra, No = Value
- | }
- defined module Variance
- scala> import Variance._
- import Variance._
- scala> class Def[C](variance:Variance.Value*)(implicit desired : Manifest[C]) {
- | def unapply[X](c : X)(implicit m : Manifest[X]) : Option[C] = {
- | val typeArgsTriplet = desired.typeArguments.zip(m.typeArguments).
- | zipWithIndex
- | def sameArgs = typeArgsTriplet forall {
- | case ((desired,actual),index) if(getVariance(index) == Contra) =>
- | desired <:< actual
- | case ((desired,actual),index) if(getVariance(index) == No) =>
- | desired == actual
- | case ((desired,actual),index) =>
- | desired >:> actual
- | }
- |
- | val isAssignable = desired.erasure.isAssignableFrom(m.erasure) || (desired >:> m)
- | if (isAssignable && sameArgs) Some(c.asInstanceOf[C])
- | else None
- | }
- | def getVariance(i:Int) = if(variance.length > i) variance(i) else No
- | }
- defined class Def
- scala> val AAFunction = new Def[Function1[A,A]]
- AAFunction: Def[(A) => A] = Def@64a65760
- scala> ((a:A) => a) match {case AAFunction(f) => Some(f(new A)); case _ => None}
- res0: Option[A] = Some(A@1bd4f279)
- scala> ((a:A) => new B) match {case AAFunction(f) => Some(f(new A)); case _ => None}
- res1: Option[A] = None
- scala> ((b:B) => b) match {case AAFunction(f) => Some(f(new A)); case _ => None}
- res2: Option[A] = None
- scala> val BAFunction = new Def[Function1[B,A]](Contra,Co)
- BAFunction: Def[(B) => A] = Def@26114629
- scala> ((b:A) => b) match {case BAFunction(f) => Some(f(new B)); case _ => None}
- res3: Option[A] = Some(B@15dcc3ca)
Labels:
Advanced,
contravariance,
covariance,
Enumeration,
manifest,
match,
matching,
Scala,
variance
Sunday, August 23, 2009
Enumerations
Scala does not have a enum keyword like java so enumerations are not quite as smooth. Depending on your requirements there are two ways to make enumerations.
In the following examples notice the use of the sealed keyword when defining the abstract class MyEnum. Sealed specifies that the heirarchy is sealed. Only classes defined in the same file can extend the class.
Also notice in the case object example that the enumeration values are "case object" not "case class". The two are similar except that there is only one instance of a case object. However all the same boiler plate code is generated and you can still match in the same way.
- Create an object that extends the Enumeration class
- Create a case-object hierarchy.
- If you only need discrete and related values without custom behaviour create and object that extends Enumeration
- If each value has custom information associated with it use case-objects
In the following examples notice the use of the sealed keyword when defining the abstract class MyEnum. Sealed specifies that the heirarchy is sealed. Only classes defined in the same file can extend the class.
Also notice in the case object example that the enumeration values are "case object" not "case class". The two are similar except that there is only one instance of a case object. However all the same boiler plate code is generated and you can still match in the same way.
- // Note: MyEnum is an object NOT a class
- scala> object MyEnum extends Enumeration("one", "two", "three") {
- | type MyEnumType = Value
- | val One, Two, Three = Value
- | }
- defined module MyEnum
- scala> MyEnum.One
- res1: MyEnum.Value = one
- scala> def printEnum( value:MyEnum.MyEnumType ) = println( value.toString )
- printEnum: (MyEnum.MyEnumType)Unit
- scala> printEnum(MyEnum.Two)
- two
- // If you don't want to prefix enums with MyEnum. Then you
- // can import the values. This is similar to static imports in java
- scala> import MyEnum._
- import MyEnum._
- scala> def printEnum( value:MyEnumType ) = println( value.toString )
- printEnum: (MyEnum.MyEnumType)Unit
- scala> printEnum(Three)
- three
- // Similar but with cases objects
- // Notice MyEnum is 'sealed' and the parameters have the 'val' keyword so they are public
- scala> abstract sealed class MyEnum(val name:String, val someNum:Int)
- defined class MyEnum
- scala> case object One extends MyEnum("one", 1)
- defined module One
- scala> case object Two extends MyEnum("two", 2)
- defined module Two
- scala> case object Three extends MyEnum("three", 3)
- defined module Three
- scala> def printEnum(value:MyEnum) = println(value.name, value.someNum)
- printEnum: (MyEnum)Unit
- scala> printEnum(One)
- (one,1)
Labels:
beginner,
case-object,
enum,
Enumeration,
object,
Scala
Subscribe to:
Comments (Atom)