我应该使用typeclasses或不? [英] Should I use typeclasses or not?
问题描述
我在使用时以及在代码中不使用typeclass时遇到一些困难。当然,我的意思是创建自己的,而不是使用已定义的类型类。通过例子(非常愚蠢的例子),我应该这样做:
data Cars = Brakes |车轮|引擎
数据计算机=处理器| RAM | HardDrive
class可修理的a其中
is_reparaible :: a - > Bool
实例可修复汽车其中
is_repairable(刹车)=真
is_repairable(车轮)=假
is_repairable(引擎)=假
实例可维修计算机其中
is_repairable(Processor)= False
is_repairable(RAM)= False
is_repairable(HardDrive)= True
checkState ::(可修复的a) => a - > ...
checkState a = ...
<显然,这是一个愚蠢的行为,不完整的例子)。
但是这对于一点用途很重要,不是吗?为什么我不应该做一些简单的事情,只定义函数而不定义新的数据类型和类型实例(使用它们的实例)。
这个例子太简单了,但事实上我当我在github上浏览Haskell代码而不是仅定义函数时,经常会看到类似的东西(新数据类型+ typeclasses +实例)。
所以,当我应该创建新的数据类型,类型类等,我应该什么时候使用函数? 。
为什么我不应该做一些简单的事情,只定义函数
,定义新的数据类型和类型类(使用它们的
实例)。
为什么?您可以定义:
checkState ::(a - > Bool) - > (a - > b) - > (a - > b) - > a - > b
checkState is_repairable repairs destroy a
= if(is_repaired a)then then a repairs a else destroy a
人们总是滥用类型类。这并不意味着它是惯用的。
要回答您的更一般的问题,下面是关于何时使用类型类以及何时不使用它们的一些经验法则:
使用类型类如果:
-
只有一个正确每种给定类型的行为
-
类型类具有所有实例都必须满足的关联方程(即规则)
不要使用类型类如果: 您正试图命名空间的东西。这就是模块和命名空间的作用。
I have some difficulties to understand when use and when not use typeclass in my code. I mean create my own, and not use already defined typeclasses, of course. By example (very stupid example), should I do:
data Cars = Brakes | Wheels | Engine
data Computers = Processor | RAM | HardDrive
class Repairable a where
is_reparaible :: a -> Bool
instance Repairable Cars where
is_repairable (Brakes) = True
is_repairable (Wheels) = False
is_repairable (Engine) = False
instance Repairable Computers where
is_repairable (Processor) = False
is_repairable (RAM) = False
is_repairable (HardDrive) = True
checkState :: (Reparaible a) => a -> ...
checkState a = ...
(Obviously, this is an stupid, incomplete example).
But this is a lot for a little use, no? Why I shouldn't do something simple and only defining functions without defining new data types and typeclasses (with their instances).
This example is too simple, but in facts I often see somethings like that (new data types+typeclasses+instances) when I browse Haskell code on github instead of only defining functions.
So, when I should create new data types, typeclasses etc and when should I use functions?
Thanks.
Why I shouldn't do something simple and only defining functions without defining new data types and typeclasses (with their instances).
Why indeed? You could just define:
checkState :: (a -> Bool) -> (a -> b) -> (a -> b) -> a -> b
checkState is_repairable repairs destroy a
= if (is_repairable a) then repairs a else destroy a
People misuse type classes all the time. It doesn't mean that it's idiomatic.
To answer your more general question, here are some rules of thumb for when to use type classes and when not to use them:
Use type classes if:
There is only one correct behavior per given type
The type class has associated equations (i.e. "laws") that all instances must satisfy
Don't use type classes if:
You are trying to just namespace things. That's what modules and namespaces are for.
A person using your type class cannot reason about how it will behave without looking at the source code of the instances
You find that the extensions you have to turn on are getting out of control
这篇关于我应该使用typeclasses或不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!