I liked the discussion at Differences in Generics, and was wondering whether there were any languages that used this feature particularly well.
我喜欢泛型中差异的讨论,并且想知道是否有任何语言特别好地使用了这个功能。
I really dislike Java's List<? extends Foo>
for a List
of things that are Liskov-substitutable for Foo
. Why can't List<Foo>
cover that?
我真的不喜欢Java的List
以获取Liskov可替代Foo的事物列表。为什么List
And honestly, Comparable<? super Bar>
?
老实说,可比较 ?
I also can't remember for the life of my why you should never return an Array of generics:
我也记不起为什么你永远不会返回一系列泛型:
public T[] getAll<T>() { ... }
I never liked templates in C++, but that was mostly because none of the compilers could ever spit out a remotely meaningful error message for them. One time I actually did a make realclean && make
17 times to get something to compile; I never did figure out why the 17th time was the charm.
我从不喜欢C ++中的模板,但这主要是因为没有一个编译器会为它们吐出一个远程有意义的错误消息。有一次我真的做了一个make realclean &&做了17次才能得到一些东西来编译;我从来没有弄清楚为什么第17次是魅力。
So, who actually likes using generics in their pet language?
那么,谁真的喜欢在他们的宠物语言中使用泛型?
6 个解决方案
#1
14
Haskell implements type-constructor parameterisation (generics, or parametric polymorphism) quite well. So does Scala (although it needs a bit of hand-holding sometimes).
Haskell很好地实现了类型构造函数参数化(泛型或参数多态)。 Scala也是如此(尽管有时需要一些手持)。
Both of these languages have higher-kinded types (a.k.a. abstract type constructors, or type-constructor polymorphism, or higher-order polymorphism).
这两种语言都具有更高级的类型(a.k.a.抽象类型构造函数,或类型构造函数多态性,或更高阶多态性)。
See here: Generics of a Higher Kind
见这里:更高级别的泛型
#2
7
I think the generics in Java are actually pretty good. The reason why List<Foo>
is different than List<? extends Foo>
is that when Foo
is a subtype of Bar
, List<Foo>
is not a subtype of List<Bar>
. If you could treat a List<Foo>
object as a List<Bar>
, then you could add Bar
objects to it, which could break things. Any reasonable type system will require this. Java lets you get away with treating Foo[]
as a subtype of Bar[]
, but this forces runtime checks, reducing performance. When you return such an array, this makes it difficult for the compiler to know whether to do a runtime check.
我认为Java中的泛型实际上非常好。 List
I have never needed to use the lower bounds (List<? super Foo>
), but I would imagine they might be useful for returning generic values. See covariance and contravariance.
我从来不需要使用下限(List ),但我认为它们可能对返回泛型值很有用。参见协方差和逆变。
On the whole though, I definitely agree with the complaints about overly verbose syntax and confusing error messages. Languages with type inference like OCaml and Haskell will probably make this easier on you, although their error messages can be confusing as well.
总的来说,我绝对同意有关过于冗长的语法和令人困惑的错误消息的抱怨。像OCaml和Haskell这样的类型推断语言可能会让你更容易,尽管他们的错误信息也会让人感到困惑。
#3
7
Heck, English doesn't even implement generics well. :)
哎呀,英语甚至没有很好地实现泛型。 :)
My bias is for C#. Mainly because that is what I am currently using and I have used them to good effect.
我的偏见是C#。主要是因为这是我目前正在使用的,我已经使用它们取得了良好的效果。
#4
3
I'll add OCaml to the list, which has really generic generics. I agree that Haskell's type classes are really well done, but it's a bit different in that Haskell has no OO semantics, but OCaml does support OO.
我将OCaml添加到列表中,该列表具有非常通用的泛型。我同意Haskell的类型类确实做得很好,但它有点不同,因为Haskell没有OO语义,但OCaml确实支持OO。
#5
1
I use .Net (VB.Net), and haven't had any problems using generics. It's mostly painless.
我使用.Net(VB.Net),使用泛型没有任何问题。这主要是无痛的。
Dim Cars as List(Of Car)
Dim Car as Car
For Each Car in Cars
...
Next
Never had any problems using the generic collections, although I haven't gone so far as to design any objects that use generics on my own.
使用泛型集合从来没有遇到任何问题,尽管我还没有设计出任何使用泛型的对象。
#6
0
I think that C# and VB.NET do a good job with generics.
我认为C#和VB.NET在泛型方面做得很好。
#1
14
Haskell implements type-constructor parameterisation (generics, or parametric polymorphism) quite well. So does Scala (although it needs a bit of hand-holding sometimes).
Haskell很好地实现了类型构造函数参数化(泛型或参数多态)。 Scala也是如此(尽管有时需要一些手持)。
Both of these languages have higher-kinded types (a.k.a. abstract type constructors, or type-constructor polymorphism, or higher-order polymorphism).
这两种语言都具有更高级的类型(a.k.a.抽象类型构造函数,或类型构造函数多态性,或更高阶多态性)。
See here: Generics of a Higher Kind
见这里:更高级别的泛型
#2
7
I think the generics in Java are actually pretty good. The reason why List<Foo>
is different than List<? extends Foo>
is that when Foo
is a subtype of Bar
, List<Foo>
is not a subtype of List<Bar>
. If you could treat a List<Foo>
object as a List<Bar>
, then you could add Bar
objects to it, which could break things. Any reasonable type system will require this. Java lets you get away with treating Foo[]
as a subtype of Bar[]
, but this forces runtime checks, reducing performance. When you return such an array, this makes it difficult for the compiler to know whether to do a runtime check.
我认为Java中的泛型实际上非常好。 List
I have never needed to use the lower bounds (List<? super Foo>
), but I would imagine they might be useful for returning generic values. See covariance and contravariance.
我从来不需要使用下限(List ),但我认为它们可能对返回泛型值很有用。参见协方差和逆变。
On the whole though, I definitely agree with the complaints about overly verbose syntax and confusing error messages. Languages with type inference like OCaml and Haskell will probably make this easier on you, although their error messages can be confusing as well.
总的来说,我绝对同意有关过于冗长的语法和令人困惑的错误消息的抱怨。像OCaml和Haskell这样的类型推断语言可能会让你更容易,尽管他们的错误信息也会让人感到困惑。
#3
7
Heck, English doesn't even implement generics well. :)
哎呀,英语甚至没有很好地实现泛型。 :)
My bias is for C#. Mainly because that is what I am currently using and I have used them to good effect.
我的偏见是C#。主要是因为这是我目前正在使用的,我已经使用它们取得了良好的效果。
#4
3
I'll add OCaml to the list, which has really generic generics. I agree that Haskell's type classes are really well done, but it's a bit different in that Haskell has no OO semantics, but OCaml does support OO.
我将OCaml添加到列表中,该列表具有非常通用的泛型。我同意Haskell的类型类确实做得很好,但它有点不同,因为Haskell没有OO语义,但OCaml确实支持OO。
#5
1
I use .Net (VB.Net), and haven't had any problems using generics. It's mostly painless.
我使用.Net(VB.Net),使用泛型没有任何问题。这主要是无痛的。
Dim Cars as List(Of Car)
Dim Car as Car
For Each Car in Cars
...
Next
Never had any problems using the generic collections, although I haven't gone so far as to design any objects that use generics on my own.
使用泛型集合从来没有遇到任何问题,尽管我还没有设计出任何使用泛型的对象。
#6
0
I think that C# and VB.NET do a good job with generics.
我认为C#和VB.NET在泛型方面做得很好。