dart缺少静态类型语义的一些好的解决方法是什么? [英] What are some good workarounds for dart's lack of static typing semantics?

查看:70
本文介绍了dart缺少静态类型语义的一些好的解决方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自C#/ F#/ Haskell,所以我想为我以前使用类型解决的编程问题提供解决方案。

I'm coming from C#/F#/Haskell so I'm trying to come up with solutions to programming problems I'm used to solving with types.


  1. A类,其中T:C#中的new(),这主要是因为我可以在某个地方进行new T()。这会在Dart中创建格式错误的类型错误。是否有合理的惯用方式解决此问题?也许是在工厂?

  1. class A where T : new() in C#, this is mainly so I can do new T() somewhere. This creates a malformed type error in Dart. Is there a reasonably idiomatic way to solve this? Perhaps with factories?

我对mixin进行了一些实验,如果继承的mixin成员的名称发生冲突,则最后一个mixin获胜。因此对于以下内容:
抽象类mixA {void foo(); }
抽象类mixB {void foo(); }
C类使用mixA,mixB扩展了对象{}
new C()。foo();

I did some experiments with mixins, in the case of name conflicts for inherited mixin members, the last mixin wins. So for the following: abstract class mixA { void foo(); } abstract class mixB { void foo(); } class C extends Object with mixA, mixB {} new C().foo();

这将最终调用mixB.foo(),而
类C会使用mixB扩展Object,mixA {}
最终将调用mixA.foo()
要访问隐藏的mixins的成员?

this would end up calling mixB.foo() whereas class C extends Object with mixB, mixA {} would end up calling mixA.foo() Is there anyway to access the members of the hidden mixins?

假设我将2个mixins与同名字段混合在一起。子类实例在运行时是否具有2个字段(仅1个是不可访问的),或者该对象像字典一样,每个名称只有1个插槽?

Suppose I mix 2 mixins with a field of the same name. Does the subclass instance have 2 fields at runtime (just 1 is inaccessible) or is the object like a dictionary and there is only 1 slot for each name?

推荐答案

1是不可能的。您不能使用通用类型(或与此相关的变量)调用 new 。最常见的解决方法是创建一个分配对象的闭包。

1 is not possible. You can't call new on a generic type (or variable for that matter). The most common workaround is to create a closure that allocates the object instead.

对于2的答案是因为Mixins可以被视为超类: A用B扩展对象,C 基本上等同于:

The answers for 2 fall out of the fact that Mixins can be seen as super-classes: A extends Object with B, C is basically equivalent to:

class B' extends Object { <copy over code inside B> }
class C' extends B' { <copy over code inside C> }
class D extends C' { ... }

考虑到这一点:


  • 不。无法访问隐藏的超级元素。

  • 是。

小注:<复制X内的代码> 部分并不完全正确,因为那会改变库范围。从概念上讲,mixin B中的代码在声明mixin的库中。

Small note: the <copy over code inside X> part is not completely correct since that would change the library-scope. Code from mixin B is conceptually in the library the mixin is declared in.

这篇关于dart缺少静态类型语义的一些好的解决方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆