Skip to content

Provide low-level API to raylib without automatic freeing (WindowResources) #55

@oshyshko

Description

@oshyshko

It's enjoyable to have a raylib API in Haskell that takes care about Foreign stuff (pointers and marshaling), so one doesn't have to.

However, at the moment h-raylib enforces a particular memory management approach that may be not good for certain cases or favorable by some programmers. It would be nice for a library user programmer have a choice whether to use raylib without WindowResources facility.

Here is a great article on different levels of APIs Haskell (please, note that it's HTTP, you might need to instruct the browser to render it):
http://blog.haskell-exists.com/yuras/posts/a-begginers-guide-to-over-engineering.html

Note, how the API of the lowest level allows building higher level APIs on top of it, but not vice versa.

Please, note that raylib philosophy is about simplicity and not enforcing things, but rather leaving a door open to have them.

Low-level API with Closeable

Here's an example of a low API that allows automatic resource freeing, but can be opted out if not wanted:

with :: Closeable a => IO a -> (a -> IO b) -> IO b
with = flip bracket close

class                     Closeable a       where close :: a -> IO ()
instance                  Closeable Image   where close = ...
instance                  Closeable Texture where close = ...
instance                  Closeable Font    where close = ...
instance (Closeable c) => Closeable [c]     where close = mapM_ close

User programmers can decide how they want to write:

t <- openTexture "1.png"
...
close t

...or...

with (openText "1.png") $ \t -> do
  ...

...or something else.

With this design, user programmers can opt-out from using bracket/with and use something like resourcet or managed, if they want (but let's not focus on these approaches here).

Another important thing: user programmers are not forced to pass WindowsResources to every API call -- especially if they favor other resource-handling techniques such as with/close.

If a user programmer want, he can still implement WindowResources approach in their code on top of low-level API and track all the resources (e.g. this can be useful for runtime introspection). Also, he may go even further and push the WindowResources to a Reader monad, so he doesn't have to carry it around -- that's up to the user programmer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions