haskell-opengl / OpenGL

Haskell bindings to OpenGL
http://www.haskell.org/haskellwiki/OpenGL
BSD 3-Clause "New" or "Revised" License
147 stars 26 forks source link

Functor GettableStateVar #60

Closed ekmett closed 9 years ago

ekmett commented 9 years ago

Please add

instance Functor GettableStateVar where
  fmap f (GettableStateVar m) = GettableStateVar (fmap f m)

I can define it outside of the package, but it seems particularly bad to have to both needlessly go through the smart constructor and introduce an orphan.

instance Functor GL.GettableStateVar where
  fmap f v = makeGettableStateVar (f <$> get v)

Would you consider exporting the constructors of these data types as well?

svenpanne commented 9 years ago

OK, I've added the instance Functor instance above and exported the constructors for a 3 kinds of state variables. Just out of curiosity: What exactly do you need the latter for? Should I add Applicative and Monad instances for GettableStateVar, too? Or even the following? (Just joking for the last part... ;-)

instance Contravariant SettableStateVar where
   contramap f (SettableStateVar s) = SettableStateVar (s . f)
ekmett commented 9 years ago

I'd take the Contravariant instance for SettableStateVar. I have that in my own code. =P

But I can accept that you can't add the dependency. ;)

I'm using "invariant functors" to map over a number of StateVars that i have access to that I know are always booleans in SDL 2, because the alternative is that I have to duplicate code.

svenpanne commented 9 years ago

What about the Applicative and Monad instances for GettableStateVar? I think it might make sense to actually add them, but I don't feel very strongly about this.

ekmett commented 9 years ago

Another option would be to just remove the GettableStateVar abstraction entirely and just turn it into a type alias for IO.

Then the need for 'get' actually vanishes entirely / it can just become id, because users can just use the state var as the action to access it.

This cleans up code that reads from these state variables quite a bit.

On Tue, Oct 21, 2014 at 11:27 AM, Sven Panne notifications@github.com wrote:

What about the Applicative and Monad instances for GettableStateVar? I think it might make sense to actually add them, but I don't feel very strongly about this.

— Reply to this email directly or view it on GitHub https://github.com/haskell-opengl/OpenGL/issues/60#issuecomment-59946077 .