[英]meaning of “type” declaration in a typeclass declaration
我刚刚落在那段代码上:
-- | Gathers common slice operations.
class Slice a where
type Loc a
sliceEvents :: a -> [ResolvedEvent]
-- ^ Gets slice's 'ResolvedEvent's.
sliceDirection :: a -> ReadDirection
-- ^ Gets slice's reading direction.
sliceEOS :: a -> Bool
-- ^ If the slice reaches the end of the stream.
sliceFrom :: a -> Loc a
-- ^ Gets the starting location of this slice.
sliceNext :: a -> Loc a
-- ^ Gets the next location of this slice.
toSlice :: a -> SomeSlice
-- ^ Returns a common view of a slice.
我不明白type Loc a
正在做什么type Loc a
......
Loc a
是一种关联类型,它是一种声明与类实例关联的类型族实例的方法。 由表示的类型Loc a
通过的类型确定a
,并且在该实例中指定的:例如
instance Slice Foo where
type Loc Foo = Bar
...
无论Loc a
出现在类声明中,它都将被替换为实例中的相应类型 - 因此Foo
的实例函数看起来像
sliceEvents :: Foo -> [ResolvedEvent]
...
sliceFrom :: Foo -> Bar
...
通过给出类约束,关联类型也可以在类声明之外的其他函数中使用:例如
myFunction :: (Slice a) => a -> Loc a
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.