参数值(而不是类型)的多态性?
是否有一种编程语言(可能是一个概念或研究论文),它允许 函数/方法参数值的多态性?有点:
function factorial(int value > 0){ /* code here */}
function factorial(int value == 0){ /* code here */}
function factorial(int value < 0){ /* code here */}
而且,这种多态性的官方名称(如果有的话)是什么?
Is there a programming language (may be a concept or research paper), which allows
a polymorphism over function/method arguments values? Kind of:
function factorial(int value > 0){ /* code here */}
function factorial(int value == 0){ /* code here */}
function factorial(int value < 0){ /* code here */}
And, what is the official name, if any, for this kind of polymorphism?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜您正在寻找的是模式匹配和/或守卫。例如,Erlang 允许这样做:
前者演示了防护的使用,后者是一个简单的模式匹配,其中第一个参数是
"bar"
、42
或其他任何内容。这两种技术都可以在许多函数式语言中找到。以防万一您不熟悉语法,这相当于(尽可能多地比较):
I guess what you're looking for is pattern matching and/or guards. Erlang for instance allows this:
The former demonstrates the use of guards, the latter is a simple pattern match, where the first argument is either
"bar"
,42
or anything else. Both techniques can be found in many functional languages.Just in case you're not familiar with the syntax, that's equivalent to (as much as it can be compared):
Matthias Blume 在 2006 年发表了一篇名为“Extensible Programming with First-Class Cases”的论文,其中讨论了这样一个系统(基于 ML、IIRC)。
您也许可以使用某些面向方面的语言(例如 AspectJ)做同样的事情,但我还没有尝试过。
此外,在像Scheme这样支持一等函数和绑定到函数的名称突变的语言中,您可以通过包装旧版本来扩展函数:
重新定义函数可以用于调试,但对于“真实代码”来说是不受欢迎的,并且有些模块系统不允许模块导出发生变化。在这种情况下,另一种方法是使用一个包含案例列表的私有可变变量;主函数显式地遍历案例,并且有一个单独的函数用于添加案例。
There's a 2006 paper by Matthias Blume called "Extensible Programming with First-Class Cases" that talks about such a system (based on ML, IIRC).
You might be able to do the same sort of thing with some aspect-oriented languages like AspectJ, but I haven't tried it.
Also, in languages like Scheme that support both first-class functions and mutation of names bound to functions, you can extend a function by wrapping the old version:
Redefining a function is accepted for debugging but frowned upon for "real code", and some module systems don't allow mutation of module exports. In that case, an alternative is to have a private mutable variable containing the list of cases; the main function explicitly goes through the cases, and there is a separate function for adding cases.
模式匹配和防护是实现此目的的一种方法; OCaml、Haskell 和 Scala 也都提供了它们。
Prolog 具有类似的功能:您可以定义依赖于特定值的关系。例如:
在这段代码中,我们定义了一个阶乘关系,当 Y=X! 时,满足阶乘(X,Y) 关系;为此,我们将其专门针对三种情况,一种涉及特定值,另一种涉及范围测试。
是的,Prolog 确实很奇怪。编程包括写下真实的陈述;然后,您可以向系统查询特定语句的真实性或使语句成立的变量赋值的真实性。例如,如果上面的代码保存在
factorial.pl
中:Pattern matching and guards is one way of doing this; OCaml, Haskell, and Scala all provide them as well.
Prolog has a similar feature: you can define relations that depend on specific values. For example:
In this code, we define a
factorial
relation such thatfactorial(X,Y)
is satisfied when Y=X!; to do so, we specialize it over three cases, one involving a specific value and another involving a range test.Yep, Prolog is really weird. Programming consists of writing down true statements; you then query the system for the truth of a particular statement or a variable assignment which makes a statement true. For example, if the above code is saved in
factorial.pl
: