moonbitlang / moonbit-docs

The docs of MoonBit programming language
https://moonbitlang.com/docs/syntax
871 stars 40 forks source link

String interpolation for expressions #206

Open hcschuetz opened 1 month ago

hcschuetz commented 1 month ago

MoonBit string interpolation seems to support only variables. While JavaScript supports

`${x} + ${y} = ${x+y}`

it is apparently not possible to write a MoonBit string as

"\(x) + \(y) = \(x+y)"

because expressions like x+y are not supported inside \(...).

Is there a deeper reason for this restriction? Or is the more general case planned and you just didn't get around to implement it?

peter-jerry-ye commented 1 month ago

The string interpolation is expected to be as simple as possible. We would not expect to see very complex expressions inside the string interpolation. Users can always write

let concat_x_y = x + y
"\(x) + \(y) = \(concat_x_y)"
hcschuetz commented 1 month ago

Thanks for your answer!

Of course it is your language and therefore your design decisions, but to me the restriction feels a bit arbitrary. With the same justification one could remove support for parentheses in expression because

3 * (4 + 5)

can always be replaced with

let plus_4_5 = 4 + 5
3 * plus_4_5

But of course one would not impose such a restriction even though it would make expressions simpler.

Or is it about the simplicity of the compiler? (It is simpler if the scanner can handle string interpolations without involving the parser.)

I'm just wondering: Overall MoonBit seems to be a quite powerful language with many goodies from ML and other languages, but suddenly in one particular place the simplicity of the language beats the simplicity of programs.

Frank-III commented 1 month ago

hi, I am also wondering if we could make it a little bit more powerful. Access struct field could be very helpful and convenient:

struct Foo {
  bar: String
}

let fooo = { bar: "Hi"}
println("Foo is foo: \(foo.bar)")