You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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
The text was updated successfully, but these errors were encountered: