# SmartCore::Types · [![Supporting](https://github.com/Cado-Labs/cado-labs-logos/blob/main/cado_labs_badge.png)](https://github.com/Cado-Labs/) · [![Gem Version](https://badge.fury.io/rb/smart_types.svg)](https://badge.fury.io/rb/smart_types) > A set of objects that acts like types (type checking and type casting) with a support for basic type algebra. Minimalistic type system for any ruby project. Supports custom type definitioning, type validation, type casting and type categorizing. Provides a set of commonly used type categories and general purpose types. Has a flexible and simplest type definition toolchain. ---

Supported by Cado Labs

--- ## Installation ```ruby gem 'smart_types' ``` ```shell bundle install # --- or --- gem install smart_types ``` ```ruby require 'smart_core/types' ``` --- ## Usage - [Type interface and basic type algebra](#type-interface-and-basic-type-algebra) - [Supported types](#supported-types) - [Primitives](#primitives) (`SmartCore::Types::Value`) - [Protocols](#protocols) (`SmartCore::Types::Protocol`) - [Variadic](#variadic) (`SmartCore::Types::Variadic`) - [Nilable types](#nilable-types) - [Custom type definition](#custom-type-definition) - [Primitive type definition](#primitive-type-definition) - [With type invariants](#with-type-invariants) - [Type validation](#type-validation) - [Type casting](#type-casting) - [Roadmap](#roadmap) - [Contributing](#contributing) - [Build](#build) - [License](#license) - [Authors](#authors) --- ## Type Interface and basic type algebra ```ruby # documentation is coming type.valid?(value) type.validate!(value) type.cast(value) type.nilable type3 = type1 | type2 type4 = type1 & type2 ``` Types with runtime: ```ruby # get a type object with a custom runtime (instances of String or Symbol): type = SmartCore::Types::Protocol::InstanceOf(::String, ::Symbol) type.valid?(:test) # => true type.valid?('test') # => true type.valid?(123.456) # => false # another type object with a custom runtime (tuple (String, Integer, Time)): type = SmartCore::Types::Variadic::Tuple(::String, ::Integer, ::DateTime) type.valid?(['test', 1, DateTime.new]) # => true type.valid?([:test, 2]) # => false ``` ## Supported types #### Primitives: ```ruby SmartCore::Types::Value::Any SmartCore::Types::Value::Nil SmartCore::Types::Value::String SmartCore::Types::Value::Symbol SmartCore::Types::Value::Text SmartCore::Types::Value::Integer SmartCore::Types::Value::Float SmartCore::Types::Value::Numeric SmartCore::Types::Value::BigDecimal SmartCore::Types::Value::Boolean SmartCore::Types::Value::Array SmartCore::Types::Value::Set SmartCore::Types::Value::Hash SmartCore::Types::Value::Proc SmartCore::Types::Value::Class SmartCore::Types::Value::Module SmartCore::Types::Value::Time SmartCore::Types::Value::DateTime SmartCore::Types::Value::Date SmartCore::Types::Value::TimeBased ``` --- #### Protocols: ```ruby SmartCore::Types::Protocol::InstanceOf ``` ```ruby # examples (SmartCore::Types::Protocol::InstanceOf): SmartCore::Types::Protocol::InstanceOf(::Integer) # only integer SmartCore::Types::Protocol::InstanceOf(::String, ::Symbol) # string or symbol SmartCore::Types::Protocol::InstanceOf(::Time, ::DateTime, ::Date) # time or datetime or date ``` --- #### Variadic: ```ruby SmartCore::Types::Variadic::ArrayOf SmartCore::Types::Variadic::Enum SmartCore::Types::Variadic::Tuple ``` ```ruby # example (SmartCore::Types::Variadic::ArrayOf): SmartCore::Types::Variadic::ArrayOf(::String, ::Array) # array of strings or arrays # example (SmartCore::Types::Variadic::Enum): SmartCore::Types::Variadic::Enum('string', 1337, :symbol) # one of enumerated values # examples (SmartCore::Types::Variadic::Tuple): SmartCore::Types::Variadic::Tuple(::String, ::Integer, ::Time) # array with signature [, ,