Sure, HLists handle this with aplomb: val xs = 1 :: 'a' :: 3.0f :: "hello world" :: HNil
And this isn't just a "List of Int | Char | Float | String", it's a "List of Int *then* Char *then* Float *then* String". Start taking heads and tails, it keeps tracking the type correctly: xs.head // Int = 1 xs.tail.tail.head // Float = 3.0 You also get the usual map, flatMap, etc (all type-preserving) To the best of my knowledge, shapeless doesn't currently have a method to convert that to a List[Int :+: Char :+: Float :+: String :+: CNil] (which is how shapeless represents unions) But it's certainly something that *could* be implemented by anyone sufficiently familiar with shapeless. If you call xs.toList, it gets you back to a List[Any] On 21 February 2014 19:52, Ricky Clarkson <[email protected]> wrote: > Regarding List(1, "a"), I guess shapeless can likely handle that and turn > it into either a HList whose first item is always an Int and whose second > is always a String, or some list of Either[Int, String]. I haven't used > shapeless but that sounds like it would be in that library's domain. > > The Ceylon approach is interesting as a default; I'd prefer a > List<Integer|String> to a List[Any] any day. > > > On Fri, Feb 21, 2014 at 10:52 AM, Cédric Beust ♔ <[email protected]> wrote: > >> On Fri, Feb 21, 2014 at 10:14 AM, Kevin Wright <[email protected]> >> wrote: >> >> It can directly duplicate anything available in Kotlin or Ceylon using >>> nothing more than built-in features >>> >> Not really, because the type system doesn’t know about union and >> intersection types. >> >> scala> List(1, "a") >> res0: List[Any] = List(1, a) >> >> In Ceylon, the type of this list would be List<Integer|String>. >> >> Null support flows very naturally out of this also, without requiring >> hacks to make it work. For example, Null|Person can’t be assigned to a >> Person without proper checking, and it’s also aliased to Person? for >> convenience. >> -- >> Cédric >> >> -- You received this message because you are subscribed to the Google Groups "Java Posse" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/javaposse. For more options, visit https://groups.google.com/groups/opt_out.
