Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 2.27 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Good example of implicit parameter in Scala?
  2. max(x : Int,y : Int) : Int
  3.        
  4. max(5,6);
  5.        
  6. max(x:5,y:6);
  7.        
  8. x = 5;
  9. y = 6;
  10. max()
  11.        
  12. max() : Int
  13. {
  14.   global x : Int;
  15.   global y : Int;
  16.   ...
  17. }
  18.        
  19. def max[T <% Ordered[T]](a: T, b: T): T = if (a < b) b else a
  20. def max[T](a: T, b: T)(implicit $ev1: Function1[T, Ordered[T]]): T = if ($ev1(a) < b) b else a
  21.        
  22. def max[T](a: T, b: T)(implicit $ev1: Ordering[T]): T = if ($ev1.lt(a, b)) b else a
  23. // latter followed by the syntactic sugar
  24. def max[T: Ordering](a: T, b: T): T = if (implicitly[Ordering[T]].lt(a, b)) b else a
  25.        
  26. new Array[Int](size)
  27.        
  28. def f[T](size: Int) = new Array[T](size) // won't compile!
  29.        
  30. def f[T: ClassManifest](size: Int) = new Array[T](size)
  31.        
  32. Manifest      // Provides reflection on a type
  33. ClassManifest // Provides reflection on a type after erasure
  34. Ordering      // Total ordering of elements
  35. Numeric       // Basic arithmetic of elements
  36. CanBuildFrom  // Collection creation
  37.        
  38. def withTransaction(f: Transaction => Unit) = {
  39.   val txn = new Transaction
  40.  
  41.   try { f(txn); txn.commit() }
  42.   catch { case ex => txn.rollback(); throw ex }
  43. }
  44.  
  45. withTransaction { txn =>
  46.   op1(data)(txn)
  47.   op2(data)(txn)
  48.   op3(data)(txn)
  49. }
  50.        
  51. withTransaction { implicit txn =>
  52.   op1(data)
  53.   op2(data)
  54.   op3(data)
  55. }
  56.        
  57. def flatten[B](implicit ev: A <:< Option[B]): Option[B]
  58.        
  59. scala> Option(Option(2)).flatten // compiles
  60. res0: Option[Int] = Some(2)
  61.  
  62. scala> Option(2).flatten // does not compile!
  63. <console>:8: error: Cannot prove that Int <:< Option[B].
  64.               Option(2).flatten // does not compile!
  65.                         ^
  66.        
  67. trait ScalaActorRef { this: ActorRef =>
  68.   ...
  69.  
  70.   def !(message: Any)(implicit sender: ActorRef = null): Unit
  71.  
  72.   ...
  73. }
  74.        
  75. trait Actor {
  76.   ...
  77.  
  78.   implicit val self = context.self
  79.  
  80.   ...
  81. }
  82.        
  83. someOtherActor ! SomeMessage
  84.        
  85. someOtherActor.!(SomeMessage)(self)
  86.        
  87. someOtherActor.!(SomeMessage)(null)
  88.        
  89. someOtherActor.!(SomeMessage)(anotherActorAltogether)
  90.        
  91. def max[B >: A](implicit cmp: Ordering[B]) : A
  92.        
  93. implicit val num = 2
  94. implicit val item = "Orange"
  95. def shopping(implicit num: Int, item: String) = {
  96.   "I’m buying "+num+" "+item+(if(num==1) "." else "s.")
  97. }
  98.  
  99. scala> shopping
  100. res: java.lang.String = I’m buying 2 Oranges.
  101.        
  102. def map[B, That](f: (A) ⇒ B)(implicit bf: CanBuildFrom[GenSeq[A], B, That]): That