Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of enumify #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Implementation of enumify #9

wants to merge 3 commits into from

Conversation

iK4tsu
Copy link
Contributor

@iK4tsu iK4tsu commented May 24, 2021

A core feature of Taurus lib. This new type enumify is similar to a dynamic enum. Basically an enum with types that can change at runtime. This is influenced by Rust's enums.

Basic usage:

import std.taurus.enumify;

// to define a new type
struct Option(T) {
	@Member!("Some", T)
	@Member!("None")
	mixin enumify;
}

Usage:

void main() {
	Option!int o = Option.Some(4);

	assert(o.match!(
		(Option!int.some) => true,
		(Option!int.none) => false
	));

	o = Option.None;

	assert(o.match!(
		(Option!int.some) => false,
		(Option!int.none) => true
	));
}

Types can created inside scopes must be static:

import std.taurus.enumify;

void main() @safe pure nothrow @nogc {
	static struct Option(T) {
		@Member!("Some", T)
		@Member!("None")
		mixin enumify;
	}

	Option!string o = Option.None;
}

This works with @nogc and BetterC!

Values can be accessed inside match.

static struct Option(T) {
	@Member!("Some", T)
	@Member!("None")
	mixin enumify;
}

Option!string o = Option.Some("foo");
	
o.match!(
	(Option!string.some s) { assert(s == "foo"); }
	(_) { assert(false); }
);

@iK4tsu iK4tsu added enhancement New feature or request help wanted Extra attention is needed core labels May 24, 2021
@iK4tsu iK4tsu requested a review from ljmf00 May 24, 2021 12:37
@iK4tsu iK4tsu mentioned this pull request May 28, 2021
@iK4tsu iK4tsu linked an issue May 28, 2021 that may be closed by this pull request
source/taurus/enumify/package.d Outdated Show resolved Hide resolved
source/taurus/enumify/package.d Outdated Show resolved Hide resolved
source/taurus/enumify/package.d Outdated Show resolved Hide resolved
@iK4tsu iK4tsu force-pushed the feature-enumify branch from c97e57a to d3e67e8 Compare June 2, 2021 14:23
@iK4tsu iK4tsu marked this pull request as ready for review June 2, 2021 14:52
@iK4tsu iK4tsu requested a review from ljmf00 June 2, 2021 14:52
@ljmf00
Copy link
Member

ljmf00 commented Jun 3, 2021

LGTM, but we need to fix the CI first, by updating the pinning version to the latest. Also, don't push personal branches on the upstream repository https://github.com/taurus-d/taurus/branches. Please use your fork instead.

@iK4tsu
Copy link
Contributor Author

iK4tsu commented Jun 3, 2021

LGTM, but we need to fix the CI first, by updating the pinning version to the latest. Also, don't push personal branches on the upstream repository taurus-d/taurus/branches. Please use your fork instead.

I forgot to fork this repo. Do we merge this directly (after the CI fix), or do I make a new duplicated PR but from my fork?

@ljmf00
Copy link
Member

ljmf00 commented Jun 3, 2021 via email

Signed-off-by: João Lourenço <jlourenco5691@gmail.com>
@iK4tsu iK4tsu force-pushed the feature-enumify branch from 6b8f0d3 to 15a6f0e Compare June 10, 2021 16:32
iK4tsu added 2 commits June 10, 2021 18:02
Signed-off-by: João Lourenço <jlourenco5691@gmail.com>
Signed-off-by: João Lourenço <jlourenco5691@gmail.com>
@iK4tsu iK4tsu force-pushed the feature-enumify branch from 15a6f0e to 9a5c622 Compare June 10, 2021 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enumify
2 participants