Code Monkey home page Code Monkey logo

Comments (2)

lierdakil avatar lierdakil commented on June 2, 2024 1

code examples

Sure. The context is I need to render some (actually quite a few) routes into PDFs. I didn't want to have to add logic for each and every action I need this for. Thus, I have this type:

data PdfController where
  PdfAction :: (Typeable c, Eq c, Show c, CanRoute c, HasPath c) => c -> PdfController

with corresponding instances:

instance Eq PdfController where
  (PdfAction a) == (PdfAction b) = Just a == cast b

deriving stock instance Show PdfController

instance HasPath PdfController where
  pathTo (PdfAction x) = "/Pdf" <> pathTo x

instance All Controller Pdfables => CanRoute PdfController where
  parseRoute' = string "/Pdf" *> choice
    ( tyApMany @Pdfables @ControllerC $
      \(Proxy @_ @x) -> PdfAction <$> parseRoute' @x
    )

where Pdfables is a type-level list of controllers I need to produce PDFs for, and tyApMany and All are type-level helpers for working with this list (I can post these, too, if you're interested, but they're somewhat trivial and the code isn't pretty). ControllerC is just a synonym type class for Data a, Controller a, CanRoute a (here Data should've been Typeable, but isn't due to this issue)

At the moment, I also require this (unused, bogus) instance:

instance Data PdfController where
  gunfold _ _ = error "PdfController.gunfold"
  toConstr = error "PdfController.toConstr"
  dataTypeOf = error "PdfController.dataTypeOf"

I also do this in FrontController:

type Controllers 
  = ... -- controllers that don't need to be "pdfable" go here
  : PdfController
  : Pdfables

instance FrontController WebApplication where
    controllers =
        [ ... -- any non-parseRoute routes go here, atm it's only startPage
        ] <> tyApMany @Controllers @ControllerC (\(Proxy @_ @x) -> parseRoute @x)

This all looks a tad overengineered, probably. The main goal was to make PDF generation as plug-and-play as possible, and it is: I just stick the controller I want to add PDFs for into the appropriate list, and I'm done.

from ihp.

mpscholten avatar mpscholten commented on June 2, 2024

Good catch, thanks for reporting 👍 Fixed via #1744

This is an inconvenience, as I have to write bogus Data instances for generic routes (which I represent with existential types).

Can you share some code examples of what you're doing? As it seems unusual I'm interested in what you're doing :)

from ihp.

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.