Skip to content

Allow nested triple-quote interpolation strings #14535

Closed as not planned
Closed as not planned
@inouiw

Description

@inouiw

Is your feature request related to a problem? Please describe.
The problem is described here: fable-compiler/Fable#3319
In short, a new feature in the F# Fable compiler allows using JSX syntax in triple-quote interpolation strings (see https://fable.io/blog/2022/2022-10-12-react-jsx.html), however nesting JSX is not possible.
If nesting triple-quote interpolation strings would be supported by the F# compiler then it would be possible to create complex UIs using the JSX syntax and F# for inline event handlers and for all other code. Without nested strings the new JSX feature has limited use for me.

A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
See above.

Describe the solution you'd like
All 3 examples below should compile, or at least examples 1 and 3.

Example 1

let nestedOnce = $"""
    {
        $"""
        {5}
        """
    }
    """

Example 2 (currently I have no use case for nesting twice but it would be nice for consistency if it would also work)

let nestedTwice = $"""
    {
        $"""
        {
            $"""
            {5}
            """
        }
        """
    }
    """

Example 3

open Fable.Core
[<JSX.Component>]       
let JsxInJsx () =
    JSX.jsx
        $"""
        <div>
          {
            [for x in 0..2 ->
              JSX.jsx
              // error FS3374: Invalid interpolated string. Triple quote string literals may not be used in interpolated expressions.
                $""" 
                <div>{x}</div>
                """]
          }
        </div>
        """

A triple-quote interpolation string should be treated as any other expression within a triple-quote interpolation string and just be compiled.

Describe alternatives you've considered

Alternative 1
Use non triple-quoted strings inside a triple-quote interpolation string. --> Disadvantages: 1. It is only a solution for nesting one level because it is not allowed to nest a single-quote interpolation string within another. 2. There is unfortunately no syntax highlight by the VsCode extension mentioned in the article https://fable.io/blog/2022/2022-10-12-react-jsx.html in that case.

Alternative 2
Concat strings. This seems to work but requires to join strings to make the example work, which will make the UI code ugly. Additionally, there is only syntax highlight for one part of the string.
image

Alternative 3
Introduce a new filetype similar to .tsx as .fsjsx that allows defining JSX code next to F# code. This would be even better than the proposed change. The advantages to the proposed change are first, no need to start JSX code with JSX.jsx $""" and second, no need to escape { and } characters.
There is an in interesting argument in this link from facebook why it is not good to embed JSX in a template, the text says "Unfortunately the syntax noise is substantial when you exit in and out of embedded arbitrary ECMAScript expressions with identifiers in scope".

Additional context

If the issue is about:
String interpolation. See https://github.com/fsharp/fslang-design/blob/main/FSharp-5.0/FS-1001-StringInterpolation.md

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions