progs/lecture4.scala
changeset 278 0c2481cd8b1c
parent 243 9bb36426c781
child 319 b84ea52bfd8f
equal deleted inserted replaced
277:acaf2099406a 278:0c2481cd8b1c
   425 }
   425 }
   426 
   426 
   427 "HAL".increment
   427 "HAL".increment
   428 
   428 
   429 
   429 
       
   430 // Abstract idea:
       
   431 // In that version implicit conversions were used to solve the 
       
   432 // late extension problem; namely, given a class C and a class T, 
       
   433 // how to have C extend T without touching or recompiling C. 
       
   434 // Conversions add a wrapper when a member of T is requested 
       
   435 // from an instance of C.
       
   436 
       
   437 //Another example
       
   438 
       
   439 case class Duration(time: Long, unit: TimeUnit) {
       
   440   def +(o: Duration) = Duration(time + unit.convert(o.time, o.unit), unit)
       
   441 }
       
   442 
       
   443 implicit class Int2Duration(that: Int) {
       
   444   def seconds = new Duration(that, SECONDS)
       
   445   def minutes = new Duration(that, MINUTES)
       
   446 }
       
   447 
       
   448 5.seconds + 2.minutes //Duration(125L, SECONDS )
       
   449 
       
   450 
   430 
   451 
   431 // Regular expressions - the power of DSLs in Scala
   452 // Regular expressions - the power of DSLs in Scala
   432 //==================================================
   453 //==================================================
   433 
   454 
   434 abstract class Rexp
   455 abstract class Rexp