package ppx_deriving_madcast

All checked builds pass
  1. Overview
  2. Docs
Library deriving cast functions based on their types

Install

dune-project
 Dependency

Authors

Maintainers

Sources

v0.2.tar.gz
md5=bca83dbf5a6fb47ced07756c6cab1d9a
sha512=f2f604685649d8fc0c750adfb89d997ea62f2fd09f383dc38b4580fdbb7f96e45a637745f1f06aa1b529d6abcfea6691ef309eb32e423c780c2370aff4678950

Description

This package provides a PPX that allows to derive cast functions based on their types.

For instance, [%madcast: ('a * string) -> ('a * int)] would be replaced by:

fun (x, y) ->
  (x,
   try int_of_string y
   with Failure _ -> failwith "madcast: string -> int")

Tags

syntax

Added to opam-repository:

README

[%madcast:]

Madcast is a library deriving cast functions based on their types.

Examples

Base types

For base types, Madcast uses the functions defined in the standard library:

[%madcast: int -> string]

will simply be replaced by string_of_int.

Parsing positions

Say you have to parse a line of coordinates x1, y1, x2, y2, etc. and you want an array of pairs of integers:

let points =
  read_line ()
  |> String.split_on_char ' '
  |> [%madcast: string list -> (int * int) array]

MySQL API

Madcast is primarily meant to be used in conjunction with low level API. Here is an example with MySQL:

let () =
  let result = Mysql.exec conn "SELECT id, name, surname FROM person WHERE username='johndoe'" in
  let row = Mysql.fetch result
            |> [%madcast: string option array option -> (int * string * string option) option]
  in match row with
  | None -> Format.eprintf "Could not find user `johndoe`@."
  | Some (id, name, None) -> Format.eprintf "%s (%d) has no surname.@." name id
  | Some (id, name, Some surname) -> Format.eprintf "%s (%d) has %s for surname.@." name id surname

Try it yourself!

You can see by yourself the code generated for a given type with test/show.exe:

$ dune exec test/show.exe 'string array -> (int * int) array'
fun a  ->
  if ((Array.length a) mod 2) <> 0
  then failwith "madcast: 'a array -> <tuple> array"
  else
    Array.init ((Array.length a) / 2)
      (fun i  ->
         (((fun s  ->
              try int_of_string s
              with | Failure _ -> failwith "madcast: string -> int")
             (a.(0 + (i * 2)))),
           ((fun s  ->
               try int_of_string s
               with | Failure _ -> failwith "madcast: string -> int")
              (a.(1 + (i * 2))))))

Actually, if you feel fancy, we recommend adding ocamlformat and bat to the lot and running:

$ dune exec test/show.exe 'string list -> (int * int) array' \
      | ocamlformat - --impl --enable-outside-detected-project \
      | bat --language ocaml

And if you don't feel like cloning but you love Nix, you can also go for:

$ nix run github:LesBoloss-es/ppx_deriving_madcast#show -- 'string array -> (int * int) array'

Installation

Using OPAM

ppx_deriving_madcast is available on OPAM:

$ opam install ppx_deriving_madcast

API

Madcast also provides an API. This API is in the package ppx_deriving_madcast.api. Its documentation can be built with make doc.

License

Madcast is distributed under the LGPL License, version 3.

Dependencies (4)

  1. ppxlib >= "0.15.0"
  2. ppx_deriving >= "5.0"
  3. ocaml
  4. dune >= "2.5"

Dev Dependencies (1)

  1. odoc with-test

Optional Dependencies

None

Used by

None

Conflicts

None

Build status

Results of building ppx_deriving_madcast with each compiler on check.ci.ocaml.org, run of against opam-repository 8a77e58. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
0.2 (this version) good good good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure