Compile time opAssign/@property constraints
uplink.coder at googlemail.com
Fri Jan 4 11:52:44 UTC 2019
On Friday, 4 January 2019 at 11:45:24 UTC, Jacob Shtokolov wrote:
> On Friday, 4 January 2019 at 10:34:07 UTC, Basile.B wrote:
>> Show us some code.
> Here is the simple example:
> The thing I'm trying to do is to make an experimental port (for
> education purposes) of https://github.com/fthomas/refined
> library for Scala, which allows to set constraints on basic
> types like numeric, bool, string, etc.
> For example, you can force an integer variable to take a range
> between 0 and 15. And if constraint is not satisfied, you get a
> compile time error.
> There is no predicate in my example, but even if I add one
> (using alias template parameter), it shows the same error.
> So is that possible in D?
You have'd to use a template to "construct" your variables;
alias val this;
template makeConstrainedInt(int Value)
static assert(Value <= 15 && Value >= 0);
enum makeConstrainedInt = ConstrainedInt(Value);
However this relies on your virtue not to call constraintInt
and always use the template.
More information about the Digitalmars-d-learn