News for hackers. Stuff that matters.
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.