Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ReasonML @ OCaml Users in Paris

ReasonML @ OCaml Users in Paris

Matthias Le Brun

September 26, 2017
Tweet

More Decks by Matthias Le Brun

Other Decks in Programming

Transcript

  1. let rec qsort = function | [] !-> [] |

    pivot #:: rest !-> let is_less x = x < pivot in let left, right = List.partition is_less rest in qsort left @ [pivot] @ qsort right
  2. Storytime •Initially, React was written in SML by Jordan Walke

    •The idea for Reason actually predates React •Front-end community wasn’t ready yet
  3. Functions let x a b = e let x =

    fun a b !-> e OCaml Reason let x a b %=> e; let x = fun a b %=> e;
  4. Equality a = b b '== c a #<> b

    a *!= b OCaml Reason a '== b b ##=== c a *!= b a --!== b
  5. Expressions let c = let a = "a" in let

    b = "b" in a ^ b OCaml Reason let c = { let a = "a"; let b = "b"; a ^ b };
  6. Tuples type tup = int * int let tuple =

    a, b OCaml Reason type tup = (int, int) let tuple = (a, b)
  7. Record types type rec = { foo: string; bar: string

    }; OCaml Reason type rec = { foo: string, bar: string };
  8. Records { foo = "bar"; bar = "baz" } OCaml

    Reason { foo: "bar", bar: "baz", }
  9. Variants type foo = | A of string | B

    of string OCaml Reason type foo = | A string | B string
  10. Pattern matching match a with | A x %=> "A"

    ^ x | B x %=> "B" ^ x OCaml Reason switch a { | A x %=> "A" ^ x | B x %=> "B" ^ x }
  11. BuckleScript •Like js_of_ocaml but takes on after an earlier step

    of compilation •Makes some optimisations considering the JS target •Great interoperability
  12. Smaller footprint var user = //* record -*/[ //* username

    -*/"Bob", //* id : Some -*/["abc"], //* age -*/32 ];
  13. type user = { name: string, age: int }; let

    fromJs jsObject %=> { name: jsObject###name, age: jsObject###age }; Convert from JS
  14. type message = | Image string | String string; let

    fromJs js %=> { switch js###message_type { | "image" %=> Image js###value | "string" | _ %=> String js###value } } Complex conversions
  15. let getPageKeywords () %=> switch ( DomRe.Document.querySelector "meta[name=\"keywords\"]" DomRe.document )

    { | Some meta %=> switch (DomRe.Element.getAttribute "content" meta) { | Some content %=> Js.String.split "," content *|> Array.to_list *|> List.map String.trim | None %=> [] } | None %=> [] };
  16. Simpler workflow •Effort is for now a lot on the

    JS back-end •Coming for native compilation
  17. ReactJS import React from "react"; import { string } from

    "prop-types"; const App = props %=> ( <div> {props.message} !</div> ) App.propTypes = { message: string.isRequired };
  18. ReasonReact let component = ReasonReact.statelessComponent "App"; let make #::message _children

    %=> { …component, render: fun _ %=> <div> (ReasonReact.stringToElement message) !</div> };
  19. ReasonReact let component = ReasonReact .reducerComponent "App"; let make _

    %=> { …component, reducer: fun action state %=> switch action { | Increment %=> ReasonReact.Update (state + 1) | Decrement %=> ReasonReact.Update (state - 1) }, //* … -*/ }
  20. ReasonReact render: fun {state, reduce} %=> <div> (ReasonReact.stringToElement (string_of_int state))

    <Button title="-" onClick=(reduce (fun _ %=> Increment)) 6/> !</div>
  21. <div> (switch resource { | Loading %=> <ActivityIndicator 6/> |

    Idle value %=> <Component value 6/> | Error %=> ErrorMessage.serverError }) !</div> ReasonReact
  22. module MyType = { let name = "Type"; type t

    = string; }; module MyTypeCollectionView = Primitives.FixedCollectionView.Make MyType; <MyTypeCollectionView items 6/> ReasonReact
  23. • A new, simpler syntax for OCaml • Lots of

    efforts in improving tooling (BetterErrors, BuckleScript is a friend project) • New features like JSX • A new community (super active at the moment) • A great core team who knows what to prioritise first to make it good for everyone