开发者

What are the benefits of using Objects in a functional programming language?

What would be a valid reason(s) to use ob开发者_Go百科jects in functional programming languages? I see that f# is a functional programming language that lends heavily on the object side when dealing with the .net ecosystem of classes. But apart from this interaction with other assemblies/programs maybe written in c#, why would anyone choose to use objects for decomposition of a program in a function oriented language or style?

Does mixing up a style of program prove a help or a hindrance?


Functional and object oriented designs make different types of extensions easy. Given a discriminated union in a functional setting, it's easy to define any number of functions which work on that type, but it's hard to add additional cases to the type, since that would require going back to add the additional case to each function which pattern matches on the type. On the other hand, given a base type (or interface) an OO setting, it's easy to add new subtypes, but adding new operations to the base type is hard, since it potentially requires modifying existing subtypes to add an implementation of the new operation to each one.

Depending on the type of extensibility that's most relevant to the task at hand, either the functional or the object oriented approach may make more sense, so it's nice to have both options available. One popular approach is to use a functional approach "in the small" and an OO approach "in the large" (e.g. it's mentioned in this podcast with Luke Hoban).


Objects provide encapsulation that makes large-scale programming easier.

What would be a valid reason(s) to use objects in functional programming languages?

Objects are used in functional languages for a variety of reasons:

  • Large-scale structuring of programs.

  • Interoperability with existing OOP code (e.g. on the JVM or CLR).

  • Problems for which the ability to extend class hierarchies with more classes is a natural fit.

Does mixing up a style of program prove a help or a hindrance?

Can be a help but it would not be a good idea to do it without good reason.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜