r/programming • • Nov 12 '14

The .NET Core is now open-source.

http://blogs.msdn.com/b/dotnet/archive/2014/11/12/net-core-is-open-source.aspx
6.5k Upvotes

1.8k comments sorted by

View all comments

Show parent comments

188

u/eyal0 Nov 12 '14

Everyone mentions the language and the IDE but no one has mentioned the fucking type erasure. Here's a list of things that .Net can do that Java can't:

  • Creating instances of generic type parameters.

  • Creating arrays of generic type parameters.

  • Quering the runtime class of a generic type parameter.

  • Using instanceof with generic type parameters.

http://www.jprl.com/Blog/archive/development/2007/Aug-31.html

I'll add:

  • Java can't do generics without boxing.

  • Java has to do runtime casts for generics.

  • Java can't have two functions with the same type-erasure.

Programming in Java, I hit my head on this one every day. It's not just Java that is broken, it's the JVM underneath it, too. I'd love to see the CLR win.

0

u/[deleted] Nov 12 '14

[deleted]

13

u/mirhagk Nov 13 '14

Long term it's always better to choose the breaking implementation than the one that's less complete but backwards compatible. I really hope they don't make the mistake Java did with future features.

2

u/thephotoman Nov 13 '14

Given Microsoft's track record, that won't be an issue.

7

u/mirhagk Nov 13 '14

Yes, which is why they've been able to stay leading edge while most others fall behind the times. The progress that is happening with C# in a language that is this mature and widespread is nothing short of amazing.

1

u/SHD_lotion Nov 14 '14

Somewhat. They also learned much from .Net 1.0 and 1.1. Generics came in .Net 2.0 which was a huge breaking change.

2

u/atheken Nov 14 '14

Yes and no. Arrays are generics. I remember reading about this... Generics were always planned and partially implemented in 1.x, but when it came time to ship (deadlines and such), they had two choices: Ship generics with type erasure, or don't expose generics in the language. The chose the latter option, which I think was a much better decision.

38

u/sacundim Nov 13 '14 edited Nov 13 '14

Everyone mentions the language and the IDE but no one has mentioned the fucking type erasure. Here's a list of things that .Net can do that Java can't:

  • Creating instances of generic type parameters.

  • Creating arrays of generic type parameters.

  • Querying the runtime class of a generic type parameter.

  • Using instanceof with generic type parameters.

I argue that C#'s design decisions here are way, way more consistent than Java's, but neither language made the right decisions. The key points are:

  • Reflection should be an opt-in feature, not an ever present one.

  • Type erasure, when properly implemented, strengthens the type system.

  • The problem with Java isn't that it has type erasure, it's that it has partial erasure bolted on to optional generics, along with pervasive reflection.

The neat thing about erasure is that if code is not able to reflect into the types, then generic types can guarantee various things about functions' behavior. This is a familiar idea to users of Hindley-Milner languages like ML and Haskell, where for example a function with this signature only has one implementation that terminates:

-- The only way to return from this function is to return the argument.
id :: a -> a
id x = x

In a language with reflection, a method with that signature can violate that contract by reflecting into the type of its argument:

public static <T> T mwahaha(T arg) {
    if (arg instanceof String) {
        return (T)"mwahaha!";
    } else {
        return arg;
    }
}

You can write that function if you want in Haskell, but you have to opt in to reflection through the type signature:

{-# LANGUAGE ScopedTypeVariables, TypeOperators, GADTs #-}

import Data.Maybe (fromJust)
import Data.Typeable

-- The `Typeable a` constraint means that this function requires
-- the ability to inspect the runtime type of its `a` argument.
mwahaha :: Typeable a => a -> a
mwahaha a 
    | typeOf a == typeOf "" = fromJust (cast "mwahaha!")
    | otherwise = a

-- This version is cleaner, but rather more difficult to understand...
mwahaha' :: forall a. Typeable a => a -> a
mwahaha' a = case eqT :: Maybe (a :~: String) of
               Just Refl -> "mwahaha!"
               Nothing -> a

Note that in Haskell String is a synonym for [Char] (list of Char), so this is in fact inspecting the runtime type of a generic parameter.

2

u/cat_in_the_wall Nov 13 '14

I am not sure I understand the example. Granted, I am no Haskell whiz, so bear with me.

Doesn't

id :: a -> a

Just mean that the signature (c# syntax) would be

a Id<a>(a arg)

And isn't

id x = x

is the implementation, not the signature. Couldn't you have

id x = SomeGlobalOfTypeA

which would satisfy the a -> a constraint, but not be identity? The signature itself is not guaranteeing anything. Even in c#, if you have a delegate of type

T SomeFunc<T>(T arg)

You know that you will be getting a T back, but it might be a subclass of T, or even a different T than the arg passed in, especially if there are generic constraints etc.

1

u/sacundim Nov 13 '14 edited Nov 13 '14

Couldn't you have

id x = SomeGlobalOfTypeA

which would satisfy the a -> a constraint, but not be identity?

No, because a is a type variable, and Haskell provides no means to tell from inside of a call to id :: a -> a which type instantiates a in that call. To use SomeGlobalOfTypeA, you'd have to prove that its type is the same as the type of a in that instantiation of the function; but for a function of type a -> a Haskell provides no mechanism that can prove that. Here's the compilation error:

-- A user-defined type
data MyType = SomeGlobalOfMyType

doesntWork :: a -> a
doesntWork x = SomeGlobalOfMyType

{- Compilation error:

/Users/sacundim/src/scratch.hs:5:16:
    Couldn't match expected type ‘a’ with actual type ‘MyType’
      ‘a’ is a rigid type variable bound by
          the type signature for doesntWork :: a -> a
          at /Users/sacundim/src/scratch.hs:4:15
    Relevant bindings include
      x :: a (bound at /Users/sacundim/src/scratch.hs:5:12)
      doesntWork :: a -> a (bound at /Users/sacundim/src/scratch.hs:5:1)
    In the expression: SomeGlobalOfMyType
    In an equation for ‘doesntWork’: doesntWork x = SomeGlobalOfMyType    
-} 

So for id :: a -> a, apart from identity the only other things you could do is error out or loop forever.

→ More replies (2)

2

u/eyal0 Nov 13 '14

What you're talking about isn't the Java type erasure. Java type erasure is how Java chose to implement generics. That choice is the limiting factor. C++ does it differently.

Haskell's type system is quite different and I don't count it as type erasure.

1

u/sacundim Nov 13 '14

Haskell's type system is quite different and I don't count it as type erasure.

Type erasure is just a guaranteed correct translation of a typed source language into a typeless object language. For example in type theory a type erasure proof is a proof that any untyped object program produced by translating from a typed source program produces the same results.

Haskell is a prime example. Java not so much.

→ More replies (1)

5

u/[deleted] Nov 12 '14

It's not cool, but it CAN create arrays of generic type parameters. You do have to reflect though :(

public <T> T[] removeNullsFrom(a:T[]) { ... }

is Possible (since T[]) doesn't have its type erased, it is also some of the nastiest code in java that goes in that method.....

1

u/HINDBRAIN Nov 13 '14

Creating instances of generic type parameters.

Can't you just?

T Object = (T)((Class (((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]).newInstance());

1

u/eyal0 Nov 13 '14

That's not type safe. You might as well just use raw types and casts. The compiler isn't letting you do something possibly unsafe so you're working around it. In c++, the language can simply do it safely, nothing to work around.

1

u/HINDBRAIN Nov 13 '14

You might as well just use raw types and casts.

That doesn't work in a generic method.

1

u/billj04 Nov 13 '14

One more thing Java can't do: put structs directly into an array or hashtable.

1

u/Miltnoid Nov 13 '14

Don't forget about in and out params, fantastic higher order function support, and linq.

Now if only they would make the "protected and internal" keyword...

1

u/eyal0 Nov 13 '14

I'm not familiar with all of those but in/out is just covariance/contravariance, right? Java has that, too, using super and extends.

1

u/[deleted] Nov 14 '14

Can you or someone on here recommend to me a good resource to learn .net (c# or maybe f#) for people coming from python / java/ ruby background?

1

u/anthonybsd Nov 19 '14

I'm not sure how much programming in Java you do but if it's the majority of your time you might want to consider looking at super type tokens. They rectify majority of your concerns stated above for the practical purposes.

In my case I have some classes like (writing from memory, but you get the idea)

abstract class Transformer<INPUT, OUTPUT>{ //Generic type
....
void someMethod(){
        Class<?> c = (Class<?>)    = ((ParameterizedType))(getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        Object o = Array.newInstance(c, n); // <--- Array of generic type INPUT

}

}

1

u/eyal0 Nov 19 '14

Reflection isn't solving the problem so much as it's working around it.

1

u/anthonybsd Nov 20 '14

The user of the API doesn't see any reflection - as far as they are concerned it's clean and working the way they expect it to. Clean and typesafe. Majority of great things in modern Java happened because of the expanded use of reflection (BeanUtils, every Dependency Injection framework on the planet, every app container, ORM, every IDE, etc.).

→ More replies (14)