Haskell 是否支持面向对象编程 [英] Does Haskell support object oriented programming

查看:27
本文介绍了Haskell 是否支持面向对象编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是否支持声明和实现(Java 中的接口和类)分离等概念?

Does it support concepts like separation of declaration and implementation (interfaces and classes in Java)?

如何限制访问(如 Java 中的访问修饰符)?

How about restricting access (like access modifiers in Java)?

推荐答案

在 Haskell 中如何分离声明和实现?

在 Haskell 中,您可以定义一个 typeclass,它与面向对象的类有很大不同,所以不要不要让这个名字欺骗了你.使用关键字 class,您可以声明函数名称和类型签名,它们可以在其他地方为特定数据类型实例化(实现).

In Haskell you can define a typeclass, which is rather different from an object oriented class so don't let the name fool you. Using the keyword class, you can declare function names and type signatures which can be instantiated (implemented) elsewhere for a particular data type.

例如,Hashable typeclass 定义了hash 函数,它可以将任何实例化的数据类型转换为Int.有一个新的、时髦的数据类型想要散列吗?好的,创建一个 Hashable 的实例.最常见的数据类型由定义 Hashable 的模块实例化(请参阅实例"的链接文档).

For example, the Hashable typeclass defines the hash function, which can turn any instantiated data type into an Int. Have a new, funky data type you want to be able to hash? Fine, make an instance of Hashable. The most common data types are instantiated by the module that defines Hashable (see the linked documentation for 'Instances').

类型类不是定义接口的唯一方法.一种经常被低估的方法是简单的旧数据结构.因为 Haskell 有第一类函数,你可以定义一个数据结构,将函数作为字段:

Typeclasses aren't the only way to define an interface. A method that is often under-rated is a plain old data structure. Because Haskell has first class functions, you can define a data structure that has functions as fields:

data ShuttleInterface =
  SI { launch    :: Delay -> IO Handle
     , deploy    :: Payload -> IO ()
     , getStatus :: IO Status
     }

您的函数可以构建或使用此数据结构:

And your functions can build or consume this data structure:

deployAllSensors :: ShuttleInterface -> IO ()
deployAllSensors shuttle = do
    status <- getStatus shuttle
    let notDeployed = filter (not . deployed) (sensors status)
    when (isOrbiting status) (mapM_ deploySensor notDeployed)

-- we used the well-known Haskell functions: filter, not, , when, mapM_
-- and some supporting functions were assumed:
isOrbitting :: Status -> Bool
deploySensor :: Sensor -> IO ()
sensors :: Status -> [Sensor]
deployed :: Sensor -> Bool

如何限制对 Haskell 中数据的访问?

为了提供抽象,Haskell 使用了代数数据类型.为了保护字段,开发人员声明了一种数据类型,但不导出它的构造函数 - 相反,他们只导出一组维护所需不变量的安全原语.

To provide abstraction, Haskell uses Algebraic Data Types. To protect fields developers declare a data type but don't export it's constructors - instead they only export a set of safe primitives that maintain desired invariants.

例如,地图 模块提供了一个平衡树.如果任何人都可以使用 BranchLeaf 的原语声明一个 Map ,则无法保证平衡,因此制造商没有导出它们.映射的构建必须依赖于从 Data.Map 导出的内容(以及那些由于在同一模块中而可以访问/使用构造函数的内容),例如 fromListemptysingleton 和一大堆修饰符.

For example, the Map module provides a balanced tree. It couldn't guarantee balance if anyone could just declare a Map using the primitives of Branch and Leaf, so the makers didn't export those. Construction of a map must rely on what is exported from Data.Map (and those have access to/use the constructors by virtue of being in the same module) such as fromList, empty, singleton, and a whole bunch of modifiers.

这篇关于Haskell 是否支持面向对象编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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