Coder Social home page Coder Social logo

ltypekit's Introduction

ltype

Advanced type checking library for Lua and Moonscript.

ltype.type

This is a custom type checking function that supports custom types using resolvers. Similarly to typical or mtype, it first checks for a __type metavalue that will be returned (or called, if it is a function). Secondly, it checks whether the value is an io handle, and returns the "io" type if so. If there are no other resolvers, it will return the primitive type.

import type from require "ltype.type"

print type 5
--> number

xy_resolver = (any) ->
  if ((type any)    == "table") and
     ((type any[1]) == "number") and
     ((type any[2]) == "number") and
     (#any == 2)
    "position"
  else false
table.insert type.resolvers, xy_resolver

print type {5,6}
--> position

ltype.init

This provides a way of attaching type signatures to functions, they can have the following formats:

number -> number -> number
  Equivalent to x -> (y -> z)
  Curried function. Unsafe mode will apply the right part (y -> z) to the function returned as a signature
number, number -> number
  Takes two arguments and returns another
number -> number, number
  Takes an argument and returns two
* -> string
  Takes any value, returns a string
! -> string
  Takes any value but nil, returns a string
x -> x
  Uses a placeholder (not registered in type.types)
  Input and output must be the same type

Usage:

import signature from "ltype"

add = (signature "number, number -> number") (a, b) -> a + b
print add 1, 1

add_curry = signature "number -> number -> number"
add_curry (a) -> (b) -> a + b
print  (add_curry 1, 2) 1 -- throws warning if .silent == false

add_curry_silent = with signature "number -> number -> number"
  .silent = true   -- Throws warnings
  .safe   = false  -- Warnings are not errors
add_curry_silent (a) -> (b) -> a + b
print dart (add_curry_silent 1) 1

tostring_ = with signature "* -> string"
  .silent = false
  .safe   = false
tostring_ (any) -> tostring any
print tostring_ nil
print tostring_ 2

position_resolver = (any) ->
  if ((type any)    == "table") and
     ((type any[1]) == "number") and
     ((type any[2]) == "number") and
     (#any == 2)
    "position"
  else false
position = signature "number, number -> position"
position - {"position"} + position_resolver
position (x, y) -> {x, y}
pos = position 3, 2
print pos

same_io = (signature "x -> y") (x) -> tonumber x
print same_io "5"

ltypekit's People

Contributors

daelvn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ltypekit's Issues

Add busted testing

Add busted tests for this project.

  • Check a
  • Check a -> a
  • Check a -> a -> a
  • Check (a -> a) -> a
  • Check a, a -> a
  • Check a -> a, a
  • Check * -> a
  • Check ! -> a
  • Check x -> a
  • Check a -> *
  • Check a -> !
  • Check a -> x
  • Check extra argument handling
  • Check right side signature attaching
  • Check wrong values

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.