fsharp / fsharp/fslang-suggestions

All iterated ComputationExpressions (CE returning CE builders)

Open
#799 9 comments 2 reactions 0 assignees View on GitHub
approved-in-principle area: computation-expressions
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

I propose we change the associativity for ComputationExpression builders. This is only relevant in extreme corner cases where CE builders return other CE builders in their `Run` method.
This currently prohibits the use of *chained* CEs like:

```fsharp
// currenlty not compiling
node { "a", "somevalue" } {
leaf { "b", "foo" }
leaf { "b", "foo" }
}
```

Here's a more complete example including the relevant parts of the builders:

```fsharp
type Node = Node of Map * list

type SubNodeBuilder(attributes : Map) =
// ...
member x.Yield(node : Node) = [node]
member x.Run f = Node(attributes, f)

type NodeBuilder() =
// ...
member x.Yield(name : string, value : string) = Map.ofList [name, value]
member x.Run f = SubNodeBuilder(f)

type LeafBuilder() =
// ...
member x.Yield(name : string, value : string) = Map.ofList [name, value]
member x.Run f = Node(f, [])

let node = NodeBuilder()
let leaf = LeafBuilder()

// currenlty not compiling
node { "a", "somevalue" } {
leaf { "b", "foo" }
leaf { "b", "foo" }
}

// currently braces are needed to ensure that the
// expression gets interpreted correctly.
(node { "a", "somevalue" }) {
leaf { "b", "foo" }
leaf { "b", "foo" }
}

```

Complete code can be found at https://gist.github.com/krauthaufen/199ff3a620fbc820dcf2f2d2174bdce5

The existing way of approaching this problem in F# is by not using CE builders like that.

## Pros and Cons

The advantages of making this adjustment to F# are that embedding custom languages for e.g. HTML-DOMs would get more flexible.
Together with implicit yields these EDSLs would syntactically look very similar to their current `list` based counterparts.

The disadvantages of making this adjustment to F# are that code may break that relies on the current interpretation of this, although I'm not sure if there actually is a way of using this for good.

## Extra information

Estimated cost XS

## 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 which 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:
* [ ] 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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.