structural-typing

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

Does Dotty support refinements?

狂风中的少年 提交于 2021-01-28 03:13:30
问题 I am reading in dread what will come with Scala 3, paying particular attention to changes to compound types. They were always somewhat of a hack, so clean, true intersection types are certainly an improvement. I couldn't find though anything about what happens to the actual refinement part of the compound type. I rely heavily in my current project on strongly interwoven types in an attempt to have every returned value be as narrow as possible. So, for example, having trait Thing { thisThing =

Confusion about type refinement syntax

試著忘記壹切 提交于 2020-11-29 10:24:27
问题 On Type Level, i stumble upon the following: sealed abstract class StSource[A] { type S def init: S // create the initial state def emit(s: S): (A, S) // emit a value, and update state } object StSource { type Aux[A, S0] = StSource[A] {type S = S0} def apply[A, S0](i: S0)(f: S0 => (A, S0)): Aux[A, S0] = new StSource[A] { type S = S0 def init = i def emit(s: S0) = f(s) } } The line that intrigued me is type Aux[A, S0] = StSource[A] {type S = S0} In paerticular {type S = S0} in StSource[A]

Confusion about type refinement syntax

大憨熊 提交于 2020-11-29 10:24:00
问题 On Type Level, i stumble upon the following: sealed abstract class StSource[A] { type S def init: S // create the initial state def emit(s: S): (A, S) // emit a value, and update state } object StSource { type Aux[A, S0] = StSource[A] {type S = S0} def apply[A, S0](i: S0)(f: S0 => (A, S0)): Aux[A, S0] = new StSource[A] { type S = S0 def init = i def emit(s: S0) = f(s) } } The line that intrigued me is type Aux[A, S0] = StSource[A] {type S = S0} In paerticular {type S = S0} in StSource[A]

Confusion about type refinement syntax

本小妞迷上赌 提交于 2020-11-29 10:23:13
问题 On Type Level, i stumble upon the following: sealed abstract class StSource[A] { type S def init: S // create the initial state def emit(s: S): (A, S) // emit a value, and update state } object StSource { type Aux[A, S0] = StSource[A] {type S = S0} def apply[A, S0](i: S0)(f: S0 => (A, S0)): Aux[A, S0] = new StSource[A] { type S = S0 def init = i def emit(s: S0) = f(s) } } The line that intrigued me is type Aux[A, S0] = StSource[A] {type S = S0} In paerticular {type S = S0} in StSource[A]

Getting java.lang.NoSuchMethodException in structural typing of AnyVal

限于喜欢 提交于 2020-05-23 10:37:33
问题 I have the following snippet that (I think) defines a method addNumber1(x:T):T on a generic type T which is a subtype of AnyVal and has a method +(s:Int):T . def addNumber1[T <: AnyVal {def +(s:Int):T}](x:T):T = {x + 1} addNumber1(31) // compiles but throws exception java.lang.NoSuchMethodException: java.lang.Integer.$plus(int) at java.lang.Class.getMethod(Class.java:1786) at .reflMethod$Method1(<console>:8) at .addNumber1(<console>:8) ... 33 elided I tried adding import scala.language

Result type in structural refinement may not refer to a user-defined value class

徘徊边缘 提交于 2019-12-22 05:54:22
问题 When I define Wrapper as value class(extending AnyVal): class Wrapper(val string: String) extends AnyVal def wrapperHolder(w: Wrapper): {def wrapper: Wrapper} = new { def wrapper: Wrapper = w } I have following compile error for wrapperHolder: Error:(5, 22) Result type in structural refinement may not refer to a user-defined value class def wrapper: Wrapper = w Why it doesn't work for value class? 来源: https://stackoverflow.com/questions/52678323/result-type-in-structural-refinement-may-not

Why doesn't type inference work here?

独自空忆成欢 提交于 2019-12-22 05:22:01
问题 This problem arose in a module I'm writing, but I have made a minimal case that exhibits the same behaviour. class Minimal[T](x : T) { def doSomething = x } object Sugar { type S[T] = { def doSomething : T } def apply[T, X <: S[T]] (x: X) = x.doSomething } object Error { val a = new Minimal(4) Sugar(a) // error: inferred [Nothing, Minimal[Int]] does not fit the bounds of apply Sugar[Int, Minimal[Int]](a) // works as expected } The problem is that the compiler manages to figure out the inner

Why doesn't type inference work here?

╄→гoц情女王★ 提交于 2019-12-22 05:21:39
问题 This problem arose in a module I'm writing, but I have made a minimal case that exhibits the same behaviour. class Minimal[T](x : T) { def doSomething = x } object Sugar { type S[T] = { def doSomething : T } def apply[T, X <: S[T]] (x: X) = x.doSomething } object Error { val a = new Minimal(4) Sugar(a) // error: inferred [Nothing, Minimal[Int]] does not fit the bounds of apply Sugar[Int, Minimal[Int]](a) // works as expected } The problem is that the compiler manages to figure out the inner

Weird nested structural type in generics

爱⌒轻易说出口 提交于 2019-12-21 20:36:28
问题 Can someone explain weird construction of structural type nested in generics: implicit def Function1Functor[R]: Functor[({type λ[α]=(R) => α})#λ] = new Functor[({type λ[α]=(R) => α})#λ] .... This example comes from Scalaz library: Functor.scala Why this construction is needed there? Wouldn't be simpler to write: implicit def Function1Functor[R,A]: Functor[R =>A] or implicit def Function1Functor[R,A]: Functor[Function1[R,A]] 回答1: The signature of the Functor type constructor shows that it is