Skip to content

initdc/types.cr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

types

Bring the Rust [Result Option] types to Crystal

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      types:
        github: initdc/types.cr
  2. Run shards install

Usage

require "types"

s = Some[0]
n = None(Int32)[]

r = Ok(Int32, String)[0]
e = Err(Int32, String)["error"]

puts "s: #{s}"
puts "n: #{n}"
puts "r: #{r}"
puts "e: #{e}"

arr = [0, 1, 2]

p arr[0]
p Option.from(arr[0])
p arr[10]?
p Option.from?(arr[10]?)
p Option.from! { arr[10] }

Better way

Just use the Union(*T) type from Crystal itself.

def div?(a : Float64, b : Float64) : Float64?
  return if b == 0
  a / b
end

p div?(10, 2)
p div?(10, 0)

def div(a : Float64, b : Float64) : Float64 | String
  return "Division by zero" if b == 0
  a / b
end

p div(10, 2)
p div(10, 0)

Contributing

  1. Fork it (https://github.com/initdc/types.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • initdc - creator and maintainer

About

Bring the Rust [Result Option] types to Crystal

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published