SMLFamily / Successor-ML

A version of the 1997 SML definition with corrections and some proposed Successor ML features added.
190 stars 10 forks source link

String interpolation #31

Open YawarRaza7349 opened 7 years ago

YawarRaza7349 commented 7 years ago

Not sure if this has been discussed before elsewhere, but I didn't see another issue on this repo about it.

It might be nice to have string interpolation available, where string literals are "interrupted" to include SML expressions that are inserted at that location. Here's an example of Swift's syntax for string interpolation, which I personally like:

"SELECT \(columnNames.joined(", ")) FROM \(tableName)"

How I'm imagining this feature is that it would only affect the grammar (and would be fully handled by the parser for implementations); an interpolated string literal would be syntactic sugar for either repeated uses of ^ or a use of String.concat.

Thoughts? Not the most important feature, but I just thought it might be kinda nice to have.

RobertHarper commented 7 years ago

Konrad Slind implemented this decades ago. Someone will, I hope, point you to the library.

(c) Robert Harper. All rights reserved.

On Mar 16, 2017, at 18:47, YawarRaza7349 notifications@github.com wrote:

Not sure if this has been discussed before elsewhere, but I didn't see another issue on this repo about it.

It might be nice to have string interpolation available, where string literals are "interrupted" to include SML expressions that are inserted at that location. Here's an example of Swift's syntax for string interpolation, which I personally like:

"SELECT (columnNames.joined(", ")) FROM (tableName)" How I'm imagining this feature is that it would only affect the grammar (and would be fully handled by the parser for implementations); an interpolated string literal would be syntactic sugar for either repeated uses of ^ or a use of String.concat.

Thoughts? Not the most important feature, but I just thought it might be kinda nice to have.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

YawarRaza7349 commented 7 years ago

Thanks. You mention a library, but there are a few advantages to having this as a language feature, such as the format string not needing to be parsed at run time (and therefore possibly being incorrect and throwing a run-time exception), and being easier to read for some people; specifying the inserted expressions after the string, rather than inside it, looks a bit clunkier (I'm assuming that's what a library function would do).

I tried searching for the library based on the info you gave, but I couldn't find it. Does it have anything to do with SML/NJ's pretty printing library? (That's the closest thing I found.)

RobertHarper commented 7 years ago

I think it's called "fragments" or similar.

Robert Harper rwh@cs.cmu.edu (c) All rights reserved

On Mar 17, 2017, at 09:05, YawarRaza7349 notifications@github.com wrote:

Thanks. You mention a library, but there are a few advantages to having this as a language feature, such as the format string not needing to be parsed at run time (and therefore possibly being incorrect and throwing a run-time exception), and being easier to read for some people; specifying the inserted expressions after the string, rather than inside it, looks a bit clunkier (I'm assuming that's what a library function would do).

I tried searching for the library based on the info you gave, but I couldn't find it. Does it have anything to do with SML/NJ's pretty printing library? (That's the closest thing I found.)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

MatthewFluet commented 7 years ago

I'm not strongly for or against string interpolation, but I've always suspected that it isn't as convenient in a language (like SML) without an implicit or overloaded toString mechanism. That is, it seems that a lot of the simple examples of string interpolation would expand to include a lot of Int.toString applications around the expressions of interest; and, when those expressions of interest are simple variables, then the Int.toString may come to syntactically dominate and lose some of the light-weight feel of string interpolation.

Related, and perhaps what @RobertHarper is referring to, is SML/NJ's quote/antiquote mechanism: http://www.smlnj.org/doc/quote.html. This generalizes string interpolation so that the type of the interpolated expression is arbitrary (but consistent); the result is a list of strings and interpolated values. Regular string interpolation would be achieved by applying String.concat o (List.map (fn QUOTE s => s | ANTIQUOTE s => s)) to a string frag.

While implementation concerns shouldn't be the final say on language design, I do find the quote/antiquote mechanism a little more appealing. It isn't very difficult to teach the compiler about special data constructors and arrange for certain syntax to expand to applications of those data constructors; we certainly do so for list expressions. It also isn't very difficult to arrange for certain syntax to expand to applications of primitive operations. However, in MLton, string concatenation is not a primitive operation -- it is just a function that happens to be implemented in the Basis Library.

YawarRaza7349 commented 7 years ago

That quote thing looks neat! Thanks for the info.

JohnReppy commented 7 years ago

I like string interpolation as a programming style (I've been doing some Swift programming lately), but, as Matthew points out, it is hard to support without either user-defined overloading or some form of subtyping.