A better way to deal with overloading?
Patrick Schluter via Digitalmars-d
digitalmars-d at puremagic.com
Fri Jan 27 03:16:09 PST 2017
On Thursday, 26 January 2017 at 00:02:03 UTC, Profile Anaysis
wrote:
> Many times we pass compound types(non-primitives) as arguments
> to functions.
>
> e.g.,
>
> void foo(T1 t1, T2 t2, T3, t3);
>
> But to call foo with new variables we have to create the
> arguments. This usually requires extra code to simply
> initialize the variables. (imagine foo being a constructor).
>
> It may be better to use a recursive process where we can
> specify all the values of the arguments inline.
>
> e.g.,
>
> foo(|a,b,c|,|e|,|f,g,c|).
>
> (I am using | but any type of symbolic notation could be used)
>
> would be equivalent to
>
> foo(T1(a,b,c),T2(e),T3(f,g,c)).
>
> When the T's are structs(other wise maybe new, or we can imply
> new for classes to make it uniform).
>
>
> If T1 has a compound type for the 3rd parameter, we can then
> call it like
>
> foo(|a,b,|c1,c2,3||,|e|,|f,g,c|).
>
> this avoids having to do things like
>
> auto t1 = T1(a,b,new X(c1,c2,c3));
> auto t2 = T2(e);
> auto t3 = T3(f,g,c);
>
> and then f(t1,t2,t3);
>
> or other wise simply inline the above.
>
>
> This also cuts down on constructor overloading.
>
> This is sort of liked named parameters but the idea is that the
> compiler simply constructs the type internally as it knows what
> type to expect and the grouping symbols allow one to specify
> the contents unambiguously.
It's funny (or sad) that C has compound types since C99 and that
they are good.
Your foo(|a,b,|c1,c2,3||,|e|,|f,g,c|) writes as
foo((T1){a,b,{c1,c2,c3}}, (T2){e}, (T3){f,g,c});
of course, the lack of object orientation and other things makes
it easier in C.
More information about the Digitalmars-d
mailing list