Implicit defaults

89 views
Skip to first unread message

Alexander Kuklev

unread,
Aug 8, 2012, 2:33:43 PM8/8/12
to scala-...@googlegroups.com
Often there are many implementations of data structures and algorithms with different performance. Currently, Scala provides sensible defaults:
val xs = List(1, 5, 2, 8, 4)
val xSorted = xs.sort

Unfortunatelly, there is no elegant way to override the defaults both locally or for an entire scope. In my opinion the most elegant way to achieve it are the implicits. If factories and methods with multiple implementation would accept the desired implementation as an implicit argument, this is how we'd be able to override the defaults:
implicit val sortMethod = SortMethod.TimSort
implicit val listVariant = ListVariant.DoublyLinkedList
val xs = List(1, 5, 2, 8, 4)
val xSorted = xs.sort
// and now override them locally
val xs2 = List(1, 5, 2, 8, 4)(ListVariant.XorList)
val xSorted2 = xs.sort(SortMethod.QuickSort)

This approach could be extended to numeric literals, we need the literals 42 to be intepreted as Int("42") where Int.apply takes an implicit argument "variant" (Int16, Int32, Int64, BigInteger) and 42.1 as Fractional("42.1") with implicit argument "variant" (Float, Double, Decimal, BigDecimal, FixedPoint(accuracy), Rational(accuracy), ContFraction(accuracy)):
implicit val intVariant = IntVariant.BigInteger
val x = 234794320943279322878

In order to completely separate implementation/optimization details from the substantial part of the algorithm, we need an analogon of Cascading Style Sheets. I would propose the following format:
«scope selector» {
  «type» = «implicit value of that type»
  ..
}

Here's an example:
com.miriamlaurel.test432 {
  SortMethod = QuickSort
  Ordering[String] = AlphabeticOrders.Swedish
  FractVariant = BigDecimal

  TestObj#cacheCriticalMethod {
    SortMethod = ShellSort
  }
}

I'd also provide the !important keyword from CSS with an optional argument "salience" for manual control of implicits priorities.
IntVariant = BigDecimal !important(100)

Apart from support for numeric literals, everything described above can be implemented with means already present in Scala 2.10 (mainly, macros). What do you think?

Chris Hodapp

unread,
Aug 9, 2012, 12:44:24 PM8/9/12
to scala-...@googlegroups.com
You had me until you brought up the CSS-inspired DSL. What, exactly, would you want this to compile to? Or are you saying that you want macros to inject code into the specified scope (and are macros run early enough to make such destructive changes?)?

Alexander Kuklev

unread,
Aug 9, 2012, 12:54:32 PM8/9/12
to scala-...@googlegroups.com


четверг, 9 августа 2012 г., 18:44:24 UTC+2 пользователь Chris Hodapp написал:
You had me until you brought up the CSS-inspired DSL. What, exactly, would you want this to compile to? Or are you saying that you want macros to inject code into the specified scope (and are macros run early enough to make such destructive changes?)?

Yes, exactly, I want macros (or some other mechanism) to parse the CSS-style files and inject implicit values defined there into correspoing scopes. As far as I understand now, as soon as macro annotations will be there, it will be possible to supply a large chunk of code with an annotation that would perform the necessary injections for that chunk.
Reply all
Reply to author
Forward
0 new messages