type-systems

How to understand the two sentences about “Covariance” and “Contravariance”?

耗尽温柔 提交于 2021-02-19 02:33:11
问题 I'm reading the first section of "Scala in depth", there are two sentences in the first section about "covariance" and "contrvariance": Covariance (+T or ? extends T) is when a type can be coerced down the inheritance hierarchy. Contravariance(-T or ? super T) is when a type can be coerced up the inheritance hierarchy. I have read some documents about "Covariance" and "Contravariance", but I can't understand the word "coerced down" and "coerced up" here in this context. 回答1: [TOP / ABSTRACT]

Is Milner let polymorphism a rank 2 feature?

↘锁芯ラ 提交于 2021-02-05 13:10:27
问题 let a = b in c can be thought as a syntactic sugar for (\a -> c) b , but in a typed setting in general it's not the case. For example, in the Milner calculus let a = \x -> x in (a True, a 1) is typable, but seemingly equivalent (\a -> (a True, a 1)) (\x -> x) is not. However, the latter is typable in System F with a rank 2 type for the first lambda. My questions are: Is let polymorphism a rank 2 feature that sneaked secretly in the otherwise rank 1 world of Milner calculus? The purpose of

Can I print in Haskell the type of a polymorphic function as it would become if I passed to it an entity of a concrete type? [duplicate]

喜欢而已 提交于 2020-12-12 05:39:42
问题 This question already has answers here : Unifying c -> a -> b and (a -> b) -> c (3 answers) Closed 35 mins ago . Here's a function polymorphic in 3 types: :t (.) (.) :: (b -> c) -> (a -> b) -> a -> c and here a non polymorphic function: :t Data.Char.digitToInt Data.Char.digitToInt :: Char -> Int If we apply the former to the latter, we get a function polymorphic in 1 type: :t (.) Data.Char.digitToInt (.) Data.Char.digitToInt :: (a -> Char) -> a -> Int which means that (.) was "instantiated"

why can in TypeScript a possible number value in an interface be converted to a not possible number value in a class implementation?

丶灬走出姿态 提交于 2020-07-03 07:15:44
问题 Today I ran into an unexpected TypeScript compiler behaviour. I'm wondering if it's a bug or a feature. Probably it will be the last one, but then I would like to know the rationale behind it. If I declare an interface method with a parameter that can be a string | number , and create a class that implements that interface, then the class method can make that parameter only string . This leads to a situation where the class implementation is not expecting a number, but the compiler allows

Is C# type system sound and decidable?

感情迁移 提交于 2020-03-17 03:53:32
问题 I know that Java's type system is unsound (it fails to type check constructs that are semantically legal) and undecidable (it fails to type check some construct). For instance, if you copy/paste the following snippet in a class and compile it, the compiler will crash with a StackOverflowException (how apt). This is undecidability. static class ListX<T> {} static class C<P> extends ListX<ListX<? super C<C<P>>>> {} ListX<? super C<Byte>> crash = new C<Byte>(); Java uses wildcards with type

Is C# type system sound and decidable?

梦想的初衷 提交于 2020-03-17 03:50:04
问题 I know that Java's type system is unsound (it fails to type check constructs that are semantically legal) and undecidable (it fails to type check some construct). For instance, if you copy/paste the following snippet in a class and compile it, the compiler will crash with a StackOverflowException (how apt). This is undecidability. static class ListX<T> {} static class C<P> extends ListX<ListX<? super C<C<P>>>> {} ListX<? super C<Byte>> crash = new C<Byte>(); Java uses wildcards with type