named arguments (C++) - Something D could learn from

aliak something at something.com
Tue Dec 18 11:08:43 UTC 2018


On Tuesday, 18 December 2018 at 10:44:28 UTC, Atila Neves wrote:
> On Monday, 17 December 2018 at 21:11:03 UTC, JN wrote:
>> On Sunday, 16 December 2018 at 22:16:28 UTC, Matthew OConnor 
>> wrote:
>>>
>>> I am a C++ developer by day and reading that article made me 
>>> cringe. C++ has this creeping, expansive, everything ends up 
>>> in the language or writable in it, if you accept awful 
>>> messages and contorting the syntax. I hope D doesn't go down 
>>> that route.
>>
>> Well that's the problem. If it was implemented as a language 
>> feature it'd be a clean solution with nice syntax. But trying 
>> to force it with templates will always look ugly.
>
> IMHO, there's no point, just wrap every parameter in a struct. 
> Is there really that much of a difference between:
>
> displayCoolName(firstName = "James", lastName = "Bond")
>
> and
>
> displayCoolName(FirstName("James"), LastName("Bond"))
>
> ?
>
> It's not even fewer characters! It's not trivial but it's 
> possible to make the order not matter by using a templated 
> implementation and aliasing it to `displayCoolName`.

You also have to build it:

func toHex(r: Int, g: Int, b: Int) -> Int {
   return r << g << b;
}

As opposed to:

struct R {
   int value;
}
struct G {
   int value;
}
struct B {
   int value;
}
int toHex(R r, G g, B b) {
   return r.value << g.value << b.value;
}

That's 67 vs 146 characters. Not to mention one pollutes the 
namespace and one doesn't.


More information about the Digitalmars-d mailing list