progs/lecture4.scala
changeset 278 0c2481cd8b1c
parent 243 9bb36426c781
child 319 b84ea52bfd8f
--- a/progs/lecture4.scala	Fri Aug 16 08:45:21 2019 +0100
+++ b/progs/lecture4.scala	Tue Oct 29 09:54:52 2019 +0000
@@ -427,6 +427,27 @@
 "HAL".increment
 
 
+// Abstract idea:
+// In that version implicit conversions were used to solve the 
+// late extension problem; namely, given a class C and a class T, 
+// how to have C extend T without touching or recompiling C. 
+// Conversions add a wrapper when a member of T is requested 
+// from an instance of C.
+
+//Another example
+
+case class Duration(time: Long, unit: TimeUnit) {
+  def +(o: Duration) = Duration(time + unit.convert(o.time, o.unit), unit)
+}
+
+implicit class Int2Duration(that: Int) {
+  def seconds = new Duration(that, SECONDS)
+  def minutes = new Duration(that, MINUTES)
+}
+
+5.seconds + 2.minutes //Duration(125L, SECONDS )
+
+
 
 // Regular expressions - the power of DSLs in Scala
 //==================================================