D-Scanner icon indicating copy to clipboard operation
D-Scanner copied to clipboard

opAssign return type check

Open WebFreak001 opened this issue 7 years ago • 5 comments

Returning void from opAssign should be forbidden, it should most of the time return the type itself so that code like this doesn't happen: https://github.com/dlang/phobos/commit/59520969ef73eaf0691972ee00b389e5bbc4c8fb#diff-4715499b2ff2d74e4eb3c6f3909c611cR673

else if (cast(bool)(match = diagnostic.message.matchFirst(undefinedIdentifier))
		|| cast(bool)(match = diagnostic.message.matchFirst(undefinedTemplate))
		|| cast(bool)(match = diagnostic.message.matchFirst(noProperty)))

->

source/served/extension.d(1087,30): Error: cannot cast expression match.opAssign(matchFirst(diagnostic.message, undefinedIdentifier)) of type void to bool
source/served/extension.d(1088,26): Error: cannot cast expression match.opAssign(matchFirst(diagnostic.message, undefinedTemplate)) of type void to bool
source/served/extension.d(1089,26): Error: cannot cast expression match.opAssign(matchFirst(diagnostic.message, noProperty)) of type void to bool

WebFreak001 avatar Mar 06 '18 20:03 WebFreak001

Ha. I actually wish the language had gone the other way and made = always return void for everything.

Hackerpilot avatar Mar 15 '18 11:03 Hackerpilot

if it would just automatically infer it that you want the instance of it in opAssign and properties returning void it would be far nicer, but sadly it's not doing it like that

WebFreak001 avatar Mar 15 '18 18:03 WebFreak001

I'm no sure if it's supposed to work like that. Consider this

bool b;
if (b = false){} // Error: assignment cannot be used as a condition, perhaps `==` was meant?
if (cast(bool()(b = false)){} // Error: assignment cannot be used as a condition, perhaps `==` was meant?

It means that you want to enforce something that doesn't even work for the basic types of the language.

ghost avatar Apr 03 '18 04:04 ghost

So i'd go for a close as wontfix. What do you think ?

ghost avatar Apr 03 '18 04:04 ghost

int fn() { return 5; }
int i;
if (cast(bool)(i = fn())) {} // works

WebFreak001 avatar Apr 03 '18 06:04 WebFreak001