January 18, 2019
On Friday, 18 January 2019 at 09:39:31 UTC, John Burton wrote:
> It likely is a bad idea for a small struct like this but if it was much bigger would it makes sense to write this as :-
>
> 	this(const ref Config config)
>
> Which is what you might do in C++ or does D handle this differently?

For big Config struct it probably doesn't matter, but ref parameters don't accept rvalues.
January 18, 2019
On Fri, 18 Jan 2019 09:39:31 +0000, John Burton wrote:
> On Thursday, 17 January 2019 at 01:43:42 UTC, SrMordred wrote:
> 
>> struct Config {
>> 	string title;
>> 	int width;
>> }
>>
>> struct Window {
>> 	this(Config config)
> 
> It likely is a bad idea for a small struct like this but if it was much bigger would it makes sense to write this as :-
> 
> 	this(const ref Config config)
> 
> Which is what you might do in C++ or does D handle this differently?

Creating a window is the dominating cost here. If you're creating enough windows that copying them is a problem, you're doing something seriously weird and it might be more productive to step back and think about that than to switch to const ref.
January 19, 2019
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.
>
> [...]

how about this

auto With(string code,T)(T value)
{
    with(value)
    {
        mixin(code ~";");
    }
	return value;
}

auto window = Window().With!q{title = "My window",width = 800,fullscreen = true};

January 21, 2019
On Saturday, 19 January 2019 at 14:26:31 UTC, Zenw wrote:
> 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.
>>
>> [...]
>
> how about this
>
> auto With(string code,T)(T value)
> {
>     with(value)
>     {
>         mixin(code ~";");
>     }
>     return value;
> }
>
> auto window = Window().With!q{title = "My window",width = 800,fullscreen = true};

The problem with using string mixins like that is when you want to use some local variable:

int width = getWidth();
auto window = Window().With!q{width = width};

This would work:

struct Window {
    string title;
    int width;
    bool fullscreen;
}

auto With(T, Args...)(T ctx, Args args) {
    static foreach (i; 0..Args.length) {
        mixin("ctx."~Args[i].name~" = args[i].value;");
    }
    return ctx;
}

struct args {
    static opDispatch(string _name, T)(T value) {
        struct Result {
            enum name = _name;
            T value;
        }
        return Result(value);
    }
}

unittest {
    auto window = Window().With(args.title = "My window", args.width = 800, args.fullscreen = true);
    assert(window.title == "My window");
    assert(window.width == 800);
    assert(window.fullscreen == true);
}

However, I don't see that there's all that much gain compared to just assigning the fields the normal way.

--
  Simen
January 21, 2019
On Monday, 21 January 2019 at 07:57:58 UTC, Simen Kjærås wrote:
> On Saturday, 19 January 2019 at 14:26:31 UTC, Zenw wrote:
>> On Tuesday, 15 January 2019 at 11:14:54 UTC, John Burton wrote:
>>> [...]
>>
>> how about this
>>
>> auto With(string code,T)(T value)
>> {
>>     with(value)
>>     {
>>         mixin(code ~";");
>>     }
>>     return value;
>> }
>>
>> auto window = Window().With!q{title = "My window",width = 800,fullscreen = true};
>
> The problem with using string mixins like that is when you want to use some local variable:
>
> int width = getWidth();
> auto window = Window().With!q{width = width};
>
> This would work:
>
> struct Window {
>     string title;
>     int width;
>     bool fullscreen;
> }
>
> auto With(T, Args...)(T ctx, Args args) {
>     static foreach (i; 0..Args.length) {
>         mixin("ctx."~Args[i].name~" = args[i].value;");
>     }
>     return ctx;
> }
>
> struct args {
>     static opDispatch(string _name, T)(T value) {
>         struct Result {
>             enum name = _name;
>             T value;
>         }
>         return Result(value);
>     }
> }
>
> unittest {
>     auto window = Window().With(args.title = "My window", args.width = 800, args.fullscreen = true);
>     assert(window.title == "My window");
>     assert(window.width == 800);
>     assert(window.fullscreen == true);
> }
>
> However, I don't see that there's all that much gain compared to just assigning the fields the normal way.
>
> --
>   Simen

Thanks everyone for your advice on this.
I'm decided to just use a parameter structure to pass in. Works well and everyone can understand it. The question was really just does D offer a nicer way and the answer appears to be "not really". But the original approach isn't bad anyway.
1 2 3
Next ›   Last »