On 2013-01-12 7:31, Eugene Burmako wrote:
> With type macros, some changes to Dynamic and optional changes to when
> Dynamic lookup is triggered, we could implement on-demand generation
> of tuples/products/etc with arbitrary arities. Does this sound
> interesting?
I don't think we should generate tuple classes. An advantage of that
would be ad-hoc specialization for large tuples (currently limited up to
arity 3 because it's not ad-hoc, so the number of required classes grows
exponentially). The disadvantage is the mess caused by having multiple
copies of the same class in different JARs or even different classloaders.
My current plan goes like this:
- Start with an HArray encoding that works without any implicits or
other hidden runtime overhead:
https://github.com/szeiger/ErasedTypes/blob/master/src/main/scala/com/novocode/erased/HArray.scala.
All methods which abstract over arity are defined in HArray. (There's
not much to see in this HArray implementation but the HList used for
typing it has a lot more methods, e.g. head, tail, consing,
concatenation, all of which can be typechecked without runtime overhead)
- Have arity-specialized tuple classes up to a certain size (initially
22, can be reduced later) which extend HArray. Tuple2 and Tuple3 would
be kept fully specialized. For larger arities, use an array-based HArray
implementation (HArrayA in the code above). This can be made a value
class in 2.10 so it would erase to a raw array in many cases.
- The fixed _1 ... _n accessor methods could be added to HArrayA with
smoke and mirrors (a.k.a. Dynamic and macros). Problem: Auto-complete in
IDEs won't work out of the box.
- The desugaring in the parser changes tuple syntax to the most specific
implementation (i.e. (1,2) becomes Tuple2(1,2), (1,2,...,23) becomes
HArrayA(Array[Any](1,2,...,23)). Once you abstract over arity (e.g.
concatenating two tuples), you only get an HArray, even if the
implementation is (or could be) specialized.
To do:
- Can Nat be a value class? (Doesn't work in 2.10 because you cannot get
a singleton type from a value class)
- What about functions?
-sz