Coder Social home page Coder Social logo

defopaque's Introduction

Defopaque

An experiment in defining 'newtypes' in Elixir.

Read the introduction here.

# file: mix.exs
defp deps do
  [
    {:defopaque, git: "https://github.com/pzel/defopaque.git", tag: "master"}
  ]
end

5-minute HOWTO

defmodule MMM do
  use Defopaque # load up the `defopaque/2` and `defopen/2` marcos

  # Generate a `valid_email(string)`
  # 1. type definition
  # 2. constructor macro, and (by extension)
  # 3. pattern-match marco
  defopen(:valid_email, String.t())

  # Generate a `token(number)` opaque type.
  # A constructor will be genereted, but
  # it will only be available in this module.
  defopaque(:token, number())


  def new_token(), do: token(System.unique_integer())

  def email_from_string(s) do
    # if string is legitimate valid email address, then:
    valid_email(s)
  end

  # we can pattern-match on `token/1` here, as it is 'our' opaque type
  def admission_pass(token(n), valid_email(s)) do
    %{seat_number: n, email_address: s}
  end
end


iex()> import MMM
iex()> input = "[email protected]"

iex()>
case MMM.email_from_string(input) do
  valid_email(^input) -> {:ok, "your email was accepted"}
  _ -> {:error, "Invalid email"}
end
{:ok, "your email was accepted"}

iex()> t = MMM.new_token()
iex()> token(^number) = t
** (CompileError) iex:18: undefined function token/1

iex()> MMM.admission_pass(t, valid_email(input))
%{email_address: "[email protected]", seat_number: -576460752303422719}
iex()> MMM.admission_pass({:token, 123}, valid_email(input))
** (FunctionClauseError) no function clause matching in MMM.admission_pass/2

    The following arguments were given to MMM.admission_pass/2:

        # 1
        {:token, 123}

        # 2
        {:"d237c68fd9b493a61d02bf9e-valid_email", "[email protected]"}

    iex:42: MMM.admission_pass/2

defopaque's People

Contributors

pzel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.