lua-Coat
Yet a Another Lua Object-Oriented Model

References

This module provides the ability to create type contraints to be used in attribute definition.

All are stored in a global space, ie. not in a class space.

Global Functions

as( parent )

sugar for subtype.

coerce( type, from, via [, from, via, ...] )

Declares the type coerce from the type named from via a function via.

enum( name, string1, string2 [, ...] )

Creates a subtype name for a given set of strings.

from( name )

sugar for coerce.

message( msg )

sugar for subtype.

subtype( name, parent, validator [, msg ] )

Creates a subtype name from the type parent which passes the constraint specified in the function validator, the optional msg is used as error message.

via( func )

sugar for coerce.

where( validator )

sugar for subtype.

Examples

With Sugars

Sugars are useful only with a patched lua.

require 'Coat.Types'

subtype 'Natural'
    => as 'number';
    => where( function (val) return val > 0 end )

subtype 'NaturalLessThanTen'
    => as 'Natural';
    => where( function (val) return val < 10 end )
    => message "This number (%d) is not less than ten!";

enum 'Colour'
    => { 'Red', 'Green', 'Blue' }

subtype 'DateTime'
    => as 'string';
    => where( function (val) return true end )

coerce 'DateTime'
    => from 'number';
    => via( function (val) return os.date("!%d/%m/%Y %H:%M:%S", val) end )