subtyping

Raw types inside of generic definition

此生再无相见时 提交于 2019-12-11 23:28:14
问题 I wonder why the following generic definition does not produce a compiler warning: class MyClass<T extends List> { } and how the above definition is different to class MyClass<T extends List<?>> { } Whenever you read about generics, you read about how raw types should be avoided and consequently, whenever you handle generic types, you get a compiler warning. The raw type inside of the first definition does however not create such a warning. Secondly, I wonder how the exact subtyping

How to create multi-level Error subtypes in Go

Deadly 提交于 2019-12-11 16:47:55
问题 I was trying to create sub-types of errors in GO. I've asked a question previously regarding the matter. Now I'am facing an issue with the multiple types. Following code shows the error type definitions: /* Interfaces */ type UniversalError interface { CommonError1 } type CommonError1 interface { error CommonError1() } /* Structs */ type Error1 struct { reason string } type Error2 struct { reason string } type Error3 struct { reason string } /* Interface function implementations */ func

Confusion over generics subtyping in java specs?

久未见 提交于 2019-12-11 05:14:37
问题 I'm reading java specs https://docs.oracle.com/javase/specs/jls/se10/html/jls-4.html#jls-4.10.2 and this line confuses me: D<U1 θ,...,Uk θ> , where D<U1,...,Uk> is a generic type which is a direct supertype of the generic type C and θ is the substitution [F1:=T1,...,Fn:=Tn]. is Uk θ here casting? and after substitution D becomes D<(U1)T1,...,(Uk)Tk> ? If so why does the author omit the brackets in Uk θ as part of casting syntax? Thanks! 回答1: Given a generic type declaration C<F1,...,Fn> (n >

Wildcard pattern overriding subtype constraint on polymorphic variant

强颜欢笑 提交于 2019-12-10 04:35:20
问题 Given these types type a = [ `A ] type b = [ a | `B | `C ] and this function let pp: [< b] -> string = function | `A -> "A" | `B -> "B" | `C -> "C" applying a value of type a works without issue, as expected: let a: a = `A let _ = pp a However, if the function is modified to include a wildcard pattern let pp: [< b] -> string = function | `A -> "A" | `B -> "B" | _ -> "?" even though everything else remains the same, it now yields the following error (on let _ = pp a ): This expression has type

union types in scala with subtyping: A|B <: A|B|C

本小妞迷上赌 提交于 2019-12-09 18:22:09
问题 I would like to have a A|B type to be the subtype of A|B|C . Is that possible to encode in Scala ? If yes, how ? I was hoping that I can make implicitly[¬¬[IF] <:< T] compile below (original code here), but it does not. Is there a way to fix this code to allow subtyping ? object NUnion{ type ¬¬[A] = ¬[¬[A]] type ¬[A] = A => Nothing trait Disj[T] { type or[S] = Disj[T with ¬[S]] type apply = ¬[T] } // for convenience type disj[T] = { type or[S] = Disj[¬[T]]#or[S] } type T = disj[Int]#or[Float]

TypeScript: subtyping and covariant argument types

懵懂的女人 提交于 2019-12-08 17:40:03
问题 Common sense suggests that subtyping should be covariant with respect to return type but contravariant with respect to argument types. So, the following should be rejected, because of the strictly covariant argument type of E.f : interface C { f (o: C): void } interface D extends C { g (): void // give D an extra service } class E implements C { // implement f with a version which makes stronger assumptions f (o: D): void { o.g() // rely on the extra service promised by D } } // E doesn't

Contravariant method argument type

浪子不回头ぞ 提交于 2019-12-07 09:16:18
问题 wiki Contravariant_method_argument_type says overriding method has the subtyping rule as function type, but no language except one support contravariant argument type. I also not able to come up with any idea of benefit to use that. example: class AnimalShelter { Animal getAnimalForAdoption() { ... } void putAnimal(Animal animal) { ... } } class CatShelter extends AnimalShelter { @Overriding Cat getAnimalForAdoption() { return new Cat(); } @Overriding void putAnimal(Object animal) { … } } My

Does Scala have a value restriction like ML, if not then why?

强颜欢笑 提交于 2019-12-06 21:35:33
Here’s my thoughts on the question. Can anyone confirm, deny, or elaborate? I wrote : Scala doesn’t unify covariant List[A] with a GLB ⊤ assigned to List[Int] , bcz afaics in subtyping “biunification” the direction of assignment matters. Thus None must have type Option[⊥] (i.e. Option[Nothing] ), ditto Nil type List[Nothing] which can’t accept assignment from an Option[Int] or List[Int] respectively. So the value restriction problem originates from directionless unification and global biunification was thought to be undecidable until the recent research linked above. You may wish to view the

Parametric Polymorphism vs Subtype polymorphism F#

笑着哭i 提交于 2019-12-06 06:11:53
What is the difference (if any) between these two F# type signatures? UseTheStream<'a when 'a :> Stream> : 'a -> unit and UseTheStream : (stream : Stream) -> unit Do they mean the same thing in this case? msdn says the following about the (:>) Type Constraint type-parameter :> type -- The provided type must be equal to or derived from the type specified, or, if the type is an interface, the provided type must implement the interface. This would indicate that the two signatures are saying the same thing. So Functionally, how are they different? Daniel They are different. Most importantly, the

Contravariant method argument type

邮差的信 提交于 2019-12-05 15:55:32
wiki Contravariant_method_argument_type says overriding method has the subtyping rule as function type, but no language except one support contravariant argument type. I also not able to come up with any idea of benefit to use that. example: class AnimalShelter { Animal getAnimalForAdoption() { ... } void putAnimal(Animal animal) { ... } } class CatShelter extends AnimalShelter { @Overriding Cat getAnimalForAdoption() { return new Cat(); } @Overriding void putAnimal(Object animal) { … } } My question is: Is contravariant argument type of overriding method any of good use? if yes, where it is?