fsharp / fsharp/fslang-suggestions
Allow use Expression<> and/or Func<> on Bind / For computations
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
Looks like today to create custom computation expressions it is required that the bind function to be an `FSharpFunc`, allowing it to be `Expression<>` or `Func<>` could be very handy for CLR interop
An example is creating a query builder for _EF_
```fsharp
type EFQueryBuilder() =
// don't work
member _.For(m: IQueryable<'t>, f: Expression>>) : IQueryable<'u> = m.SelectMany(f)
// works but create an wrapping expression on FSharpFunc
// member _.For(m: IQueryable<'t>, f: 't -> IEnumerable<'u>) : IQueryable<'u> = m.SelectMany(f)
member _.Yield(q) = Enumerable.Repeat(q, 1)
member _.YieldFrom(q: IQueryable<_>) = q
member _.Zero() = Enumerable.Empty()
[]
member _.Select(m: IQueryable<'t>, [] f: Expression>) : IQueryable<'u> =
m.Select(f)
let test =
EFQueryBuilder() {
for n in items do // ERROR: This function takes too many arguments, or is used in a context where a function is not expected
select $"{n}"
}
```
And actually the custom operator with `ProjectionParameter` recognize the expression as the same as any function
## Pros and Cons
**The advantages of making this adjustment to F# are** :
- easy to create CLR interop tools
- consistency on which type of function can be used on CEs definition
**The disadvantages of making this adjustment to F# are**:
- more complexity of CEs methods signatures
## Extra information
**Estimated cost (XS, S, M, L, XL, XXL):** ?
**Related suggestions:** (put links to related suggestions here)
## Affidavit (please submit!)
Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] This is not something that has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.
Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this
## For Readers
If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.
Contributor guide
No contributing guide indexed for this repository
Research direction
Review computation-expression handling for Bind/For and the ProjectionParameter custom operation example. Compare the current FSharpFunc requirement with the requested Expression<>/Func<> acceptance for the EF/IQueryable query-builder scenario. Done means the shown custom computation expression can use these CLR interop function types.
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
- Mostly clear
- Newbie friendliness
- 30/100