Hacker News new | past | comments | ask | show | jobs | submit login

On the compare page: "Reified generics (check generic types at run time)"

Does anyone know how they've gotten around the issue of Type Erasure on the JVM?




Here are a few hints in the manual:

"you can add objects to a collection, but not primitives".

Why not?

"The collection and list classes used frequently in Gosu rely on the Java language’s collection classes. However, there are important differences because of built-in enhancements to these classes that use Gosu blocks, anonymous in-line defined functions that are not directly supported in the Java language."

It also has some type-inference that converts:

  var str = {"foo", "bar", "baz"}
To

  ArrayList<String> str = new ArrayList<String>();
  str.add("foo").add("bar").add("baz");
Or some such.

LtU wont be going wild over this any time soon.


We don't work around Java type erasure: if you create some object in Java and pass it off to Gosu, then that information is lost. For Gosu classes that are reified, we actually store the type parameter on the object itself, and it becomes an implicit constructor argument. For generified functions, we pass the type parameter through that's determined statically by the parser. For enhancements on generified Java classes, we also pass through the statically-determined type parameter. I.e. if you have a Gosu class Foo generified on T, when you construct Foo with type param String we'll pass the String type through to the constructor and store it on the object instance so we can retrieve it later. If you have something the compiler thinks is a List of Objects, and you call an enhancement method on List, we'll pass through Object as the type parameter, even if at runtime it's actually a List of Strings. So it's not perfect in its interaction with Java code, but it's usually good enough (and better than losing it entirely). As far as the Java classes we generate go, your generic type Foo has only one class, but as far as Gosu's type system goes, Foo parameterized on Object is a separate type instance from Foo parameterized on String.


Does this extract from the Reference Guide answer your question?

"Gosu types preserve generic type information at run time. This Gosu feature is called reified generics. This means that in complex cases you could check the exact type of an object at run time, including any parameterization. In contrast, Java discards this information completely after compilation, so it is unavailable at run time.

"Note: Even in Gosu, parameterization information is unavailable for all native Java types because Java does not preserve this information beyond compile time. For example the run time type of java.util.List<Address> in Gosu returns the unparameterized type java.util.List."

From http://gosu-lang.org/doc/wwhelp/wwhimpl/js/html/wwhelp.htm#h...




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: