-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add an attribute enforcing the use of named parameters at callsite #414
Comments
@dsyme I have a bit of prototype code making this check work at callsite. Can this suggestion be (edit) considered for The compiler should also have checks on the method definitions, and probably fair amount of edge cases I'm not considering. |
There was one comment from @dsyme on UserVoice:
edit: I feel there is something remotely related in F# language: |
Interesting. A little unclear how F#-specific this is. There might be similar syntax in C#: there instead of parameters they have fields that you initialize using |
I'm generally in favor of this suggestion, but I think this should be implemented together with another suggestion (still not submitted) which would allow to curry the parameters by indicating its order in a succinct way. Something like Note that here's only 3 parameters, in real code this is normally much more and when you have 8 or more parameters writing that "fun" is not that fun. |
There is a roslyn analyzer doing the same: Also an implementation in Dart: @dsyme, @cartermp, any feedback wether the feature is a good fit for F#? The minimum changes don't seem to amount to a lot of code: dotnet/fsharp@main...smoothdeveloper:argument-names-enforced |
I'd personally be in favor but this is a @dsyme call |
There is another existing feature which is alike in the compiler: type [<Measure>] cm
type [<Measure>] km
let [<RequiresExplicitTypeArguments>] removeUoM<[<Measure>] 'a> (v: float<'a>) = float v
let k = removeUoM<cm> 1.<cm>
//let bad = removeUoM<cm> 1.<km>
let bad2 = removeUoM 1.<cm> // error FS0685: The generic function 'removeUoM' must be given explicit type argument(s) @dsyme is that helping making a good case for this feature? |
The C# suggestion is here: dotnet/csharplang#1005 If there is to be some attribute, it would be good to discuss with C#/.Net. It wouldn't be good if there is an F# attribute that C#/.Net consumers can't understand, and then a C#/.Net attribute down the line that F# has to implement separately. |
I think it's a little different to have an analyzer (which I would +1) rather than having an attribute to enforce the use. Let's say you create a library and use enforce (with an analyzer) this practice on your codebase then I am still able to make a choice on my side while with the attribute I would be also forced to follow this pattern. For me this is more on the coding style side and so it should not be forced to users. Besides, you could also create some record type and take this as parameter instead of multiple arguments, this would force having "named" arguments. Just my 2 cents and based on what I see from this suggestion. |
This becomes problematic for type providers, there is no provision in the SDK to be able to emit provided record types. |
Yes I think this is a good suggestion. Perhaps call the attribute |
Thanks @dsyme, adjusting the name (BTW we sadly have some inconsistency between I'm resuming the investigation, I wonder if it makes sense if @cartermp, @davidfowl or others would like to loop back with C#/BCL people, the attribute may find its way in the BCL, it may be good to get their opinion on the name too. Will fill in a RFC as I progress, I appreciate if a placeholder for it could be added so it is in the right spot / has the right name. |
I feel like python has this feature? Doesn't it? |
@davidfowl not too familiar, but section "Requiring your arguments be named" indicates it does, seemingly on argument by argument basis. edit: seems to allow for mix of it, and capturing positional arguments separately, akin to C# and F# allowing freely to use named arguments only after positional, but embedding that choice in the signature of the member. |
I would love to have this feature. My use case is working with EF Core. Ideally one could just use records for everything, but navigation properties don't play well their inability to be declared as |
@LiteracyFanatic, thanks for your feedback, please check fsharp/fslang-design#538 and https://github.com/fsharp/fslang-design/blob/main/drafts/FS-1095-requirenamedargumentattribute.md for further details. You may surface your eagerness about this feature at dotnet/runtime#51451 so it would also be picked up by roslyn analyzers and having the attribute defined in the BCL. |
Submitted by Gauthier Segay on 2/19/2015 12:00:00 AM
4 votes on UserVoice prior to migration
In some code, it's critical to have function/methods called with parameter names at call site, for readability reasons, but also for correctness. I would like F# to enforce this using an attribute for example:
[<EnforceNamedParametersAtCallSite>]
. Suppose we have this code (for sake of showing the idea):now suppose we have this slight change in the code (just swapped the parameter names)
this would not produce the expected result
foo.Bar(1.00, 0.00)
. With the proposed feature:Another example is with
FSharp.Data.SqlClient's SqlCommandProvider
, which defines methods taking parameters based on their order of appearance in the sql code.We have seen by now that change of order in a sql file provokes havoc. API design wise, some people might want to enforce that callsite will use named parameters to avoid those kind of misshaps.
I hope the description is clear enough.
Original UserVoice Submission
Archived Uservoice Comments
The text was updated successfully, but these errors were encountered: