linksplatform / Ranges

LinksPlatform's Platform.Ranges Class Library
https://linksplatform.github.io/Ranges
The Unlicense
4 stars 1 forks source link

Can we simplify this? #45

Open poul250 opened 3 years ago

poul250 commented 3 years ago

It looks verdy difficult https://github.com/linksplatform/Ranges/blob/d7b5ac881f7e8921520f5264ce04a1d97dfd31c3/cpp/Platform.Ranges/EnsureExtensions.h#L33-L43 Why do we create lambdas, recursively descend into other functions, sums a bunch of lines to check that an element is in the range? And why do we throw an exception if the element is not in the range, when we can return a boolean value? Can we simplify this?

uselessgoddess commented 3 years ago

We can write noexcept

Konard commented 3 years ago

The reason lambdas are used is lazy execution. We do not need to calculate the string until the exception is thrown. This method called Ensure::Always::ArgumentInRange the reason it exists is to ensure the condition is met. If it is not, the exception is thrown. You can always use range.Contains method if you would like to check it yourself. This method returns boolean. If you need to throw an exception - use Ensure::Always::ArgumentInRange, if you don't want to use exceptions, just don't use this method.

uselessgoddess commented 2 years ago

The reason lambdas are used is lazy execution. We do not need to calculate the string until the exception is thrown. This method called Ensure::Always::ArgumentInRange the reason it exists is to ensure the condition is met. If it is not, the exception is thrown. You can always use range.Contains method if you would like to check it yourself. This method returns boolean. If you need to throw an exception - use Ensure::Always::ArgumentInRange, if you don't want to use exceptions, just don't use this method.

Use std::string_view with static string messages for this