Two things I thought would be simple
Steven Schveighoffer
schveiguy at yahoo.com
Wed Mar 7 15:11:17 UTC 2018
On 3/7/18 9:20 AM, Shachar Shemesh wrote:
> import std.traits;
> import std.stdio;
>
> void func(int a, int b=2, int c=3) {
> return a+b+c;
> }
>
> void func2(Parameters!func args = ParameterDefaults!func) {
> return func(args);
> }
>
> void main() {
> writeln(func2(1));
> writeln(func2(1,1));
> writeln(func2(1,1,1));
> }
>
> So, this does not compile. I can do that with a mixin, but I would much
> rather not. Is there a non-Mixin method of copying not only the
> function's signature, but also its default values?
Well, you could do it with templates, but obviously that is less desirable:
void func2(Args...)(Args args) if(is(typeof(func(args)))) { return
func(args); }
> The second thing I thought would be simple is to refer to call a
> struct's member.
>
> struct S {
> void func(int a) {
> }
> }
>
> unittest {
> alias v = S.func;
This is actually a function that requires a `this` pointer. It can't be
called unless you stuff it in a delegate:
>
> S s;
> s.v(12); // No property v for type S
void delegate(int) dg;
dg.ptr = &s;
dg.funcptr = &v; // note you need the address operator
> // UFCS to the rescue!
> v(s, 12);
dg(12);
It would be nice if v(s, 12) worked. Related DIP:
https://github.com/dlang/DIPs/blob/master/DIPs/DIP1011.md
-Steve
More information about the Digitalmars-d
mailing list