News for hackers. Stuff that matters.
Re:How generics were added to .NET by krat0sprakhar on Monday March 05, 2018 @06:49PM (#16525671)
> Ultimately, an erasure model of generics would have been adopted, as for Java, since the CLR team would never have pursued a in-the-VM generics design without external help.

Can someone explain why "in-the-VM" generics design is better than the erasure model of Java?

Re:How generics were added to .NET by rusbus on Monday March 05, 2018 @06:58PM (#16525724)
In Java, you can't make runtime decisions based on the generic type of an object. Eg. you can't have a List<?> and check to see if it's a List<Foo> or a List<Bar> -- that information has been erased.

You could look at the head of the list to make that determination, (because the List members still have a concrete type), but that isn't a general purpose solution.

Languages like Scala use manifests to preserve the information but it's a major kludge.

Re:How generics were added to .NET by yarg on Monday March 05, 2018 @07:26PM (#16525875)
Looking at the head of a List does not work. The class of that element is within the scope of <? extends ListType>, an is not necessarily not ListType.
Re:How generics were added to .NET by Groxx on Tuesday March 06, 2018 @01:06AM (#16527192)
To spell this out so it's a bit more obvious, given a Thing at head of a list, you could have:

    - a List<Thing>
    - a List<ThingSuperclass>
    - a List<? extends Thing>
    - a List<? super ThingSubclass>
    - a List<IThing>
    - a List<ILiterallyAnyImplementedIFace>
    - a List<IHaventForgottenInterfacesCanExtendOthers>
    - a List<Object>
    - a List<I'm probably forgetting some possibilities>
And you have no way of knowing which it is.

So yes, it's a boundary, which is significantly more than nothing. But far less useful than it feels like at first.

Re:How generics were added to .NET by yarg on Monday March 05, 2018 @09:50PM (#16526511)
*and is not necessarily ListType.

Idiot.

Re:How generics were added to .NET by Twisol on Tuesday March 06, 2018 @12:24AM (#16527052)
For the downvoters, the author is correcting themselves after the edit window. It's clearly self-deprecating humor, not a personal attack.
Re:How generics were added to .NET by rusanu on Tuesday March 06, 2018 @03:29AM (#16527644)
Not to mention that an empty list still has a type...
Re:How generics were added to .NET by Gibbon1 on Monday March 05, 2018 @07:40PM (#16525936)
One the way to looking at other things(tm) I found a blog post by a CS language researcher talking about type erasure.

His two points.

Being able to do type erasure is great because it means your language is coherent. (Don't need to do any nasty kluges in the run time)

And then everyone ratfucks themselves by actually implementing type erasure, and now your language will never have good tooling and thus will die a lonely unmourned death.

He commented that C/C++ has type erasure and it's also a massive kludge (via the elf format). No one will invest that much effort into a new language.

tl;dr: Type erasure, it's great, don't do it.

Re:How generics were added to .NET by eeperson on Monday March 05, 2018 @08:02PM (#16526055)
What tooling do you think is missing due to erasure?
Re:How generics were added to .NET by Gibbon1 on Tuesday March 06, 2018 @12:40AM (#16527100)
Not a CS person, but I think if you keep the type information pined to the object in memory then a debugger can use the object type information in memory to match it with the object's definition in the program. That makes creating a debugger easy.

Imagine wanting to know what the object at address 0x34199920 is. If you have type info[1] you can cross reference the type info with 'generic array' and then do the same with element 2, address 0x34199920 and know it's a 'foo object'.

[1] Say the first 4 bytes is _always_ a type index that the compiler or interpreter generates.

Re:How generics were added to .NET by cm2187 on Tuesday March 06, 2018 @04:00AM (#16527733)
But then what is the difference between a a collection of type object and a errasure model generic collection?
Re:How generics were added to .NET by masklinn on Tuesday March 06, 2018 @05:27AM (#16528006)
At runtime? None, it's a static (compile-time) feature, though the compiler does add some type assertions & casts automatically.

Note that this is not necessarily an issue for very statically-typed languages e.g. if I remember correctly GHC uses erased generics (they don't exist at runtime) which works fine because Haskell has neither pervasive RTTI nor specialisation, so the type erasure has ~no runtime visibility or impact.

Re:How generics were added to .NET by kelnos on Tuesday March 06, 2018 @04:13AM (#16527770)
At the VM/bytecode level, not much (if anything?). The benefit comes at compile-time, where the compiler can verify that you're putting the correct types into a collection, and that you're expecting the correct types out.
Re:How generics were added to .NET by SideburnsOfDoom on Tuesday March 06, 2018 @04:29AM (#16527833)
> At the VM/bytecode level, not much (if anything?).

I don't think this is correct. In C# code at runtime, and in the bytecode, you can inspect types for metadata about their generic type params. It is present in the .Net bytecode.

See https://docs.microsoft.com/en-us/dotnet/csharp/programming-g...

> When a generic type or method is compiled into (bytecode), it contains metadata that identifies it as having type parameters.

from https://docs.microsoft.com/en-us/dotnet/csharp/programming-g...

Re:How generics were added to .NET by cm2187 on Tuesday March 06, 2018 @04:57AM (#16527909)
But C# generics aren't erasure model collections are they?
Re:How generics were added to .NET by SideburnsOfDoom on Tuesday March 06, 2018 @07:44AM (#16528442)
No, they are not. The point is, it is different at the bytecode level.
Re:How generics were added to .NET by shellac on Tuesday March 06, 2018 @05:29AM (#16528014)
You've got that completely the wrong way around. An erasing compiler has full access to the type (obviously - it's doing the erasing) and so can verify whether you're putting the correct types in or out of a collection.

Where erasure can cause issues is at runtime. For example:

    X isa List<Int>
Even if X is indeed a list of ints that information isn't held at runtime, so the test can't be supported.

Having said that in some respects this is a C# / Java ecumenical matter. Haskell, for example, doesn't bake types into output code so I suppose it erases even more that java, but it supports very rich polymorphic behaviour.

Re:How generics were added to .NET by seanmcdirmid on Monday March 05, 2018 @08:58PM (#16526309)
Performance: you can get rid of all those bridge casts without the need for an inter-procedural analysis. However, since C# also supports structs, generic instances of structs can be (and most of the time are) inlined to realize struct performance benefits.

Ease of programming: your generic type bindings don't disappear at run-time and can be used run-time logic decisions, basically violating Bracha's law (types shouldn't influence run-time behavior), but I found it incredibly useful for meta-programming reasons.

Re:How generics were added to .NET by quickthrower2 on Monday March 05, 2018 @09:50PM (#16526513)
As a Haskell aficionado I can appreciate type erasure and the idea that you don't need to know shit at runtime. However yes when using hybrid languages like C# you have to cheat sometimes. Although anything that uses reflection makes me cringe and there is the practical concern that analysis tools such as find all references etc. are of less use when there is stuff going round and reflecting on shit.
Re:How generics were added to .NET by seanmcdirmid on Monday March 05, 2018 @10:15PM (#16526607)
If you admit dynamic casts/tests in your language, then why go only half way? Such inconsistency leads to bad usability. Haskell is at the other extreme (no dynamic casts) but then it is consistent about it.
Re:How generics were added to .NET by paulddraper on Monday March 05, 2018 @10:36PM (#16526683)
Yeah. People complain about Java type erasure, but the problem is not that they have type ensure, it's that they did it only halfway.
Re:How generics were added to .NET by orclev on Tuesday March 06, 2018 @01:09AM (#16527204)
Yeah, I can see that. Basically you've got two options, go the Haskell way and have zero runtime reflection support and full type erasure, or go the C# way and have full reflection support and no type erasure. Java it's just painful because you get runtime reflection except it's incomplete because half the type info was erased at compile time.
Re:How generics were added to .NET by daxfohl on Tuesday March 06, 2018 @03:44AM (#16527685)
This seems orthogonal though. Just because a runtime uses reified generics, doesn't mean that it has to provide reflection on them.

Is there something special that type erasure adds, that you couldn't have Haskell without it? From a layman's perspective it seems that Haskell could be implemented with either an erased or a reified generics model under the hood, without changing the public surface of the language. But is there something that type erasure enables that reified generics does not?

Re:How generics were added to .NET by masklinn on Tuesday March 06, 2018 @06:53AM (#16528256)
> This seems orthogonal though. Just because a runtime uses reified generics, doesn't mean that it has to provide reflection on them.

It's not completely orthogonal as erased generics are annoying/problematic when the language does provide reflection/RTTI.

So you've got 4 (reification x reflection) states 3 of which are fine:

* if you have erasure and no reflection (Haskell) you're fine: you don't have runtime types but they don't matter/are inaccessible

* if you have reification and reflection (C#, C++/RTTI) you're fine: you can access runtime types and have them

* if you have reification and no reflection (Rust, C++/noRTTI) you're fine: you can specialise & discard types at runtime

* if you have erasure and reflection (Java) you're fucked: you can access types at runtime, but many aren't here anymore

> From a layman's perspective it seems that Haskell could be implemented with either an erased or a reified generics model under the hood, without changing the public surface of the language. But is there something that type erasure enables that reified generics does not?

A simpler implementation.

Re:How generics were added to .NET by lmm on Tuesday March 06, 2018 @11:40AM (#16529918)
Sure you could put reified generics in there, but what would you use them for if you didn't have reflection?
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.
Re:How generics were added to .NET by tehlike on Tuesday March 06, 2018 @02:43AM (#16527499)
2 main things:

- Boxing/unboxing would not be an issue anymore, so you gain performance.

- Runtime types are enforced: List myList = new List<String>(); myList.add(new Object());

In JAVA, this is ok. In c#, it throws.

Re:How generics were added to .NET by eterm on Tuesday March 06, 2018 @05:26AM (#16528002)
Testing this, it doesn't throw in C#, it just doesn't compile because you can't declare a variable to be a List without declaring the type with it. If you specify myList as List<String> then it still doesn't compile.
Re:How generics were added to .NET by tehlike on Tuesday March 06, 2018 @09:31AM (#16529047)
For c#, try compiling with IList instead of list.

IList myList = new List<String>(); myList.add(new Object());

Re:How generics were added to .NET by Sharlin on Tuesday March 06, 2018 @10:39AM (#16529459)
To be fair, raw types like List are really deprecated and only exist in order to interface with legacy APIs.
Re:How generics were added to .NET by tehlike on Tuesday March 06, 2018 @12:04PM (#16530112)
Java? In a lot of cases you still need them. Anything that touches reflections is in that category, plenty of di/ioc systems, or mappers, etc relies on them.
Re:How generics were added to .NET by TheCoelacanth on Tuesday March 06, 2018 @12:17PM (#16530208)
Can you give an example?

Most of the time when reflection is needed I see something like this instead of just a plain list type.

    <T> void foo(List<T> list, Class<? extends T> type) { ... }
Re:How generics were added to .NET by ygra on Tuesday March 06, 2018 @02:57AM (#16527551)
You'd have to call the Java variant via reflection to show the difference at runtime, as the compiler would already enforce that myList.add(new Object()) won't compile. The point stands, though.
Re:How generics were added to .NET by tehlike on Tuesday March 06, 2018 @03:00AM (#16527562)
Reflection is not required. I actually had the code, somehow got deleted

List myList = new ArrayList<String>(); myList.add(new Object());

this would compile.

Re:How generics were added to .NET by ygra on Tuesday March 06, 2018 @03:34AM (#16527654)
Ah, I missed the missing generics on the declaration. Sorry.
Re:How generics were added to .NET by tybit on Tuesday March 06, 2018 @03:59AM (#16527725)
I would say java and c# have both failed on your second point, separate to their respective VMs issues.

It would be nice if mistakes like that were deprecated then eventually fixed, even if over many years/versions.