package ppx_deriving_madcast

  1. Overview
  2. Docs
Library deriving cast functions based on their types.

Install

Dune Dependency

Authors

Maintainers

Sources

v0.1.tar.gz
sha256=520e423e92e776610dba2a3d6e24bc6a5f975930251d45ae8780dd86641051f2
md5=312d4fcb58029810900c50c7cb9cfe69

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

Published: 21 May 2018

README

README.org

#+TITLE: [%madcast:] [[https://travis-ci.org/Niols/ppx_deriving_madcast][https://travis-ci.org/Niols/ppx_deriving_madcast.svg?branch=master]]
#+STARTUP: indent

/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:
#+BEGIN_SRC ocaml
[%madcast: int -> string]
#+END_SRC
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:
#+BEGIN_SRC ocaml
let points =
  read_line ()
  |> String.split_on_char ' '
  |> [%madcast: string list -> (int * int) array]
#+END_SRC

** MySQL API
/madcast/ is primarily meant to be used in conjunction with low level
API. Here is an example with MySQL:
#+BEGIN_SRC ocaml
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
#+END_SRC

** Try it yourself!
If you are able to build the project and the tests (=make && make
test=), then you can see by yourself the code generated for a given
type with =test/show.sh=:
#+BEGIN_SRC ocaml
$ sh test/show.sh '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))))))
#+END_SRC

* Installation
** Using OPAM
=ppx_deriving_madcast= is now available on OPAM:
: opam install ppx_deriving_madcast
** Dependencies
To build:
- =jbuilder=
- =ppx_deriving=
- =ppxfind=
- =ppx_tools=

To install:
- =opam-installer=

To release:
- =opam-query=
- =opam-publish=

* API
/madcast/ also provides an API in case you only want the function that
takes two =Parsetree.core_type= and derives a =Parsetree.expression= that
casts the first =core_type= into the second one.

This API is in the package =ppx_deriving_madcast.api=.

* License
/madcast/ is distributed under the LGPL License.

Dependencies (5)

  1. jbuilder >= "1.0+beta7"
  2. ppxfind build
  3. ppx_tools
  4. ppx_deriving < "5.0"
  5. ocaml >= "4.04.0"

Dev Dependencies (1)

  1. ocamlfind with-test

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.