Choosing the syntax for variance of a type parameter in C # and VB

In both C # and VB, type parameter modifiers are used to express the variance of type parameters. For example, a C # version looks like this:

interface Foo<in X, out Y> { }

and the VB version looks like this:

Interface Foo(Of In X, Out Y)

End Interface

Since the variance specifications basically limit where and how to use the type parameter, I tend to think of them as additional restrictions on the type parameters.

I'm just wondering why they are not represented like that. I mean, why are they presented in both languages ​​as additional modifiers for type parameters, and are not added to the list of type restrictions? If they were type restrictions, a C # version would look like this:

interface Foo<X, Y> where X:in where Y:out { }

and the VB version would look like this:

Interface Foo(Of X As In, Y As Out)

End Interface

, - , - ?

+3
1

, .

, (X/Y ), .

where X: in Of X As In, X. "" - "T". , :

interface Foo<out X> {}

: " X ", , X , .

, "X ", X.

, , .

+2

Source: https://habr.com/ru/post/1740801/


All Articles