On Tuesday, 15 January 2019 at 11:14:54 UTC, John Burton wrote:
As an example let's say I have a type 'Window' that represents a win32 window. I'd like to be able to construct an instance of the type with some optional parameters that default to some reasonable settings and create the underlying win32 window.

I'd ideally like some syntax like this :-

auto window = Window(title = "My Window", width = 1000, fullscreen = true);

Assume that title, width, fullscreen are optional and if not specified there are defaults to use. And that there are many other settings than just these 3 that I've chosen to just use the default here.

I know that I can't do it like this is D but what is the best way to achieve this kind of thing? I can add properties and then do a specific "create" function to create the underlying win32 window once I'm done but that seems ugly.

auto window = Window();
window.title = "My Window";
window.width = 1000;

This is ok, but I'm not so keen on separating the creation and construction like this.
Is there a better way that's not ugly?

Let me throw this idea here:

struct Config
        string title;
        int width;

struct Window
        this(Config config)
                //use static foreach magic to set everything :P

auto NewWindow( alias code )()
        mixin("Config config = {"~code~"};");
        return Window(config);

auto a = NewWindow!q{ title : "MainTitle" };
auto b = NewWindow!q{ title : "MainTitle", width : 800 };
auto c = NewWindow!q{ width : 1000 };
auto d = NewWindow!q{};


Reply via email to