Skip to content

Conversation

@forki
Copy link
Contributor

@forki forki commented Feb 1, 2016

I "rebased" the original prototype by @v2m on the current master and made it compile.

image

@ashic
Copy link

ashic commented Feb 1, 2016

Would it be preferable to use interpolation notation similar to other languages? Common things are $var, ${var}, #var , #{var} , possibly prefixing interpolated strings with a leader character (e.g. s"foo ${bar}") in order to avoid accidental interpolation. %(foo) seems like a new notation (I could be wrong...maybe it exists somewhere).

@forki
Copy link
Contributor Author

forki commented Feb 1, 2016

@ashic I think C# syntax wasn't ready when @v2m built this. So yes making this more like C# string interpolation might be a good idea.
I just wanted to start by rebasing and bringing this back to life. Now we should discuss if we want this and how it should behave.

@forki
Copy link
Contributor Author

forki commented Feb 1, 2016

A swift sample:

let multiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// message is "3 times 2.5 is 7.5"

@isaacabraham
Copy link
Contributor

I'm split on the $"Hello {name}" C# usage - on the one hand, it's quite nice and goes well with the Console.WriteLine style syntax. On other other hand, the % style goes well with F#'s printfn style syntax. Maybe something like "Hello %{name}" would be reasonable (curly brace instead of brackets).

@isaacabraham
Copy link
Contributor

Yes sure. Frankly it's something that's easy to change anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these test should be in Compiler unit tests.
FSharp.Core shouldn't know about string interpolation.

@forki forki force-pushed the interpolation branch 2 times, most recently from ec413d0 to 384fadd Compare February 2, 2016 11:06
@forki
Copy link
Contributor Author

forki commented Feb 2, 2016

just noticed the following

type B = {
    X : string
    Y : int
};;

let b = { X = "world"; Y = 42 };;

printf "hello %(b.X)";;  // OK

printf "hello %(b.Y)";;  // crashes fsi

last line crashes at runtime with

FatalExecutionEngineError occurred
Message: Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'D:\code\visualfsharp\src\fsharp\fsi\..\..\..\Debug\net40\bin\fsi.exe'.
Additional information: Die Laufzeit hat einen schwerwiegenden Fehler entdeckt. Fehleradresse: "0x7319931f" in Thread "0x3ddc". Fehlercode: 0xc0000005. Bei diesem Fehler könnte es sich um ein Problem in der CLR oder in den unsicheren oder nicht verifizierbaren Teilen des Benutzercodes handeln. Übliche Ursachen dieses Bugs sind Marshallerfehler für COM-Interop oder PInvoke, die den Stapel beschädigen können.

seems we can't format ints.

let x = 1;;

printfn "%(x)" // crashes with NRE in printf.fs

@forki
Copy link
Contributor Author

forki commented Feb 2, 2016

OK "fixed" that NRE exception by making it the same error class like printf "hello %(b.Y)";; (FatalExecutionEngineError)

@forki
Copy link
Contributor Author

forki commented Feb 2, 2016

@7sharp9
Copy link
Contributor

7sharp9 commented Feb 2, 2016

Erm ... dunno, I not looked at the WIP on this lately :-)

@KevinRansom
Copy link
Contributor

I think a spec for this would be preferable to a prototype. It was possible to discus the general approach that was taken for C# before a line of code was written. At the very least decide if we want interpolation in string.Format, printf or both. Should we mimic the C# syntax and format specs, or use the F# syntax ... or both, give some examples of what strings using interpolation look like to illustrate arguments for and against each spec requirement.

Let's discuss the language needs for the feature before we pile in and write a ton of code. This seems like a pretty scary feature to "just evolve and debate"

Kevin

@forki
Copy link
Contributor Author

forki commented Feb 3, 2016

As already written above: language design is at https://github.com/fsharp/FSharpLangDesign/blob/master/FSharp-4.1-Proposals/StringInterpolation.md

I just rebase an existing explorative implementation on the latest master and added couple of tests to see how far it went.

@isaacabraham
Copy link
Contributor

I don't think Steffen expects this to go straight through into master - I don't see a problem with anyone prototyping a sample string interpolation if for no other reason than to get us all thinking about how this could work in the real world (if at all).

@7sharp9
Copy link
Contributor

7sharp9 commented Feb 3, 2016

@forki Nice work bringing this feature back to life.

I think this has been discussed thoroughly in the links at the top of the page, I was really disappointed that it was dropped in F#4 when it was so close to completion.

@vasily-kirichenko
Copy link
Contributor

I think it should be string interpolation (as it's in C#), not printf interpolation.

@forki
Copy link
Contributor Author

forki commented Feb 3, 2016

@vasily-kirichenko yes that would make sense as well and unify sprintf and String.Format - since it would be already interpolated at that time.

@jarlestabell
Copy link

I assume most people here know the Scala solution to this, but just in case I want to point that one out, as I think it is quite elegant.
I assume it is drastically much heavier to implement though and it may overlap a bit with type provider territory.

The key point is that it has a concise syntax and is extensible.
So the same concept can be used both for string interpolation without formatting:

val name = "James"
println(s"Hello, $name")  // Hello, James

with formatting:

val height = 1.9d
val name = "James"
println(f"$name%s is $height%2.2f meters tall")  // James is 1.90 meters tall

The above examples are taken from:
http://docs.scala-lang.org/overviews/core/string-interpolation.html

And plain SQL queries: (http://slick.typesafe.com/doc/3.1.1/sql.html#string-interpolation)

def insert(c: Coffee): DBIO[Int] =
 sqlu"insert into coffees values (${c.name}, ${c.supID}, ${c.price}, ${c.sales}, ${c.total})"

and even things like ReactJS-like JSX/TSX xml-based syntax:

xml”””
      <body>
        <a href = “some link”> ${linktext} </a>
    </body>
    ”””

(example from http://docs.scala-lang.org/sips/completed/string-interpolation.html)

@enricosada
Copy link
Contributor

@jarlestabell scala syntax and extensibility it's really interesting, StringContext it's like System.FormattableString and more like sprintf.
The extensibility is really nice to have.

@jarlestabell can you post your comment in the string interpolation discussion in f# lang design repo? fsharp/fslang-design#6 so this pr can remain focused on implementation.

@jarlestabell
Copy link

@enricosada Done, thanks! :)

@jon49
Copy link

jon49 commented Feb 18, 2016

Another option for adding formatting:

type Person = {name: string; age: int} 
let person = {name = "Robert"; age=5} 
printfn "Hi %(person, A), your age is %(person.age, 10i) with spaces or age %(person.age) without." 
printfn : unit -> string

It would be nice to keep the formatting options. Whether this way or the ways that were suggested above.

Can't wait to get this feature.

@KevinRansom
Copy link
Contributor

@forki,
@dsyme
@vladima

I'm closing this due to inactivity, if you get time to do further work on it, please reactivate.

Thanks

Kevin

@dsyme
Copy link
Contributor

dsyme commented May 27, 2016

@KevinRansom Let's keep open any WIP PRs that correspond to an approved F# RFC. Inactivity is OK for RFCs - they can take months or years to get through the system.

@dsyme dsyme reopened this May 27, 2016
@dsyme dsyme changed the title [WIP] string interpolation [WIP] RFC FS-1001 - String Interpolation Jun 13, 2016
@AviAvni
Copy link
Contributor

AviAvni commented Jul 3, 2016

@forki @dsyme Can I help with this?

@forki
Copy link
Contributor Author

forki commented Jul 6, 2016

I'm not sure if @dsyme has decided which syntax we will use? Is this
settled in RFC?
On Jul 3, 2016 16:36, "Avi Avni" [email protected] wrote:

@forki https://github.com/forki @dsyme https://github.com/dsyme Can I
help with this?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#921 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AADgNA-icGestT_6d0BEd3j5A0A2tTxNks5qR8jlgaJpZM4HQlqo
.

@vasily-kirichenko
Copy link
Contributor

I think we should just finally pick one of the syntaxes and release it. Finally.

@dsyme
Copy link
Contributor

dsyme commented Jul 6, 2016

@vasily-kirichenko @forki @AviAvni The RFC is here: https://github.com/fsharp/FSharpLangDesign/blob/master/RFCs/FS-1001-StringInterpolation.md

The basic proposed syntax is covered there. However the RFC really needs considerable work. I'd particularly recommend that someone go over all the comments in this thread, and in the RFC discussion thread, and the original codeplex thread, and capture all the remaining issues and list them at the end of the RFC. We can then resolve them one by one.

@vasily-kirichenko
Copy link
Contributor

The implementation works with string expressions only because of forki@6542316#diff-5b9ab9dd9d7133aaf23add1048742031R6583 where coercion from string to obj is performed:

mkCoerceIfNeeded cenv.g cenv.g.obj_ty cenv.g.string_ty s

What we need here is real type of each expression s. I have no idea how to get it, it seems there are no types at this stage at all.

@KevinRansom
Copy link
Contributor

Closing

@KevinRansom KevinRansom closed this Nov 8, 2016
@isaacabraham
Copy link
Contributor

@KevinRansom again - why has this been closed?

@dsyme
Copy link
Contributor

dsyme commented Nov 9, 2016

@isaacabraham Same response as here #908 (comment). The issue is tracked by an RFC which is definitely still active, and we can resubmit this work (or a variation of it) for F# 4.2

@realvictorprm
Copy link
Contributor

I'm going to rebase this on master again and just implement for test purposes the C# syntax. There's nothing particular wrong with extending this later again.

@realvictorprm
Copy link
Contributor

realvictorprm commented Sep 14, 2017

@forki do you want to give me access to your PR or should I open another separate PR?

@forki
Copy link
Contributor Author

forki commented Sep 14, 2017

it's closed. go ahead and submit your own

@realvictorprm
Copy link
Contributor

Alright then, thank Sir!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.