stdlib: Add Result
and Option
types
Since the type checker sucks less now, we can think about maybe adding some features to the language. ...At some point I'd like to have the type checker clean up the index map.
This commit is contained in:
parent
8ff17fd475
commit
4747b65414
@ -12,6 +12,10 @@ pub mod num;
|
||||
|
||||
pub mod str;
|
||||
|
||||
pub mod option;
|
||||
|
||||
pub mod result;
|
||||
|
||||
pub mod range;
|
||||
|
||||
#[cfg("test")]
|
||||
|
21
stdlib/std/option.cl
Normal file
21
stdlib/std/option.cl
Normal file
@ -0,0 +1,21 @@
|
||||
//! The optional type, representing the presence or absence of a thing.
|
||||
|
||||
pub enum Option<T> {
|
||||
Some(T),
|
||||
None,
|
||||
}
|
||||
|
||||
impl Option {
|
||||
pub fn is_some(self: &Self) -> bool {
|
||||
match self {
|
||||
Some(_) => true,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
pub fn is_none(self: &Self) -> bool {
|
||||
match self {
|
||||
Some(_) => false,
|
||||
None => true,
|
||||
}
|
||||
}
|
||||
}
|
@ -1,17 +1,15 @@
|
||||
//! Iterable ranges
|
||||
|
||||
type T = _;
|
||||
|
||||
/// An Exclusive Range `a .. b` iterates from a to b, excluding b
|
||||
// #[lang = "range_exc", T]
|
||||
pub struct RangeExc(T, T)
|
||||
// #[lang = "range_exc"]
|
||||
pub struct RangeExc<T>(T, T)
|
||||
|
||||
/// An Inclusive Range `a ..= b` iterates from a to b, including b
|
||||
// #[lang = "range_inc", T]
|
||||
pub struct RangeInc(T, T)
|
||||
// #[lang = "range_inc"]
|
||||
pub struct RangeInc<T>(T, T)
|
||||
|
||||
impl RangeExc {
|
||||
fn next(this: &RangeInc) -> T {
|
||||
fn next<T>(this: &RangeExc) -> T {
|
||||
(*this).0
|
||||
}
|
||||
}
|
||||
|
21
stdlib/std/result.cl
Normal file
21
stdlib/std/result.cl
Normal file
@ -0,0 +1,21 @@
|
||||
//! The Result type, indicating a fallible operation.
|
||||
|
||||
pub enum Result<T, E> {
|
||||
Ok(T),
|
||||
Err(E),
|
||||
}
|
||||
|
||||
impl Result {
|
||||
pub fn is_ok(&self) -> bool {
|
||||
match self {
|
||||
Ok(_) => true,
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
pub fn is_err(&self) -> bool {
|
||||
match self {
|
||||
Ok(_) => false,
|
||||
Err(_) => true,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user