News for hackers. Stuff that matters.
Re:How generics were added to .NET by kllrnohj on Monday March 05, 2018 @06:59PM (#16525737)
Think of all the places where you need to pass in an explicit type to a Java class to workaround the fact that the types are erased (list.toArray() being the canonical example, but reflection uses hit this a lot as well). That's the downside.

The only benefit to the erasure model is it's easier to implement for the runtime.

Re:How generics were added to .NET by seanmcdirmid on Monday March 05, 2018 @09:00PM (#16526317)
Interop is another advantage of the erasure model. Getting a version of Scala to run with reified generics on the CLR is a huge, perhaps impossible, undertaking. And if you can't do that, interoping with generic C# code becomes really hard. On the other hand, Scala interoping with Java code, generic or otherwise, is much easier.
Re:How generics were added to .NET by matthewwarren on Tuesday March 06, 2018 @04:55AM (#16527900)
I've heard this argument a few times now, it's an interesting point, having 'reified generics' imposes a tax on any language that wants to use that runtime.

Here's a few other examples of the same point, https://twitter.com/jon_cham/status/969929683587432450 and https://twitter.com/headius/status/958371298975080448

Re:How generics were added to .NET by noblethrasher on Monday March 05, 2018 @09:18PM (#16526381)
> Interop is another advantage of the erasure model

And, in the case of F#'s type providers, it means not having to generate zillions of types when coding against some huge schema. But, on the other hand, it does give you the option to do so.

Re:How generics were added to .NET by seanmcdirmid on Monday March 05, 2018 @09:39PM (#16526470)
If the bindings are reference types, the CLR will economize their representations as long as they don't have any static state, I think.

I'm not sure if F# is leveraging the DLR, but there is a lot of fun that can be had there in generating types on the fly, on par with the most dynamic languages out there. It is too bad that it has been nerfed with the UWP/AOT compilation push.

Re:How generics were added to .NET by noblethrasher on Monday March 05, 2018 @10:29PM (#16526658)
> If the bindings are reference types, the CLR will economize their representations as long as they don't have any static state, I think.

Yep, I knew about that, but type providers don't necessarily generate generic types. So, I meant something a bit different.

As you know, If Foo<T> is an ordinary generic, then T must be a CLR type, which is to say that it must be code that was somehow turned into a CLR type (usually by a compiler such as C#). But, if Foo<> is a type provider, then arbitrary code of an arbitrary language can go in between the angle brackets (which is why type providers can accept strings). Essentially, each type provider is a compiler, but the code that is generated must be expressed (or "wrapped", if you will) as a CLR type. But these types need not be generic (and usually aren't, I think). So, not having erased types in F# is akin to having a compiler that can't generate loops or subroutines†.

†(Perhaps a better analogy is a compiler that can't eliminate tail-calls, since erasing everything to a certain base type (that F# lets you choose) is kind of like reusing stack frames vis-à-vis the analogy)

Re:How generics were added to .NET by seanmcdirmid on Monday March 05, 2018 @11:25PM (#16526854)
It is possible to generate interface definitions in C# on the fly (at run-time), but unfortunately it requires going outside of the CLR or even the DLR. Resource usage also becomes a problem since you have to generate it in a new assembly.
Re:How generics were added to .NET by matthewwarren on Tuesday March 06, 2018 @05:00AM (#16527924)
> It is possible to generate interface definitions in C# on the fly (at run-time),

Huh, I never thought about this restriction, so you can't add a new interface with 'Reflection.Emit', you can only add a class that implements an existing Interface, is that right?

> but unfortunately it requires going outside of the CLR or even the DLR.

By this do you mean writing raw CLR metadata yourself and then somehow injecting it into the running process or something else? Either way, its sounds like an interesting technique, any links to how it's done?

Re:How generics were added to .NET by seanmcdirmid on Tuesday March 06, 2018 @10:46AM (#16529498)
Using type builder, you just set the type attributes to interface and define it otherwise like a class. See https://stackoverflow.com/questions/136528/using-nets-reflec...
Re:How generics were added to .NET by matthewwarren on Wednesday March 07, 2018 @05:00AM (#16535362)
Thanks for the link, I see what you mean now
Re:How generics were added to .NET by paulddraper on Monday March 05, 2018 @10:42PM (#16526705)
Scala used to target CLR.

It was abandoned because interop was hard, and interop was Scala's strategy for success

Re:How generics were added to .NET by seanmcdirmid on Monday March 05, 2018 @11:26PM (#16526858)
Scala used erasure to target the CLR.
Re:How generics were added to .NET by paulddraper on Tuesday March 06, 2018 @01:11PM (#16530676)
Yep. And erasure meant that it couldn't interop with generics, which was a big deal for them.
Re:How generics were added to .NET by seanmcdirmid on Tuesday March 06, 2018 @01:20PM (#16530750)
Yes, that is correct.
Re:How generics were added to .NET by KMag on Monday March 05, 2018 @07:44PM (#16525966)
Note also that Java does have reified generics, but only for arrays. The designers of the JVM from early on did realize the advantages of reified generics. Type erasure is not some principled stand being made by the JVM.
Re:How generics were added to .NET by kbsletten on Monday March 05, 2018 @08:20PM (#16526150)
Well, really their hand was forced because they needed to be able to specialize all of the array-of-primative types. It's not as much they saw the advantage as they couldn't imagine a language where you can't express `byte []`. But the point stands that they could have realized that `ArrayList <byte>` is almost as fundamental and created refied generics for user-defined types as well.
Re:How generics were added to .NET by KMag on Tuesday March 06, 2018 @02:25AM (#16527429)
They could have had type erasure for all arrays of non-primitives. Their hand was not forced.
Re:How generics were added to .NET by masklinn on Tuesday March 06, 2018 @06:56AM (#16528272)
> Note also that Java does have reified generics, but only for arrays.

By that count, so does C.

Re:How generics were added to .NET by TheCoelacanth on Tuesday March 06, 2018 @12:10PM (#16530165)
Java has generics but they are only reified for arrays.

C has types that are only generic in the case of arrays.

There is a difference.

In Java I can write this function

    <T> String getTypeOfArray(T[] arr) {
        return arr.getClass().getComponentType().getName();
    }
and that will return the name of type T at run-time. If I instead of T[] I use List<T>, getComponentType will return null and there is no way to access what type T is at run-time. At compile time List<T> and T[] behave the same, but at runtime the List loses what type if contains.

In C I can't write a function with that signature at all because it doesn't support generic functions.

Re:How generics were added to .NET by Sharlin on Tuesday March 06, 2018 @10:35AM (#16529426)
C has no runtime type information at all. Java knows the difference between Integer[] and Float[] at runtime but not between List<Integer> and List<Float>.
Re:How generics were added to .NET by lmm on Tuesday March 06, 2018 @11:42AM (#16529933)
list.toArray() is only a problem in Java because arrays aren't erased. Mixing erasure and non-erasure in the same language is a problem, but it's not a problem with erasure.