本文介绍了在函数式编程中实现多态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在享受从面向对象语言到功能语言的转变。这是一股清新的空气,我发现自己比以前更加富有成效。然而,有一个OOP的方面我还没有看到在FP方面令人满意的答案,那就是多态性。即我有大量的数据项,当它们被传递到某些功能时需要以完全不同的方式处理。为了争论起见,假设有多种因素促成多态行为,所以可能呈指数级地多种不同的行为组合。



在OOP中,可以使用多态性进行相对较好的处理:无论是通过组合+继承还是基于原型的方法。

在FP中,我有点卡住了:


  • 通过分支每个数据项的值来编写或编写有效实现多态行为的纯函数 - 感觉就像组装一个巨大的条件表,甚至模拟一个虚拟方法表!

  • 将函数放在纯数据结构中以类似原型的方式运行 - 这看起来很有效,但它不违反定义与数据分开的纯函数的想法吗?



对于这种情况,推荐的功能方法是什么?还有其他不错的选择吗?

解决方案

如果virtual method dispatch是你想要解决问题的方式,这是一个完全合理的方法。至于从数据中分离功能,这是一个明显不起作用的概念。我认为函数式编程的基本原理是函数是数据。至于你对模拟虚拟功能的感觉,我会认为它根本不是一种模拟。它是一个虚函数表,并且完全没问题。



仅仅因为语言没有内置OOP支持,并不意味着应用相同的设计原则 - 这意味着您必须编写更多其他语言提供的内置机器,因为您正在与您使用的语言的自然精神作斗争。现代类型化函数式语言确实对多态性有很深的支持,但它是一种非常不同的多态性方法。



OOP中的多态性很像逻辑中的存在量化多态值具有SOME运行时类型,但您不知道它是什么。在许多函数式编程语言中,多态性更像通用量化 - 多态值可以实例化为其用户需要的任何兼容类型。它们是同一枚硬币的两面(特别是,它们交换位置取决于您是从内部还是外部)查看功能,但是在设计一个硬币时它变得非常困难语言来使硬币公平,尤其是在存在其他语言特征(例如子类型或更高主观多态性)(多态类型上的多态性)时。

如果它有帮助,你可能想把函数式语言中的多态性看作非常像C#或Java中的泛型的东西,因为这正是ML和Haskell所喜欢的多态性类型。


I'm currently enjoying the transition from an object oriented language to a functional language. It's a breath of fresh air, and I'm finding myself much more productive than before.

However - there is one aspect of OOP that I've not yet seen a satisfactory answer for on the FP side, and that is polymorphism. i.e. I have a large collection of data items, which need to be processed in quite different ways when they are passed into certain functions. For the sake of argument, let's say that there are multiple factors driving polymorphic behaviour so potentially exponentially many different behaviour combinations.

In OOP that can be handled relatively well using polymorphism: either through composition+inheritance or a prototype-based approach.

In FP I'm a bit stuck between:

  • Writing or composing pure functions that effectively implement polymorphic behaviours by branching on the value of each data item - feels rather like assembling a huge conditional or even simulating a virtual method table!
  • Putting functions inside pure data structures in a prototype-like fashion - this seems like it works but doesn't it also violate the idea of defining pure functions separately from data?

What are the recommended functional approaches for this kind of situation? Are there other good alternatives?

解决方案

If virtual method dispatch is the way you want to approach the problem, this is a perfectly reasonable approach. As for separating functions from data, that is a distinctly non-functional notion to begin with. I consider the fundamental principle of functional programming to be that functions ARE data. And as for your feeling that you're simulating a virtual function, I would argue that it's not a simulation at all. It IS a virtual function table, and that's perfectly OK.

Just because the language doesn't have OOP support built in doesn't mean it's not reasonable to apply the same design principles - it just means you'll have to write more of the machinery that other languages provide built-in, because you're fighting against the natural spirit of the language you're using. Modern typed functional languages do have very deep support for polymorphism, but it's a very different approach to polymorphism.

Polymorphism in OOP is a lot like "existential quantification" in logic - a polymorphic value has SOME run-time type but you don't know what it is. In many functional programming languages, polymorphism is more like "universal quantification" - a polymorphic value can be instantiated to ANY compatible type its user wants. They're two sides of the exact same coin (in particular, they swap places depending on whether you're looking at a function from the "inside" or the "outside"), but it turns out to be extremely hard when designing a language to "make the coin fair", especially in the presence of other language features such as subtyping or higher-kinded polymorphism (polymorphism over polymorphic types).

If it helps, you may want to think of polymorphism in functional languages as something very much like "generics" in C# or Java, because that's exactly the type of polymorphism that, e.g., ML and Haskell, favor.

这篇关于在函数式编程中实现多态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 13:38
查看更多