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

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

问题描述

它支持像声明和实现(Java中的接口和类)分离这样的概念吗?如何限制访问(如访问Java中的修饰符)?

谢谢

解决方案

在Haskell中,你如何区分声明和实现? 在Haskell中,你可以定义一个typeclass ,这与面向对象的类非常不同,所以不要让名字欺骗你。使用关键字 class ,您可以声明函数名称和类型签名,这些签名可以在其他地方为特定数据类型实例化(实现)。



例如, Hashable typeclass定义了 hash 函数,它可以将任何实例化的数据类型转换为 Int 。有一个新的,时髦的数据类型,你想能够哈希?好吧,做一个Hashable的实例。最常见的数据类型由定义 Hashable 的模块实例化(请参阅实例的链接文档)。



<类型类不是定义接口的唯一方法。经常被低估的方法是普通的旧数据结构。由于Haskell具有头等功能,因此可以定义一个具有字段功能的数据结构:

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

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

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

- 我们使用了众所周知的Haskell函数:filter,not,when,mapM_
- 以及一些支持函数:
isOrbitting :: Status - > Bool
deploySensor :: Sensor - > IO()
sensors :: Status - > [传感器]
已部署::传感器 - > Bool

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



为了提供抽象,Haskell使用代数数据类型。为了保护字段开发者声明一个数据类型,但不要导出它的构造函数 - 相反,它们只导出一组保持期望不变量的安全基元。

例如,

a href =http://hackage.haskell.org/packages/archive/containers/0.4.0.0/doc/html/Data-Map.html#g:1> Map 模块提供了一个平衡树。如果任何人都可以使用 Branch Leaf 的原语来声明Map,那么它不能保证平衡,所以制造商没有出口这些。构建映射必须依赖于从Data.Map导出的内容(以及由于处于相同模块而可以访问/使用构造函数),例如 fromList singleton 和一大堆修饰符。


Does it support concepts like separation of declaration and implementation (interfaces and classes in Java)? How about restricting access (like access modifiers in Java)?

Thanks

解决方案

How do you separate declaration and implementation in Haskell?

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.

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').

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

How do you restrict access to data in 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.

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天全站免登陆