"type" 声明在类型类声明中的含义
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
在做什么...
Loc a
是关联类型,这是一种声明与 class 实例关联的类型族实例的方式。 Loc a
表示的类型由a
的类型决定,在实例中指定:例如
instance Slice Foo where
type Loc Foo = Bar
...
无论 Loc a
出现在 class 声明中的什么地方,它都会被实例中的相应类型替换 - 因此 Foo
的实例函数看起来像
sliceEvents :: Foo -> [ResolvedEvent]
...
sliceFrom :: Foo -> Bar
...
关联类型也可以通过给定 class 约束在 class 声明之外的其他函数中使用:例如
myFunction :: (Slice a) => a -> Loc a
我刚刚迷上了那段代码:
-- | 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
在做什么...
Loc a
是关联类型,这是一种声明与 class 实例关联的类型族实例的方式。 Loc a
表示的类型由a
的类型决定,在实例中指定:例如
instance Slice Foo where
type Loc Foo = Bar
...
无论 Loc a
出现在 class 声明中的什么地方,它都会被实例中的相应类型替换 - 因此 Foo
的实例函数看起来像
sliceEvents :: Foo -> [ResolvedEvent]
...
sliceFrom :: Foo -> Bar
...
关联类型也可以通过给定 class 约束在 class 声明之外的其他函数中使用:例如
myFunction :: (Slice a) => a -> Loc a