Named constructors
Neia Neutuladh
neia at ikeran.org
Thu Jan 10 16:30:09 UTC 2019
On Thu, 10 Jan 2019 13:53:40 +0000, Atila Neves wrote:
> What's the _real_ difference between `someFunc(firstName="Alice")` and
> `someFunc(FirstName("Alice"))`?
> The former saves one character?
It means you don't need to declare a new struct for every function
parameter.
Even if you are willing to declare a new struct for every function
parameter, it means you don't have to do the work of either creating a
unique name for each one (which would be annoying to users) or manually
garbage collecting the structs as your code evolves (in case a parameter
struct is used by multiple functions).
Even if you're willing to do the work of declaring a new struct for each
paramater, using short names, and manually garbage collecting them,
language-provided named parameters mean that your end users don't need to
worry about name collisions between two modules using struct wrappers as
named parameters.
Even if you're willing to do the work of declaring a new struct for each
parameter, with short names and manually maintaining a minimal set of
parameter structs, and you're willing to make your code's consumers deal
with naming conflicts, you still don't get reorderable parameters. For
that, you need to turn a function into a wrapper template that accepts
parameters in any order and forwards them to an implementation function in
a fixed order. This makes it difficult to take the address of the function.
So, no, just using wrapper structs is not a serious alternative.
More information about the Digitalmars-d
mailing list