subtyping

Why does Typescript allow for subtyping?

偶尔善良 提交于 2021-02-05 08:23:46
问题 According to docs, "Type compatibility in TypeScript is based on structural subtyping". So this is possible: type Person: { name: string; } const developer = { name: 'Joe', language: 'Typescript', } // this is ok because Person is a subtype of typeof developer const otherDeveloper: Person = developer; // who writes code like that?! This has many consequences, one of many is that you lose type information when using Object.keys: // "keys" type is array of strings not `name` literal as this

Why do subtypes need to be assigned to a variable before being used as a function argument?

一个人想着一个人 提交于 2021-01-27 18:26:58
问题 I'm learning about subtypes and am wondering why an example given here https://www.typescriptlang.org/docs/handbook/type-compatibility.html compiles, but when I pass the subtype directly as an argument to a function, it does not compile. Here's the original code from typescriptlang.org interface Named { name: string; } let x: Named; // y's inferred type is { name: string; location: string; } let y = { name: "Alice", location: "Seattle" }; function greet(n: Named) { console.log("Hello, " + n

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

半世苍凉 提交于 2020-01-14 03:35:16
问题 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

Parametric Polymorphism vs Subtype polymorphism F#

风格不统一 提交于 2020-01-14 01:44:43
问题 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

Calling any method on Nothing

巧了我就是萌 提交于 2020-01-05 05:49:09
问题 Although it is not explicitly stated that Nothing is the subtype of all types, this (among others) suggests it: fun f(x:Float) { } fun g(x:Char) { } fun dead(q: Nothing) { f(q) g(q) } However, this fails with “unresolved reference”: fun dead(q: Nothing) { q.not() } Is this a bug or a feature? Notes: First piece of code compiles (with warnings), second does not It's possible to use a Nothing typed receiver, for instance calling toString() This is legal: {b:Boolean -> b.not()}(q) Upcast too: (q

Scala UpperBound and LowerBound concept

北城余情 提交于 2019-12-20 14:40:18
问题 Below is the code I am trying to run: class Student { def printDetails = println("I am a student") def printSomeOtherDetails = println("I love Studying") } class ComputerScienceStudent extends Student { override def printDetails = println("I am a Computer Science Student") override def printSomeOtherDetails = println("I love Scala") } class InformationTechnologyStudent extends Student { override def printDetails = println("I am an Information Technology Student") override def

Can we represent “self class” in Java (or Kotlin)? [duplicate]

那年仲夏 提交于 2019-12-12 23:05:31
问题 This question already has an answer here : How to deal with generic bounds migrating from Java to Kotlin? (1 answer) Closed last year . I think the question title is a little bit confusing, but I can't find a more precise way to say this. I just need a simple code example to tell you what I want. I have: // code 1 interface A { A bla(); } class B implements A { @Override public B bla() { return this; } } class C implements A { @Override public C bla() { return this; } } But actually, this

How to define Subtypes in Isabelle and what they mean?

北城余情 提交于 2019-12-12 01:58:36
问题 The question regarding subtyping in Isabelle is very lengthy here. So my simple question is that how I can define type B to be a subtype of A if I define A as below: typedecl A By doing this I would like to make all operations and relations defined over A (they are not printed here) accessible to elements of type B. A bit more complex example is to define B and C to be subtype of A such that B and C are disjoint, and every element of A is either of type B or of type C. Thanks 回答1: Isabelle