我正在使用Haskell和OpenGL.OpenGL有一种使用一组统一函数加载变量的方法:
glUniform1i location intValue
glUniform1f location floatValue
glUniform2i location intValue1 intValue2
... etc.
Run Code Online (Sandbox Code Playgroud)
我试图将其翻译成更惯用的Haskell.我想写一个函数:
uniform :: String -> a -> IO ()
uniform location value = ...
Run Code Online (Sandbox Code Playgroud)
我的问题是我调用的函数依赖于的值a.有没有办法做到这一点,而无需编写ň不同的功能?
定义此类函数的一种方法是定义新类型,如:
class GlUniformable a where
uniform:: String -> a -> IO()
Run Code Online (Sandbox Code Playgroud)
每个实例都可以有像
instance GlUniformable Int where
uniform location value = ...
Run Code Online (Sandbox Code Playgroud)
其中value将被Int编译器理解为类型的参数