must
Version, currently 0.2.02 versions
- 0.2.0latestJan 27, 2020
- 0.1.0not indexedJan 27, 2020
github.com/maiha/must.cr
Library for dynamic typing in Crystal
1 stars
1 dependent
License: MIT
Installation
# Add this to your shard.yml
dependencies:
must:
github: maiha/must.cr
version: ~> 0.2.0Then run:
shards installshard.yml
- Crystal
- no constraint declared
- License
- MIT
- Author
- maiha
Dependencies
This version declares no dependencies.
README
must.cr 
Library for dynamic typing in Crystal.
Core class pollution
Object#must
Features
- Checking Types : Dynamically guarantee object types
- Casting Types : Dynamically cast object types
- Matching Features : Object matching and data extraction
# checking
1.must(Int32) # => 1
1.must(Bool) # raises Must::TypeError
# casting
1.must.cast(String) # => "1"
1.must.cast(Bool) # raises Must::CastError
# matching
708.must.match(/0/) # => 708
708.must.match(/0(.)/, "$1") # => "8"
- crystal: 0.31.1
Why not use as for casting?
aschecks types at compile timemustchecks type at runtime
json = JSON.parse(%|{"a": 1}|)
json["a"].as_f # Unhandled exception: cast from Int64 to Float64 failed (runtime)
json["a"].raw.to_f # undefined method 'to_f' for Array(JSON::Any) (compile time)
json["a"].raw.must.cast(Float64) # => 1.0
Installation
- Add the dependency to your
shard.yml:
dependencies:
must:
github: maiha/must
version: 0.2.0
- Run
shards install
Usage
require "must"
API
any?, any
Checks empty? or not. It returns true if the class doesn't have empty? method.
1.must.any? # => true
1.must.any # => 1
Set(Int32).new.any? # => false
"".must.any? # => false
"".must.any # raises Must::Error("(value) is empty")
"".must.any(name: "foo") # raises Must::Error("foo is empty")
Development
Contributing
- Fork it (https://github.com/maiha/must.cr/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- maiha - creator and maintainer
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.2.0- Tagged
- Jan 27, 2020
- Commit
b12119ede97d- Indexed
- yes
Dependents
Repository
github.com/maiha/must.cr
Metadata
- Created
- Aug 19, 2026
- Updated
- Nov 12, 2026
- Synced
- Nov 12, 2026
- Versions
- 2