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

An attribute enforcing usage (in F# code) of named parameters at callsite #414

Open
cloudRoutine opened this issue Oct 12, 2016 · 0 comments

Comments

@cloudRoutine
Copy link
Owner

Idea 7119363: An attribute enforcing usage (in F# code) of named parameters at callsite

Status : open
Submitted by Gauthier Segay on 2/19/2015 12:00:00 AM
4 votes

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: []
suppose we have this code (for sake of showing the idea):
type Foo() =
member x.Bar(b, a) = (a, b)
let foo = new Foo()
foo.Bar(1.00, 0.00)
foo.Bar(a = 1.00, b = 0.00)
now suppose we have this slight change in the code (just swapped the parameter names)
type Foo() =
member x.Bar(b, a) = (a, b)
this would not produce the expected result
foo.Bar(1.00, 0.00)
With the proposed feature:
type Foo() =
[]
member x.Bar(b, a) = (a, b)
foo.Bar(1.00, 0.00) // doesn't compile, expects usage of parameter names
Another example is with FSharp.Data.SqlClient's SqlCommandProvider, which defines methods taking parameters based on their order of appearance in the sql code.

samplesql1.sql

declare @numerator float
declare @Denominator float
set @numerator = @A
set @Denominator = @b
select @numerator / @Denominator

samplesql2.sql

declare @numerator float
declare @Denominator float
set @Denominator = @b // swapped this line, didn't change anything else
set @numerator = @A
select @numerator / @Denominator

sample1.fsx

r @"..\tools\nuget\packages\FSharp.Data.SqlClient\lib\net40\FSharp.Data.SqlClient.dll"

open FSharp.Data
type divideInSql = SqlCommandProvider< "sample1.sql", YourConnectionString>
let cmd = new divideInSql()
cmd.Execute(0.0, 1.0) // work but wait bellow (don't want that to compile)
cmd.Execute(a = 0.0, b = 1.0) // works
cmd.Execute(b = 1.0, a = 0.0) // works

sample2.fsx

r @"..\tools\nuget\packages\FSharp.Data.SqlClient\lib\net40\FSharp.Data.SqlClient.dll"

open FSharp.Data
type divideInSql = SqlCommandProvider< "sample2.sql", YourConnectionString>
let cmd = new divideInSql()
cmd.Execute(0.0, 1.0) // fails divide by 0 (don't want that to compile)
cmd.Execute(a = 0.0, b = 1.0) // works

cmd.Execute(b = 1.0, a = 0.0) // works

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.

Archived Uservoice Comments

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

1 participant