Crystal implementation of Scala's Option Monad
Add this to your application's shard.yml
:
dependencies:
option:
github: marceloboeira/option.cr
By requiring the library you add the following functions/classes to your global scope
none
- Creates an instance of the None(Nil) classsome(value)
- Creates an instance of the Some(T) class with the value type. e.g.: some("foo") -> Some(String)option(value)
- Returns either a None or a Some(T) instance
Option(T)
- Abstract class that defines the contract followed by None(T)/Some(T)None(T)
- Class that defines the interaction with nil valuesSome(T)
- Class that defines the interaction with the value
get
- Returns the value or raises an exception in case of nilor_else(default)
- Returns the value or the defined defaultempty?
- Returns true when the value is not nil, false when is nil
option("value") #<Some(String):0x10de55bc0 @value="value">
option(123) #<Some(Int32):0x10de5ef10 @value=123>
option(nil) #<None(Nil):0x10de5ef00>
option("123").or_else("0") # "123"
option(nil).or_else("0") # "0"
require "option"
module UnsafeIO
class User
def count : Int32?
# IO operation can fail
[10, nil].sample
end
end
end
10.times do
users = option(UnsafeIO::User.new.count).or_else(0)
puts "#{users} on storage"
end
Check the examples folder for more.