Skip to content

kslng/ksl-maybe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

KSL Maybe Library

A library for adding "nullability" to KSL, similar to Rust's Option<T>.

Using

The KSL maybe library adds a single template'd struct called Maybe. As you may have guessed, it might contain a value. It's helpful for things like getting an element from an array when it might not be there.

Since KSL as a language tries to avoid undefined behavior as much as possible, any kind of nullable variable can't exist by default in the language. This is a kind of hacky work around.

fn main() -> void {
	// Create a `Maybe` w/o a value
	Maybe<int> maybe_not_an_int = Maybe<int>();

	// Create a `Maybe` w/ a value
	Maybe<int> maybe_has_an_int = Maybe<int>(4);

	maybe_not_an_int.has_value(); // false
	maybe_has_an_int.has_value(); // true

	/*
	 * This will crash, unfortunately there's
	 * no way around it currently :(
	 */
	maybe_not_an_int.get_value();

	/*
	 * Will return the value contained within
	 * the `Maybe`, so in this case, `4`
	 */
	maybe_has_an_int.get_value();
}

You can even use it as a return type from a function!

struct Vector2 {
	int x,
	int y
}

fn my_helper_function() -> Maybe<Vector2> {
	/* ... */
}

Notes

The maybe library intentionally works around some of the nullability restrictions set in place by KSL. Although it's officially supported, we still suggest you use an abundance of caution when building with Maybe<T>.

Maybe uses an array internally, which means that it may have slightly more overhead than expected.

Legal

KSL maybe is copyright © of kgsensei and the KSL team.

About

A library for adding "nullability" to KSL, similar to Rust's Option<T>.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Contributors