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

Issue 18244: std.math generic functions need to have sig constraints #6040

Merged
merged 3 commits into from
Jan 17, 2018

Conversation

quickfur
Copy link
Member

Not having any sig constraints causes needless conflicts with user-defined numerical types, e.g.:

// myNumber.d
module myNumber;
struct MyNumber { ... }
int signbit(T : MyNumber)(T num) { ... }

// usercode.d
import myNumber;
import std.math;
MyNumber num;
auto x = signbit(num); // tries to call std.math.signbit, and fails

To anticipate the objection "user code should not use the name signbit": sometimes, we want to deliberately reuse established names for certain operations, so that we can drop in a user-defined type as a replacement for built-in floats without having to modify a lot of user code. As long as it behaves similarly enough to a built-in float, it should be allowed with a minimum of roadblocks.

@dlang-bot
Copy link
Contributor

dlang-bot commented Jan 16, 2018

Thanks for your pull request, @quickfur!

Bugzilla references

Auto-close Bugzilla Description
18244 Generic functions in std.math cannot be overloaded

@PetarKirov
Copy link
Member

@quickfur do you plan on addressing all problems mentioned in https://issues.dlang.org/show_bug.cgi?id=18244, or just signbit for this PR?

@quickfur
Copy link
Member Author

Haha, I didn't realize there was a whole list. I'll add them to this PR and update accordingly.

@quickfur quickfur changed the title std.math.signbit needs to have proper sig constraints. Issue 18244: std.math generic functions need to have sig constraints Jan 16, 2018
@quickfur
Copy link
Member Author

Fixed everything except abs. The insane sig constraints there defeated me, for the time being. I should probably save abs for another PR.

Copy link
Member

@wilzbach wilzbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If constraints need to be indented on the same level as the declaration: https://dlang.org/dstyle.html#phobos_declarations

otherwise LGTM 👍

std/math.d Outdated
@@ -5411,6 +5412,7 @@ if (isFloatingPoint!(X))
* $(D true) if $(D_PARAM x) is finite.
*/
bool isFinite(X)(X x) @trusted pure nothrow @nogc
if (isFloatingPoint!X)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If constraints need to be indented on the same level as the declaration: https://dlang.org/dstyle.html#phobos_declarations

@quickfur
Copy link
Member Author

Fixed.

H. S. Teoh added 3 commits January 17, 2018 02:26
…g constraints.

Not having any sig constraints causes needless conflicts with
user-defined numerical types, e.g.:

````
// myNumber.d
module myNumber;
struct MyNumber { ... }
int signbit(T : MyNumber)(T num) { ... }

// usercode.d
import myNumber;
import std.math;
MyNumber num;
auto x = signbit(num); // tries to call std.math.signbit, and fails
````

Add sig constraints to:

* signbit
* isFinite.
* isNormal.
* isSubnormal
* sgn.
* nextafter.
@wilzbach wilzbach changed the base branch from master to stable January 17, 2018 01:26
@wilzbach
Copy link
Member

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️
Regression or critical bug fixes should always target the stable branch.

Should we use H1 for this?

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

(rebased your PR against stable, once merge merge_stable can be run. The Bot will soon learn to do this automatically).

@wilzbach wilzbach closed this Jan 17, 2018
@wilzbach wilzbach reopened this Jan 17, 2018
@wilzbach
Copy link
Member

(jenkins seems to be unable to find this PR)

@dlang-bot dlang-bot merged commit e4da79f into dlang:stable Jan 17, 2018
@quickfur
Copy link
Member Author

Something funny is going on, dlang bot didn't seem to have updated the bug even though it recognized the bug number in the commits.

@wilzbach
Copy link
Member

Yeah Martin set the GH integration active only to master a few days ago as the duplicate mails where getting annoying and we plan to automate merge_stable soon:

image

In the mean time, we can do merge_stable manually -> #6045

@quickfur quickfur deleted the signbit_sig branch January 19, 2018 17:47
@John-Colvin
Copy link
Contributor

This broke all these functions for types with alias this to numeric types, including ones used in dstats. Is there some policy that phobos doesn't support these or was this an oversight?

@quickfur
Copy link
Member Author

quickfur commented Jan 22, 2018 via email

@MetaLang
Copy link
Member

MetaLang commented Jan 23, 2018

@quickfur it may seem like a good idea, but it's really not (see #5797). Better to have an isFloatLike template or something similar for template constraints, then define a duplicate overload.

@John-Colvin
Copy link
Contributor

@wilzbach
Copy link
Member

Another regression: https://issues.dlang.org/show_bug.cgi?id=18473

@Geod24
Copy link
Member

Geod24 commented Feb 23, 2018

Why did this even make it into a PATCH release ?
Breaking user code which have been working for years gratuitously is not nice, but in a patch release, it's definitely not okay. And why wasn't it fixed in another patch release ?
For ref, I came accross this while trying to use std_data_json: dlang-community/std_data_json#36

@quickfur
Copy link
Member Author

Yeah, it should not have been merged into a patch release when there's a regression filed against it.

@MartinNowak
Copy link
Member

It was indeed more of an enhancement than a bug fix and shouldn't have landed in stable.
Requires a bit of attention to distinguish such issues in Bugzilla.

How should we proceed? For the time being I'd consider to just revert this.

Any good idea how to test if sth. is convertible to a numeric type? This reminds me of the similar issue we had with constraints on strings that stopped working with alias this string types.

@MartinNowak
Copy link
Member

It has already been reverted #6213.

@MartinNowak
Copy link
Member

Similar to what we had to do for string constraints.
15027 – rangified functions no longer work with alias this'ed strings (e.g. DirEntry)
#3770
#3774

@quickfur
Copy link
Member Author

Is there an easy way to check if something might be related to a regression? Because @John-Colvin had already filed one after this PR was merged, before it landed in stable.

Anyway, about conversion, I'm thinking it's probably possible, but unlikely to be a simple one-liner. Probably have to factor out the test(s) into a helper template.

@don-clugston-sociomantic
Copy link
Contributor

Any good idea how to test if sth. is convertible to a numeric type?

Don't. It is always a mistake. Checking for "is it an integer or floating point type" makes no more sense than "is it a string or an integer". They look very similar to each other but actually have no non-trivial semantics in common.

Even if you restrict yourself to floating point types, one immediate problem you face is, is it losslessly convertible to a numeric type? Lossy conversion is something you didn't have to worry about with strings.
Can int be converted to double? Even that question isn't simple.

@jmdavis
Copy link
Member

jmdavis commented Mar 2, 2018

Supporting implicit conversions with templated functions is almost always a mistake. It generally causes subtle bugs, though which bugs it causes tends to depend on the types involved. Part of the problem we frequently have with that though is that a function that's not templated inherently supports various implicit conversions but in a way that the conversion is forced at the call site, and when we go and change the function so that it's templated, the semantics change even if it accepts exactly the same set of types. It doesn't look like that's the case with what happened here, but implicit conversions in generic code tend to be a nasty can of worms and are usually best avoided.

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

Successfully merging this pull request may close these issues.

10 participants