Giter VIP home page Giter VIP logo

Comments (6)

sboosali avatar sboosali commented on May 31, 2024

iiuc, like the technique that supports variadic functions, this technique lets a method have implementations with different arities.

(But this might be wrong, since I'm not quite sure what you mean, without a concrete program, and because I'm not familiar with the Op typeclass either)

from fltkhs.

MarisaKirisame avatar MarisaKirisame commented on May 31, 2024

@sboosali To me that sounds to be too little gain... Different arities can be done by uncurrying

from fltkhs.

sboosali avatar sboosali commented on May 31, 2024

Yes, which requires records. However, a design goal of this package is making the many fltk examples (written in C), fltk being extensively documented, as copy-pasteable and "mutually intelligible" as possible.

from fltkhs.

deech avatar deech commented on May 31, 2024

It can be done but it's ugly and it should really be part of the API but here's how to do it. Say you originally had something like :

ui :: IO ()
ui = do
 window <- windowNew ...
 begin window
 button <- buttonNew ...
 ...
 end window
 ...

If you only cared about Window and not any of it's subclasses (like SingleWindow, DoubleWindow etc.) you can do:

within :: Ref Window -> IO () -> IO ()
within window action = do
  begin window
  action
  end window

ui :: IO ()
ui = do
 window <- windowNew ...
 within window $ do
   button <- buttonNew ...
   ...
 ...

However if you want it to work for any Window it becomes ugly:

withinGeneric ::
  (
    Parent win Window,
    Match x ~ FindOp win win (Begin ()),
    Op (Begin ()) x win (IO ()),
    Match y ~ FindOp win win (End ()),
    Op (End ()) y win (IO ())
  )
  => Ref win -> IO () -> IO ()
withinGeneric ref' action = do
  () <- begin ref'
  action
  end ref'

ui :: IO ()
ui = do
 window <- windowNew ...
 withinGeneric window $ do
   button <- buttonNew ...
   ...
 ...

There's a lot of complexity here but in essence the Parent win Window constraint ensures that win is a "subclass" of Window, then the pair of Match x ~ FindOp win win (Begin ()), Op (Begin ()) x win (IO ()) first finds a function called begin on win and then ensure that it's of type IO () and then similarly with end.

In any case this should be provided so feel free to leave this issue open and I'll add it to the API on the next release. You're also free to put up a PR if you want. :)

Edit: You'll also want to add:

{-# LANGUAGE FlexibleContexts, TypeFamilies #-}

from fltkhs.

deech avatar deech commented on May 31, 2024

And to answer your original question about FunDeps, I did try them originally but they didn't work because impl is a function signature like (a -> b -> IO ()). So for example, when setting the font size of a Widget, impl is (Font -> IO ()) which has to be a constraint which is ignored for the purposes of FunDeps.

from fltkhs.

MarisaKirisame avatar MarisaKirisame commented on May 31, 2024

this is exactly what I do (and it work)
it is incredibly ugly though. I agree that it should be added into the library.
I will work on the PR :)

from fltkhs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.