4 ms·
You can always assume that it's the most restrictive type that you want. How many times did you really need Map<Object, Object>, when you initialised the map wi
by viraptor 17y ago
You can always assume that it's the most restrictive type that you want. How many times did you really need Map<Object, Object>, when you initialised the map with {"abc":1, "def":2, "ghi":3} ?
Since you can always make it specific what you want, you can assume that the most common case is the "default". That's what c#'s `var` does anyways - if you have a `var a=1;`, 'a' an int, not an object. If you use `var a=new []{1,2,3};`, it's int[], not object[].
- ajross 17y agoOr go one better and extend the language to do true type unification. It's not like this isn't well-travelled territory.
- bad_user 17y agoIt's not. How many OOP languages do you know where type-inference works? I know of none ... Ocaml uses a special syntax for reconciling the type-inference with the inclusion polymorphism that it supports ... (name : sub_type :> super_type). And IMHO it gets ugly. I'm not really clear about what "type unification" you're referring to, but the actual types in Java aren't known until runtime (you usually only know a parent). To change this behavior an overhaul of the entire architecture is needed.
- StrawberryFrog 17y agoHow many OOP languages do you know where type-inference works C# Not "type-inference" in the very general sense, just "type-inference" in the "when you see 'var someVar = 3;' you can infer that someVar is of integer type" sense. Which is what java is doing again. Playing catch-up to c#.
- bad_user 17y agoJava is not adding local type inference as in C#. It only adds the diamond construct for inferring the generic types from the variable's type being assigned ... Map<String, Integer> m = new HashMap<>(); Which is ugly, but many Java developers are taking "programming to an interface" rule quite literally.
- p0nce 17y ago> How many OOP languages do you know where type-inference works? D, C++0x
- bad_user 17y agoThose only work for local declarations where the type of the expression is known. This thread was talking about type inference in this case ... var l = [12.4, 13, "a string"]; This would require the array to have generic arguments, and those generics arguments to be inferred from the types given to the constructor (in this case the base class would be Object). I think it works in Scala, but a type unification is really hard ... i.e. to be able to write your own generic functions / classes with the generic type inferred from the base class of the given arguments. The definition of a generic function that would do this would look like ... Array<T> createArray<T> (T ... args) Also, since generic definitions are a PITA to write and read, many people dream of a Hindley-Milner type system where the generic type is discovered. But I don't know of a single OOP language that does that (Ocaml and F# try, but it's ugly) since OOP polymorphism is not compatible.
- azim 17y agoThe difficulty is how you define "most restrictive" in a strongly typed language. If I have classes which implement multiple interfaces which aren't subsets of each other... maybe cloneable and serializable? Which is more restrictive? Does C# solve this?