Thread overview
mixins
Nov 17, 2021
Abby
Nov 17, 2021
Tejas
Nov 17, 2021
Tejas
Nov 17, 2021
Paul Backus
Nov 22, 2021
Alexey
November 17, 2021

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;
}
November 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?

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);
}
November 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:

>

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?

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);
}

Wait a minute

You literally just want to template the entire function

Why are you using mixin templates?

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 enough

November 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:

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, a special kind of string literal designed specifically for putting D code in a string.

November 22, 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

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));
}