• 2 Posts
  • 43 Comments
Joined 1 year ago
cake
Cake day: June 15th, 2023

help-circle

  • I’ve tried something recently in a project with Dream.

    Suppose you have an url like /page/param1/param2/ : you want to be able to do three things:

    1. Build this url in the application in a safe way, which mean : param1 -> param2 -> string
    2. Create a route which handle this url pattern (the compilation should fail if you don’t handle all the arguments)
    3. Ensure the handler will have the signature param1 -> param2 -> Dream.handler

    Of course, some pages will have two arguments, some other three, and you have to find a way represent this in the type system.

    For this, I’ve used a gadt:

    
    type ('a, 'b) t =
      | Static : string * ('a, 'b) t -> (unit -> 'a, 'b) t
      | Int : string * ('a, 'b) t -> (int64 -> 'a, 'b) t
      | String : string * ('a, 'b) t -> (string -> 'a, 'b) t
      | End : ('a, 'a) t
    

    The string is the parameter name in the url (id, login, …) and the gadt make the representation for this parameter in the type system. This gives me a way to declare some urls in the application:

    val root : (unit -> 'a, 'a) t
    (** The path to root / *)
    
    val topic_url : (string -> 'a, 'a) t
    (** The path to /:topic *)
    
    val thread_url : (string -> int64 -> 'a, 'a) t
    (** The path to /:topic/:thread *)
    

    Then I have some some functions wihch handle this gadt:

    val repr : (unit -> ('a, 'b) t) -> string
    (** Represent the route associated with this path *)
    
    val unzip : (unit -> ('a, 'b) t) -> (string -> string) -> 'a -> 'b
    (** Extract the parameters from the request. 
    
        [unzip path extract f] will apply the function extract for all the
        arguments in the path, and gives them to f 
    
        The function [extract] is given in argument in order to break dependency
        circle.
    
        This should be something like: 
    
            let extract_param request name =
              Dream.param request name |> Dream.from_percent_encoded
    
    *)
    


  • Alors c’est parti, digressons (le vinaigre balsamique n’est pas vraiment un vinaigre donc je suis un hors sujet)

    J’ai un vinaigrier dans la cuisine et je fais propre vinaigre. En fait c’est pas compliqué, une fois que la transformation est lancée, ca continuer tout seul, ca prend juste du temps. Donc le vinaigre se fait pas l’action d’un champignon qui transforme l’alcool en acide.

    Ce champignon se développe en milieu acide et continue d’acidifier son environnement, une fois que ca a commencé on est en tranquille, il n’y a pas de risque que ca tourne mal.

    Bref, tous les trois mois, je retire 1/4 du contenu, et je le remplace par du vin nouveau. Et j’ai un vinaigre parfumé et gouteux, qui n’a rien a voir avec ce que l’on achète (mais ca prend du temps), on sent le goût derrière l’acidité (comme on peut sentir le goût du vin derrière l’alcool)

    Je pense qu’il ne doit plus y avoir grand monde a faire ca chez soi, mais je vous assure que c’est un vrai plaisir d’avoir ca dans sa cuisine