digitalmars.D.learn - mixins
- Abby (21/21) Nov 17 2021 Hello I would like to create validation mixin or mixin template
- Tejas (34/55) Nov 17 2021 This good enough?
- Tejas (26/87) Nov 17 2021 Wait a minute
- Paul Backus (24/45) Nov 17 2021 Sadly this can't be done with a template mixin, because the body
- Alexey (32/53) Nov 21 2021 I'll add My 5 cents: with aliases this a little bit flexibilier,
Hello I would like to create validation mixin or mixin template which would return on error. Something like this: ``` mixin template Validate(a, b) { if(a > b) { writeln("invalid input"); return false; } } bool test(a,b) { mixin Validate!(a,b); ---- on valid code ---- return true; } ```
Nov 17 2021
On Wednesday, 17 November 2021 at 14:51:58 UTC, Abby wrote:Hello I would like to create validation mixin or mixin template which would return on error. Something like this: ``` mixin template Validate(a, b) { if(a > b) { writeln("invalid input"); return false; } } bool test(a,b) { mixin Validate!(a,b); ---- on valid code ---- return true; } ```This good enough? ```d mixin template Validate(T) { import std.stdio:writeln; bool Validate(T a, T b){ if(a > b) { writeln("invalid input"); return false; } else{ writeln("Valid input"); return true; } } } bool test(int a, int b) { mixin Validate!(int); /+---- on valid code ----+/ if (Validate(a,b)) return true; else return false; } void main(){ test(100,300); test(300,100); } ```
Nov 17 2021
On Wednesday, 17 November 2021 at 15:12:50 UTC, Tejas wrote:On Wednesday, 17 November 2021 at 14:51:58 UTC, Abby wrote:Wait a minute You literally just want to template the entire function Why are you using mixin templates? ```d template Validate(T) { import std.stdio:writeln; bool Validate(T a, T b){ if(a > b) { writeln("invalid input"); return false; } else{ writeln("Valid input"); return true; } } } void main(){ Validate(100,300); Validate(300,100); } ``` This is enoughHello I would like to create validation mixin or mixin template which would return on error. Something like this: ``` mixin template Validate(a, b) { if(a > b) { writeln("invalid input"); return false; } } bool test(a,b) { mixin Validate!(a,b); ---- on valid code ---- return true; } ```This good enough? ```d mixin template Validate(T) { import std.stdio:writeln; bool Validate(T a, T b){ if(a > b) { writeln("invalid input"); return false; } else{ writeln("Valid input"); return true; } } } bool test(int a, int b) { mixin Validate!(int); /+---- on valid code ----+/ if (Validate(a,b)) return true; else return false; } void main(){ test(100,300); test(300,100); } ```
Nov 17 2021
On Wednesday, 17 November 2021 at 14:51:58 UTC, Abby wrote:Hello I would like to create validation mixin or mixin template which would return on error. Something like this: ``` mixin template Validate(a, b) { if(a > b) { writeln("invalid input"); return false; } } bool test(a,b) { mixin Validate!(a,b); ---- on valid code ---- return true; } ```Sadly this can't be done with a template mixin, because the body of a template mixin is only allowed to contain *declarations*, not *statements*. However, it can be done with a string mixin: ```d enum string Validate(string a, string b) = q{ if (} ~ a ~ q{ < } b ~ q{) { writeln("invalid input"); return false; } }; bool test(int a, int b) { mixin(Validate!("a", "b")); // on valid code return true; } ``` The `q{...}` syntax is a [token string][1], a special kind of string literal designed specifically for putting D code in a string. [1]: https://dlang.org/spec/lex.html#token_strings
Nov 17 2021
On Wednesday, 17 November 2021 at 14:51:58 UTC, Abby wrote:Hello I would like to create validation mixin or mixin template which would return on error. Something like this: ``` mixin template Validate(a, b) { if(a > b) { writeln("invalid input"); return false; } } bool test(a,b) { mixin Validate!(a,b); ---- on valid code ---- return true; } ```I'll add My 5 cents: with aliases this a little bit flexibilier, but without temporary function declaration this doesn't work ```D import std.stdio; mixin template validation(alias a, alias b) { mixin( q{ bool x(){ writeln("a: ",a, " b: ", b); if (a > b) { writeln("invalid input"); return false; } return true; } } ); } bool test(int a, int b) { mixin validation!(a, b); if (!x()) return false; return true; } void main() { writeln("test result: ", test(2,1)); } ```
Nov 21 2021