有没有一种面向对象的静态类型语言,其变量类型很少?

发布于 2024-09-18 22:37:05 字数 387 浏览 4 评论 0原文

我喜欢阅读编程理论,所以你能告诉我是否有任何面向对象的静态类型语言允许变量有几种类型? 伪代码中的示例:

var value: BigInteger | Double | Nil

我考虑在该对象上调用方法的方法。如果对象具有BigInteger | 类型Double 语言可以允许用户仅调用共享方法(lake plus、minus),但当类型为 BigInteger | 时双 | Nil 那么 Nil 的对象没有 plus 和 minus 方法,所以我们不能用这个对象做任何有用的事情,因为它只有很少的共享方法(比如 toString)。

那么,有什么想法应该如何在静态类型面向对象语言中调用具有少量类型的变量的方法吗?

I like reading about programming theories, so could you tell me if there is any object-oriented static typed language that allow variables to have a few types?
Example in pesudocode:

var value: BigInteger | Double | Nil

I think about way of calling methods on this object. If object value have type BigInteger | Double language could allow user to call only shared methods (lake plus, minus) but when the type is BigInteger | Double | Nil then object of Nil hasn't methods plus and minus, so we can't do anything usefull with this object, because it has only few shared methods (like toString).

So is there any idea how should work calling methods on variable with few types in static typed object-oriented language?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

贪了杯 2024-09-25 22:37:05

您所描述的是交叉点类型 。它们Java 中确实存在,例如,但它们仅作为捕获转换和类型推断的结果出现在类型检查器中。你不能自己写一个。

我不知道有任何语言直接使用它们,但它们通常用于描述分析语言类型系统,特别是实际上不的语言>有一个类型系统。例如,Diamondback Ruby 是动态类型 Rub​​y 编程语言的静态类型系统和类型推断器,它使用并集和交集类型。

请注意,您使用的语法通常用于表示联合类型,它们是交集类型的对偶。交叉点类型一般写作A &住宿加早餐旅馆C。

What you are describing is an intersection type. They do exist in Java, for example, but they only arise within the type-checker as the result of capture conversion and type-inference. You cannot write one yourself.

I don't know of any language which uses them directly, but they are often used to describe or analyze type systems of languages, espececially languages which don't actually have a type system. For example, Diamondback Ruby, which is a static type system and type-inferencer for the dynamically typed Ruby programming language, uses both union and intersection types.

Note that the syntax you are using is generally used to denote union types, which are the dual of intersection types. Intersection types are generally written A & B & C.

苯莒 2024-09-25 22:37:05

我不知道有任何语言可以做到这一点......可悲的是,我很想尝试一下(但首先,他们应该采用类型推断和参数多态性;))。

虽然这是可能的:在结构类型系统中相对优雅(如果 a 具有 b 所具有的一切,则类型 a 是类型 b 的子类型),只需指定作为 BigInteger 结构子类型的值的类型和 DoubleNil 以及在主格类型系统中稍微不太优雅(类型 a 是类型 b 的子类型当且仅当它直接或间接继承自类型 b )通过指定所有三个的共同祖先(如果所有其他方法都失败,则为 object)。当然,我们需要递归 - toString 的类型是什么? (Integer | Double | BigInteger).+ 的类型是什么?!?这绝非小事(事实上,寻找解决方案让我有点头疼)。我不能说这是否不可能,但是主要面向对象语言的类型系统在任何地方都没有足够复杂来提供可能的解决方案。

底线是:如果有一些高手出现并解决它提出的问题,那就太酷了。可能不值得付出努力...

编辑:您知道代数数据类型吗?它们与您的想法类似(但更古老;)),因为代数数据类型由多种类型组成,因此可以包含例如 BigInteger、Double 和 Nil - 实际值是其中之一和标签(如标记联合)说的是哪个。但是要使用存储在代数数据类型中的值,您必须使用 模式匹配 安全地提取它。 这个概念非常强大,并且仍然“简单”到足以被工具理解 - 例如类型推断和静态类型检查工作。

I am not aware of any language that does this... sadly, I'd love to play around with it (but first, they should adopt type inference and parametric polymorphism ;) ).

Although it is alreapossible: Relatively elegantly in a structural type system (type a is a subtype of type b if a has everything b has), simply by specifying a type for value that is a structural subtype of BigInteger and of Double and of Nil and slightly less elegantly in a nominative type system (type a is a subtype of type b if and only if it inherits from it, directly or indirectly) by specifying a common ancestor of all three (if all else fails, object). Of course we'd need to go recursive - what is the type of toString? And what's the typ of (Integer | Double | BigInteger).+?!? This is far from trivial (in fact, looking for a solution made my head hurt a bit). I can't say if it is impossible, but no mainly-OO-language's type system is anywhere sophisticated enough for a possible solution.

The bottom line is: It'd be really cool if some whizz came along and sorted out the issues it raises. Propably not worth the effort...

Edit: Do you know algebraic data types? They are similar to your idea (but much older ;) ) in that an algebraic data type is composed of several types and can therefore contain e.g. a BigInteger, a Double and Nil - the actual value is one of these and a tag (as in tagged union) says which. But to use the value stored in an algebraic data type, you have to use pattern matching to extract it safely. This concept is very powerful, and still "simple" enough to be understood tools - e.g. type inference and static typechecking work.

喜你已久 2024-09-25 22:37:05

它与 OO 没有太大关系,但(据我了解)你所描述的看起来很像 C++ 实现的多态性。

It has not much to do with OO but (as far as I understand it) what you describe looks much like polymorphism as implemented by C++.

心病无药医 2024-09-25 22:37:05

是的,OCaml 以多态变体的形式提供这些:

type my_var = Integer of int | Float of float;;
let x = Integer(10);;
let y = Float(3.14);;

Yes, OCaml has these in the form of polymorphic variants:

type my_var = Integer of int | Float of float;;
let x = Integer(10);;
let y = Float(3.14);;
抱猫软卧 2024-09-25 22:37:05

Pike 有它们,Magpie,我正在研究的一种可选类型语言。 Google 的 Javascript 闭包编译器 允许您使用 | 在 Javascript 中注释类型。

它们经常出现在连接静态和动态类型的语言中,因为动态语言中的许多表达式可以产生以下几种类型之一:

var a = 123;
if (foo) { a = "string"; }
bar(a);

传递给 bar() 的静态确定类型是 <代码>号码|字符串。

Pike has them, as does Magpie, an optionally-typed language I'm working on. Google's Closure compiler for Javascript allows you to annotate types in Javascript using |.

They crop up frequently in languages that bridge static and dynamic typing because a lot of expressions in a dynamic language can yield one of a couple of types:

var a = 123;
if (foo) { a = "string"; }
bar(a);

The statically-determined type being passed to bar() is Number | String.

北陌 2024-09-25 22:37:05

我不太确定我们是否真的对静态类型语言有完整的定义,但我也希望您描述的语言不符合静态类型语言的资格。

我担心的一个问题是,如果您添加类型 T1 和 T2 作为 BigInteger | 的一部分。双 | Nil,他们如何了解彼此以及如何处理您定义的操作?现在我意识到您从未说过该语言将允许扩展“隐式”转换定义。

想想看,C# 在其 中做了类似的事情字符串处理

string s = -42 + '+' + "+" + -0.1 / -0.1 + "=" + (7 ^ 5) + 
  " is " + true + " and not " + AddressFamily.Unknown; 

=> “1+1=2 为真且不是未知”

string str = 1 + 2 + "!=" + 1 + 2;

==> “3!=12”

我不喜欢它。

I'm not so sure if we really have a complete definition of what a static typed language is but I also hope that the language you describe wouldn't qualify as one.

One of my concerns is that if you add type T1 and T2 to be a part of your BigInteger | Double | Nil, how would they know about each other and how to handle the operations you defined? Now I realize you never said that the language would allow expanding the "implicit" conversion definition.

Come to think of it, C# does something that resembles this in its string handling

string s = -42 + '+' + "+" + -0.1 / -0.1 + "=" + (7 ^ 5) + 
  " is " + true + " and not " + AddressFamily.Unknown; 

=> "1+1=2 is True and not Unknown"

string str = 1 + 2 + "!=" + 1 + 2;

=> "3!=12"

And I do not like it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文