-
-
Notifications
You must be signed in to change notification settings - Fork 704
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
std.math: hide parameter names for common math functions #7480
Conversation
|
Thanks for your pull request and interest in making D better, @marler8997! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#7480" |
2613f44
to
c73c031
Compare
|
This seems unnecessary to me. We're trying to prevent users from having their code broken if they use named arguments for a 1 arg math function... Both parts seem unlikely: who's going to use named args to call sin? Is it really worth uglifying phobos to avoid a possible, future, easily fixed compile time error? |
Agreed. Necessary is a very high bar. Named arguments themselves aren't necessary, in fact the D language itself isn't necessary. Instead I think we should be asking whether it's an improvement.
Yes it's unlikely. But the purpose of this PR isn't to fix these 2 functions in particular, but to establish a pattern for all of druntime/phobos whenever we come across functions whose parameter names shouldn't be apart of the API.
How important are clean interfaces to you? From personal experience, clean minimal interfaces are one of the most important aspects of programming when it comes to libraries and large projects. |
In anticipation of named parameters, this is an idea to establish a pattern to keep parameters out of the API when it makes sense. Math functions that only take one parameter are a good example of functions where the name of the parameter is unhelpful to include in the function's "interface".
c73c031
to
7749e4e
Compare
Sure, and I think this pattern would make phobos less readable/maintainable. It reminds me of a C++ STL impl that has to throw underscores in front of everything. It seems like an overreaction to me that's solving a problem that is unlikely to exist
By minimal, do you mean reducing character count by omitting the parameter name? I'm not distracted by a 1 argument math function using the name I get what you're trying to present here, and appreciate it, but for me this example shows that "parameter names as part of the API" is generally not a problem. I suspect a function that takes >3 arguments would be possibly more compelling |
|
Hmm not sure about this one, but I think at the very least we would need to touch ddoc to display something nicer than _param_0 as this would be very confusing for newcomers. |
Oh no, I mean "minimal interface". Imagine adding extra parameters to a function that don't do anything, that would not be a minimal interface.
Ok I think we are mostly in agreement, we just have slightly different priorities. For me, a libarary's interface/API (the information shared between the application and the library) is one of the most important parts of a library. To me, having to type |
Still doesn't seem to work even with |
But, for 99.99% of users the interface is still |
We don't currently have a solution for the doc so it's currently not worse or better :)
Right, we removed that possibility from the function's interface.
You just showed the example that would no longer be supported. That's minimizing the function's interface. Also |
| @@ -566,7 +566,7 @@ enum real SQRT1_2 = SQRT2/2; /** $(SQRT)$(HALF) | |||
| * | |||
| * Params: | |||
| * Num = (template parameter) type of number | |||
| * x = real number value | |||
| * _param_0 = real number value | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work, and could also be confusing. We would need to figure out how to support documenting nameless parameters in ddoc.
| if ((is(Unqual!Num == short) || is(Unqual!Num == byte)) || | ||
| (is(typeof(Num.init >= 0)) && is(typeof(-Num.init)))) | ||
| { | ||
| alias x = _param_0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like _param_0 is an implementation detail rather than part of the spec. We would need to make this part of the spec before establishing a pattern that uses it I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many things that are unnamed in D get a name. That's a huge leak of abstraction, and shouldn't happen, but does due to the compiler having lacking abstractions. I had a go at solving some of the problems it introduces a bit ago but hell broke loose. But if anything, I believe we want to move away from exposing compiler-generated names, instead of relying on it.
If one want to access unnamed parameters, it should be via a traits / is expression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And regarding documentation, since an identifier cannot start with a digit, we can just use the index. It would need a fix to DDOC, that's all.
|
Why would one even use named parameter for single-argument function ? Why would one do it for unambiguous types or to unambiguous functions ? Named parameters are great for basic types ( |
|
I don't think this change makes much sense. Even if the named arguments DIP gets approved (which is by no means guaranteed), single parameter functions aren't going to be used that way unless they're badly named. If we introduce named parameters and a way to make some of them positional-only like Python, then sure. |
I'm not sure I agree. For example, I have another PR open (#7481) which changes the name of the single-parameter trigonometry functions from sin(x:90);
// vs
sin(radians:90);In that example if you understand radians you'll see they probably meant Any function that has a unit-specific parameter could find it useful, another example would be the C sleep function: sleep(3);
// VS
sleep(seconds:3);The other example are boolean flags, i.e. do_the_thing(true);
// vs
do_the_thing(enableLogging:true);When a function has alot of parameters, named parameters can help distinguish what each argument means, but there are other use cases that apply to functions with single parameters as well. |
Calling it |
Python like is probably the way to go, don't see any benefit making all parameters positional without any explicit annotation. |
I don't think that one is useful either.
I don't know what to say to someone expecting to call
Any function that takes a unit like that shouldn't take an integer. At the very least it should be renamed so that the unit is in the function name (e.g.
I agree.
If there are, those functions should be rewritten. |
|
(Responding here, even though this pertains more to #7481.)
Explicitly specifying the units can still be useful for clarity in situations where data from multiple sources/devices is mixed. In my day job, I frequently write code that (through no fault of my own) uses radians, degrees as well as turns to specify angular quantities. Why not give people the option to be explicit if they want to? It's not like anyone would want to write |
I agree, but radians aren't units.
|
In anticipation of named parameters, this is an idea to establish a pattern to keep parameter names out of the API when it makes sense. Some math functions like
floorandabsthat only take one parameter are a good example of functions where the name of the parameter is unhelpful to include in the function's "interface". This PR can serve as a place to debate the pros and cons of such a pattern, and a jumping off point for updating druntime/phobos with this pattern if it is accepted by the community.