mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 20:11:17 +00:00
37 lines
586 B
Plaintext
37 lines
586 B
Plaintext
module std::collections::maybe{Type};
|
|
import std::io;
|
|
|
|
struct Maybe (Printable)
|
|
{
|
|
Type value;
|
|
bool has_value;
|
|
}
|
|
|
|
fn usz! Maybe.to_format(&self, Formatter* f) @dynamic
|
|
{
|
|
if (self.has_value) return f.printf("[%s]", self.value);
|
|
return f.printf("[EMPTY]");
|
|
}
|
|
|
|
fn void Maybe.set(&self, Type val)
|
|
{
|
|
*self = { .value = val, .has_value = true };
|
|
}
|
|
|
|
fn void Maybe.reset(&self)
|
|
{
|
|
*self = {};
|
|
}
|
|
|
|
fn Maybe value(Type val)
|
|
{
|
|
return { .value = val, .has_value = true };
|
|
}
|
|
|
|
const Maybe EMPTY = { };
|
|
|
|
macro Type! Maybe.get(self)
|
|
{
|
|
return self.has_value ? self.value : SearchResult.MISSING?;
|
|
}
|