Haskell:SDL 绑定中的奇怪实例声明
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不是 Prelude 中的 Enum 实例(请参阅文件顶部附近的
import Prelude hide (Enum(..))
)。它是 来自 Graphics.UI.SDL.Utilities 的枚举,它采用第二个参数:
与 Prelude 中的定义进行比较:
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:
Compare to the definition from the Prelude: