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

Ability to generate associated consts #194

Closed

Conversation

vic1707
Copy link
Contributor

@vic1707 vic1707 commented Nov 17, 2024

This PR makes it possible to do, inspiration taken from #35

use nutype::nutype;

#[nutype(
	validate(finite, greater = 0.0),
	consts(
		ONE = 1.0,
		TWO = 2.0_f32,
		SUM = 1.0 + 4.0,
		INVALID_LESSER = -1.0,
		INVALID_FINITE = f32::NAN,
		FROM_CONST_FN = get_static_f32(),
		// and pretty much anything else
	),
)]
pub struct FinitePositiveFloat(f32);

Tests are generated in order to ensure that those consts are valid.

TODO:
[ ] make consts only available to ints and floats. Strings simply can't. But how about any other datatypes ?
[ ] changelog & docs
[ ] any ideas ? @greyblake

@vic1707 vic1707 force-pushed the associative_consts_generation branch 4 times, most recently from da4473c to e68eb37 Compare November 17, 2024 23:37
@vic1707 vic1707 force-pushed the associative_consts_generation branch from e68eb37 to 289ad2a Compare November 17, 2024 23:53
@greyblake
Copy link
Owner

Hi, thanks for your initiative and effort, but I don't think I will be willing to merge & maintain this.
It's not the direction I want this library to move to.

I'd rather to to make const fn new possible (where it's possible), and let users to define the constant they want. They should not need to use nutype macro for that.

@greyblake greyblake closed this Nov 18, 2024
@vic1707
Copy link
Contributor Author

vic1707 commented Nov 18, 2024

I was thinking of this as a temporary solution since I don't have the skills to enable nutype to generate const fn, but I get your point 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants