fslang-suggestions icon indicating copy to clipboard operation
fslang-suggestions copied to clipboard

Allow use Expression<> and/or Func<> on Bind / For computations

Open lucasteles opened this issue 2 years ago • 1 comments

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

type EFQueryBuilder() =

    // don't work
    member _.For(m: IQueryable<'t>, f: Expression<Func<'t, IEnumerable<'u>>>) : 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()

    [<CustomOperation("select", AllowIntoPattern = true)>]
    member _.Select(m: IQueryable<'t>, [<ProjectionParameter>] f: Expression<Func<'t, 'u>>) : 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) and I have searched stackoverflow for discussions of this issue
  • [x] I have searched both open and closed suggestions on this site 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.

lucasteles avatar Jul 31 '23 18:07 lucasteles

You can make the single table query working like this

type EFQueryBuilder() = 
    member _.For (source: IQueryable<'T>, body: 'T -> _) : IQueryable<'T> =
        source

    [<CustomOperation("select",MaintainsVariableSpace=true,AllowIntoPattern=true)>] 
    member _.Select (source, [<ProjectionParameter>] projection: Expression<Func<'T,'U>>) =
        Queryable.Select(source, projection)
    
    [<CustomOperation("where",MaintainsVariableSpace=true)>] 
    member _.Where (source, [<ProjectionParameter>] projection: Expression<Func<'T,_>>) =
        Queryable.Where(source, projection)

    [<CustomOperation("orderBy",MaintainsVariableSpace=true)>] 
    member _.OrderBy (source, [<ProjectionParameter>] keySelector: Expression<Func<'T,_>>) =
        Queryable.OrderBy(source, keySelector)

    member _.Yield (value: 't) =
        Seq.singleton value

EFQueryBuilder() {
    for i in db.Blogs do
        where (i.BlogId > 1)
        select $"{i}"
}

ijklam avatar Dec 24 '23 14:12 ijklam