Showing posts with label type alias. Show all posts
Showing posts with label type alias. Show all posts

Friday, February 19, 2010

=>? type alias for PartialFunction

A big thanks to Ben Jackman for this tip.

His tip cleans up the examples in post Chaining Partial Functions with orElse
The secret is to define the type alias type =>?[-A, +B] = PartialFunction[A, B]. This alias may be added to Predef in the future but until it is you can add it yourself.
  1. scala> type =>?[-A, +B] = PartialFunction[A, B]
  2. defined type alias $eq$greater$qmark
  3. scala> val i : (Any =>? Unit) = {case x:Int => println("int found")}
  4. i: =>?[Any,Unit] = < function1>
  5. scala> val j : (Any =>? Unit) = {case x:Double => println("Double found")}
  6. j: =>?[Any,Unit] = < function1>
  7. scala> val * : (Any =>? Unit) = {case x=> println("Something else found")}
  8. *: =>?[Any,Unit] = < function1>
  9. scala> (i orElse j orElse *)(1)
  10. int found
  11. scala> (i orElse j orElse *)(1.0)
  12. Double found
  13. scala> (i orElse j orElse *)(true)
  14. Something else found
  15. scala> def =>?[A, B](id : A =>? B) = id
  16. $eq$greater$qmark: [A,B](id: =>?[A,B])=>?[A,B]
  17. scala> ( =>?[Any, Unit]{case s : String => println("String found")} orElse j orElse *)("hello")
  18. String found

Tuesday, February 9, 2010

Structural Types: Multiple Methods and Type Aliasing

There are two more aspects related to structural typing that are useful to look at. Structural types with multiple methods and type aliases.

For background on this topic also look at:
Structural Types are not limited to defining a single method. In that regard they are very similar to interfaces without the binary incompatibility issues. However do not be fooled into thinking they are the same thing. For one reason reflection is used so performance can be an issue in certain cases and also interfaces/traits have semantics that structural types do not.
  1. /*
  2. Defining a types that has both a length and charAt method.  
  3. Just a warning.  If you leave off the () after length this will not work.  This is not a bug.  Martin kindly left a comment on why it is not.
  4. */
  5. scala> def foreach(t : {def length():Intdef charAt(i:Int):Char}, f : Char => Unit) = {
  6.      | 0 until t.length foreach {i => f(t.charAt(i))}  
  7.      | }
  8. foreach: (t: AnyRef{def length(): Intdef charAt(i: Int): Char},f: (Char) => Unit)Unit
  9. // A string matches the structural type
  10. scala> foreach ("hello", println _)
  11. h
  12. e
  13. l
  14. l
  15. o

Pretty unexpected I would say. A feature of Scala which complements Structural types are type aliases. They are useful in many situations and one is with use with Structural Types:
  1. /*
  2. With type aliasing you can assign a structural type a name
  3. */
  4. scala> type IChar = {def length():Int
  5.      |               def charAt(i:Int):Char}
  6. defined type alias IChar
  7. scala> def print( t : IChar) = 0 until t.length() foreach {i => println(t.charAt(i))}
  8. print: (t: IChar)Unit
  9. scala> print("gurk")
  10. g
  11. u
  12. r
  13. k