fsharp / fsharp/fslang-suggestions
Add an attribute enforcing the use of named parameters at callsite
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
**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: `[]`. Suppose we have this code (for sake of showing the idea):
``` fsharp
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)
``` fsharp
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:
``` fsharp
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
```
``` fsharp
#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
```
``` fsharp
#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.
**[Original UserVoice Submission](https://fslang.uservoice.com/forums/245727-f-language/suggestions/7119363)**
[Archived Uservoice Comments](../tree/master/archive/suggestion-7119363-an-attribute-enforcing-usage-in-f-code-of-name.md#comments)
Contributor guide
No contributing guide indexed for this repository
Research direction
The issue provides examples in sample1.fsx and sample2.fsx, but no compiler entry point or test file. Start by reviewing the proposal and archived UserVoice comments, then locate the compiler tests and implementation area for named-argument checking. Done means the attribute is specified, implemented, and positional calls to marked members fail while named calls continue to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fsharp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100