badlogic / quack

If it looks like a duck, walks like a duck... quack!
2 stars 0 forks source link

Type system research #4

Open badlogic opened 9 years ago

badlogic commented 9 years ago
any
   obj
      function
   val
     num
        int8, int32, ..
         uint8, uint32, ...
         float32, float64,
      bool

Generics should not be covariant, i.e. Array != Array. This means we may need auto-boxing when working with generics over primitive types.

Compare to C#

var arr = new Wrapper<object> [10];
arr [0] = new Wrapper<int>(10);  // fails, no covariance
arr [0] = new Wrapper<String>("HEllo World"); // fails, no covariance
badlogic commented 9 years ago

Turns out C# allows you to specify variance with in/out. There are still limits on primitive types.

badlogic commented 9 years ago

Concise overview www.cs.cornell.edu/Courses/cs312/2007fa/lectures/lec22-type-inf.pdf

Check how Java, Ceylon, Scala, C# et al treat variance.

badlogic commented 9 years ago
badlogic commented 9 years ago

Mixed site variance in Java, JEP: http://openjdk.java.net/jeps/8043488

badlogic commented 9 years ago

Scala's type hierarchy: http://docs.scala-lang.org/tutorials/tour/unified-types.html auto-boxing/unboxing for AnyVals. Can't create own AnyVal.

badlogic commented 9 years ago
badlogic commented 9 years ago

Assuming we treat everything as an object like Scala, but optimize for AnyVal (use primitives, auto-box/unbox). How'd we be able to generate code for this (ignore bool which is also an anyval)

def sum<T: AnyVal>(l:List<T>) {
   var sum: T
   foreach(v in l) {
      sum += v
   }
}

Well, i just answered my question. What about Any? Check what Scala folk can do with any. I.e. comparator interface over any. Possible? Or is any just a virtual type?