Scripting with Variant from std.variant: parameter passing

Paul Backus snarwin at gmail.com
Fri Feb 2 20:58:12 UTC 2024


On Friday, 2 February 2024 at 20:28:50 UTC, Carl Sturtivant wrote:
> On Friday, 2 February 2024 at 19:22:22 UTC, Steven 
> Schveighoffer wrote:
>> ```d
>> // shim
>> auto foo(Args...)(Args args) if (!allSatisfy!(isVariant, Args))
>> {
>>     mixin("return foo(", argsAsVariants(args.length), ");");
>> }
>> ```
>
> Thanks for this idea. I'll work on it.

Another variation on the same theme:

```d
void foo(Variant x, Variant y)
{
     import std.stdio: writeln;
     writeln("x = ", x);
     writeln("y = ", y);
}

/// map over a variadic argument list
template mapArgs(alias fun)
{
	auto mapArgs(Args...)(auto ref Args args)
	{
		import std.typecons: tuple;
		import core.lifetime: forward;
		import std.meta: Map = staticMap;

		auto ref mapArg(alias arg)()
		{
			return fun(forward!arg);
		}

		return tuple(Map!(mapArg, args));
	}
}

import std.variant: Variant;
import std.meta: allSatisfy;

enum isVariant(T) = is(T == Variant);

auto foo(Args...)(Args args)
     if (!allSatisfy!(isVariant, Args))
{
     return .foo(mapArgs!Variant(args).expand);
}

void main()
{
     foo(123, 456);
     foo("hello", "world");
}
```


More information about the Digitalmars-d-learn mailing list