Named constructors

JN 666total at wp.pl
Thu Jan 10 17:47:32 UTC 2019


On Thursday, 10 January 2019 at 16:22:27 UTC, H. S. Teoh wrote:
> These days, whenever my functions start needing so many 
> optional arguments that this becomes a problem, I just start 
> using a struct to encapsulate them instead. I wouldn't claim 
> it's the perfect solution, but it does pretty much have 
> equivalent functionality: you can pass values in order 
> (MyArgs(...)), or pass them by name (MyArgs args; args.field1 = 
> 123;), and there can be default arguments (default field 
> values). It does need a bit more typing, but other than that it 
> works pretty well.

This is a viable solution. It works for many usecases. APIs like 
Vulkan and Direct3D like to use the struct solution to pass 
arguments to their methods. 
https://github.com/wilzbach/DIPs/blob/b1283b455b635d7dcbc2c871d2aa47cc67190059/DIPs/DIP1xxx-sw.md would make it much sweeter to work with structs though.


More information about the Digitalmars-d mailing list