问题标签 [structural-typing]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
127 浏览

scala - 作为结构类型成员的值类

这是简化的代码:

错误:结构细化中的结果类型可能未引用用户定义的值类

val y = 新 { val f: 值 = v }

我不明白。既然 Int 也是一个值类,为什么 Int 可以用作成员,但我的值类 Value 不能?为什么 scala 定义了这个限制?如何在结构类型中使用用户定义的值 vlass?

0 投票
2 回答
166 浏览

scala - 什么是scala中的结构匹配

关于scala中结构匹配的几个问题

问题 1)在下面的代码中,我是否能够传递BirdPlane到,因为takeOff在结构上匹配起飞所需的对象?BirdPlaner

问题 2) 什么是反射调用?我必须导入,scala.language.reflectiveCalls否则我会收到警告reflective access of structural type member value callsign should be enabled by making the implicit value scala.language.reflectiveCalls visible.

问题 3)如何创建Bird如下: val bird = new Bird("Polly the parrot"){ val callsign = name }. 不应该只是val bird = new Bird("Polly the parrot")。这是怎么编译的。

问题 3.1)。bird仍然是类型还是Bird现在是其他类型,因为我已经通过了额外的{...}

4) in 的类型是r什么takeOff

0 投票
0 回答
33 浏览

typescript - TypeScript 中的 `{}` 类型和 `object` 类型有什么区别?

有什么区别:

我找不到两者不同的情况。例如,即使null是一个对象(大写的“O”),以下任何一行都不会进行类型检查:

0 投票
0 回答
106 浏览

elm - Is there a way to specify partial custom types in type annotations?

Is there a way to specify partial custom types in type annotations?

For instance, if I have my type aliases this way: func : { a | foo : String } can I have this function:

and I would like to do something like this:

where we know msg has at least NoOp value

0 投票
2 回答
83 浏览

scala - 基于方法定义泛型类型(结构类型)

我正在尝试声明一个基于某些函数的类型的类。

我希望 readC 应该与 A 和 B 一起使用。我也不能对 A 和 B 类进行更改,因此没有基于特征的解决方案可以在这里工作。

另外,有没有更好的方法呢?

0 投票
1 回答
118 浏览

typescript - TypeScript 什么时候使用结构类型?

鉴于下面的类型,为什么编译器允许下面的赋值?我猜它与在某些情况下使用结构类型的 TypeScript 有关(即,因为SuccessFailure结构上等价,编译器将它们视为可互换的),但我想我不清楚在什么条件下使用结构类型。

好奇的额外背景: 正如 Malvolio 指出的那样,问题在于类型联合是可交换的,解决这个问题的方法是使类型在结构上不等效。Malavolio 的解决方案是为类型赋予不同的字段(即svaluefvalue)。这可行,但我更喜欢让界面相同,所以我选择了下面的解决方案,使用符号来区分类(如果这有与之相关的问题,请加入):

0 投票
1 回答
112 浏览

typescript - TypeScript 泛型与 Java 有何不同?

以下代码会在 Java 中产生预期的类型错误,但 TypeScript 并不认为这是错误。这是设计使然,还是 TypeScript 中的错误?

事实上,TypeScript 甚至不认为这是一个错误:

TypeScript 是否将所有类视为结构类型而不是名义类型?我习惯于 Java 和 Flow 将类视为名义类型。

0 投票
1 回答
72 浏览

scala - 使用 scala.tools.reflect.ToolBox 进行类型检查后检查类型的相等性

我想确定由语法树表示的表达式是否tree1属于tree2同一类型。我尝试使用来自的类型检查方法这样做scala.tools.reflect.ToolBox,但它似乎与实际的 Scala 类型不一致。

在这里我创建了工具箱:

它报告类型不等式:

我要求类型的表示:

现在这些类型成功统一了:

我可以以某种方式检查tree1tree2表示与上一个片段中类似类型的表达式吗?

编辑:最小定义

0 投票
1 回答
80 浏览

scala - Scala中的方法调用反射会导致性能下降吗?

我有一个方法打算对数据 IO 做一些简单的统计,如下所示。

因为我希望它能够处理具有{ def length: Int }方法的不同数据类型,所以我将其设为通用。
问题是,这个方法调用使用了反射。
由于这种方法被调用了数百万次,我不希望它有性能问题。
我知道使用反射进行类实例化会降低性能,但是这个方法调用怎么样?

(关于该方法的另一个问题是,它不能适应具有 方法的类型,有{ def length: Long }什么建议来处理这个问题吗?)

0 投票
1 回答
52 浏览

typescript - TypeScript generics: how to define type T which is structurally the same as other type S

I have a notion of a Step, which requires value of type A as an input and gives out a value of type B.

Now I would like to compose two steps, so I end up with something like this inside class Step:

What I would like to achieve is to somehow tell the type system that we can pass type B to a function which expects type C (structural typing should check that all fields in C are present in B), so that the line return nextStep.run(b) works fine.

Example:

As you can see stepB requires as an input {a: number}, so it can be fed an output from stepA which is {a: number, b: string}. But I cannot figure out how to define the relation in andThen. Any thoughts how this can be achieved?