Haskell “的含义;“类型”;类型类声明中的声明
我刚刚发现了这段代码:Haskell “的含义;“类型”;类型类声明中的声明,haskell,type-families,associated-types,Haskell,Type Families,Associated Types,我刚刚发现了这段代码: -- | 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 directi
-- | 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
是一个关联类型,这是一种声明与类实例关联的类型族实例的方法。由Loc a
表示的类型由a
的类型确定,并在实例中指定:例如
instance Slice Foo where
type Loc Foo = Bar
...
在类声明中出现loca
的地方,它将被实例中相应的类型替换-因此Foo
的实例函数如下所示
sliceEvents :: Foo -> [ResolvedEvent]
...
sliceFrom :: Foo -> Bar
...
关联类型还可以通过提供类约束在类声明之外的其他函数中使用:例如
myFunction :: (Slice a) => a -> Loc a
这不是标准的Haskll 2010;它是GHC的一种更具异国情调的语言扩展。(关联类型,我想…)是的,这就是名字。。。。。这是扩展。。。{-#语言类型族{-}