Haskell:SDL 绑定中的奇怪实例声明

发布于 2024-10-15 20:36:56 字数 599 浏览 2 评论 0原文

Haskell SDL 绑定导出一个名为 SDLKey 的枚举类型。不过,Enum 实例的声明方式很奇怪:

instance Enum SDLKey Word32 where
    fromEnum SDLK_UNKNOWN = 0
    fromEnum SDLK_FIRST = 0
    fromEnum SDLK_BACKSPACE = 8
    ...

SDLKey 不接受任何类型参数,这怎么不是语法错误呢? Haskell 报告 SDLKey 不是 Enum 的实例,那么 Enum 函数是为什么类型定义的?而且,最重要的是,给定 SDLKey,我如何调用它的 Enum 函数?

来自 hackage 的源代码:http://hackage.haskell.org/packages/archive/SDL/0.6.2/doc/html/src/Graphics-UI-SDL-Keysym.html#SDLKey

The Haskell SDL bindings export an enumerated type called SDLKey. The Enum instance is declared in a strange way though:

instance Enum SDLKey Word32 where
    fromEnum SDLK_UNKNOWN = 0
    fromEnum SDLK_FIRST = 0
    fromEnum SDLK_BACKSPACE = 8
    ...

SDLKey does not take any type parameters, how is this not a syntax error? Haskell reports that SDLKey is not an instance of Enum, so for what type are the Enum functions being defined? And, most importantly, given an SDLKey, how can I invoke the Enum functions on it?

Source code from hackage here: http://hackage.haskell.org/packages/archive/SDL/0.6.2/doc/html/src/Graphics-UI-SDL-Keysym.html#SDLKey

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巾帼英雄 2024-10-22 20:36:56

它不是 Prelude 中的 Enum 实例(请参阅文件顶部附近的 import Prelude hide (Enum(..)))。

它是 来自 Graphics.UI.SDL.Utilities 的枚举,它采用第二个参数:

class Enum a b | a -> b where
  succ :: a -> a
  pred :: a -> a
  toEnum :: b -> a
  fromEnum :: a -> b
  enumFromTo :: a -> a -> [a]

与 Prelude 中的定义进行比较:

class  Enum a   where
    succ                :: a -> a
    pred                :: a -> a
    toEnum              :: Int -> a
    fromEnum            :: a -> Int
    enumFrom            :: a -> [a]
    enumFromThen        :: a -> a -> [a]
    enumFromTo          :: a -> a -> [a]
    enumFromThenTo      :: a -> a -> a -> [a]
    -- comments and default definitions elided

It is not an instance of Enum from the Prelude (see import Prelude hiding (Enum(..)) near the top of the file).

It is an instance of Enum from Graphics.UI.SDL.Utilities, which takes a second parameter:

class Enum a b | a -> b where
  succ :: a -> a
  pred :: a -> a
  toEnum :: b -> a
  fromEnum :: a -> b
  enumFromTo :: a -> a -> [a]

Compare to the definition from the Prelude:

class  Enum a   where
    succ                :: a -> a
    pred                :: a -> a
    toEnum              :: Int -> a
    fromEnum            :: a -> Int
    enumFrom            :: a -> [a]
    enumFromThen        :: a -> a -> [a]
    enumFromTo          :: a -> a -> [a]
    enumFromThenTo      :: a -> a -> a -> [a]
    -- comments and default definitions elided
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文