C++17 Init statement for if/switch
Jonathan Marler via Digitalmars-d
digitalmars-d at puremagic.com
Tue Aug 15 13:31:50 PDT 2017
On Tuesday, 15 August 2017 at 20:17:40 UTC, ag0aep6g wrote:
> On 08/15/2017 08:55 PM, Daniel Kozak wrote:
>> C++17 will have this feature:
>> https://tech.io/playgrounds/2205/7-features-of-c17-that-will-simplify-your-code/init-statement-for-ifswitch
>>
>>
>> What do you think about it, can we have something similar to
>> that.
>> Maybe something like this:
>>
>>
>> int func() return 5;
>>
>> with(auto x = func()) if (condition(x))
>> do_something_with(x);
>> else
>> do_something_else_with(x);
>
> Previous discussion:
> http://forum.dlang.org/post/vfjlpvpwuyfqoljvpkkw@forum.dlang.org
>
> Andrei doesn't like the C++ way, but he seems to be ok with the
> `with` way:
> http://forum.dlang.org/post/oktru0$159b$1@digitalmars.com
>
> Walter didn't comment.
>
> I think this has a good chance of getting in, but someone will
> have to write a DIP and implement it.
I wouldn't mind writing a DIP but I haven't seen enough benefit
to warrant the change yet.
I grep'd for 'if' statements in a couple phobos modules and
didn't see too many places it could be used there.
The syntax does remind me of Andrei's scoped imports proposal:
with(import std.stdio : File) void foo(File file)
{
//...
}
I think if that proposal gets accepted it would be a natural
extension to allow both imports and declarations inside a 'with'
expression and it would get more exposure/usage among the greater
community. Without alot of usage, it will just be an esoteric
construct that looks confusing to the average developer. So I
guess it depends on whether or not you think it would gain
widespread adoption?
More information about the Digitalmars-d
mailing list