在使用PureScript教程时,代码示例开始使用"=>"而不引入它.结果我不明白何时使用'=>'而不是' - >'.
例如,这使用'=>':
instance showArray :: (Show a) => Show (Array a) where
show array = "[" <> map show array <> "]"
Run Code Online (Sandbox Code Playgroud)
这里使用' - >':
greet :: forall r. { name :: String | r} -> String
greet namedThing = "Hello, " ++ namedThing.name
Run Code Online (Sandbox Code Playgroud)
(Show a) =>是一种类型约束,它将类型限制为类a的实例,Show并且a -> b是一种函数.所以这段代码
foo :: forall a. (Show a) => a -> b
Run Code Online (Sandbox Code Playgroud)
是一个函数foo从a到b和类型a必须有类的一个实例Show
在OO语言中,它将是这样的
public B foo<A,B>(A x) where A:IShow
Run Code Online (Sandbox Code Playgroud)