Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

min/max <? / >? expressions #20624

Open
TurkeyMan opened this issue Dec 30, 2024 · 5 comments
Open

min/max <? / >? expressions #20624

TurkeyMan opened this issue Dec 30, 2024 · 5 comments

Comments

@TurkeyMan
Copy link
Contributor

This bugs me every few weeks for decades... I still want this just as much as the first say it ever came up for me:

It's really annoying to import a module for min/max, it feels so 'heavy' for such a trivial operation. It's also annoying that they're functions; you have to step-in/step-out etc while debugging code, and they are so frequently used in hot-loops, while calling functions in debug/unoptimised code just slows it down needlessly, even more than unoptimised code already it...

C++ has a non-standard language extension, and I think it's also present in C# that adds a min/max operator. It is this:
a <? b and a >? b, which simply expands a < b ? a : b and a > b ? a : b.
I desperately want these in D... they're just so nice and readable, don't require bulky imports, efficient in debug builds, nicer when working in the debugger, etc...

I know these are all minor nuisances, but the practical result is that I very rarely actually import min/max, and almost always just write the expression out a < b ? a : b, which damages code readability, particularly when a or b are long-ish expressions.

This could be implemented with trivial lowering. It's lame when other languages have nice QOL features that we don't have in D, especially like this which are just cosmetic and pose no threats.

@TurkeyMan
Copy link
Contributor Author

#20626 ...just trying it on!

@Herringway
Copy link
Contributor

Maybe it's because I don't worship at the altar of C++, but I don't see how these messes of random punctuation are more readable than the status quo.

@TurkeyMan
Copy link
Contributor Author

TurkeyMan commented Dec 30, 2024

Suggesting that x+y*z < object.method(x) ? x+y*z : object.method(x) is a shit thing to read is not worshipping the alter of C++... and it's not even in C++!
I feel like you didn't actually read the text above...

@Herringway
Copy link
Contributor

Suggesting that x+y*z < object.method(x) ? x+y*z : object.method(x) is a shit thing to read is not worshipping the alter of C++... and it's not even in C++! I feel like you didn't actually read the text above...

I feel like you didn't read my post, either. x+y*z <? object.method(x) is ugly. Why the question mark? Is it supposed to be meaningful or was it just a symbol that happened to be available? We should copy the syntax verbatim from a C++ extension because... why, exactly?

@TurkeyMan
Copy link
Contributor Author

The question mark because it's short-hand for the ?: operator which eliminates the repetition:

x < y ? x : y => x <? y

You don't need to repeat x and y twice...
If you can't see how the question mark is obviously logical... 🤷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants