Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpolated strings in F#

I am trying to use the Entity Framework Core interpolated SQL query function in F# which requires a FormattableString. However to my surprise it doesn't function as I cannot find a way to convert a regular F# string to that type. I figured just doing what you do in C# would work but it doesn't. Here is the code I currently have:

let fromDbUser (u : Entity.User) = 
    {
        name = u.Name
        age = u.Age
        phone = u.Phone
    }

let mname = "Foo" 

let ctx = new Entity.DatabaseContext()
ctx.User.FromSqlInterpolated($"Select * FROM User Where name = {mname};") 
|> Seq.map(fromDbUser)
|> printfn "%A"

Running that block of code yields a compile error:

This token is reserved for future use

I have been trying to google around but I was unable to find any way to get this to work, any help would be most appreciated!

like image 670
manywows Avatar asked Feb 07 '20 20:02

manywows


People also ask

What is f {} in Python?

The f-string was introduced(PEP 498). In short, it is a way to format your string that is more readable and fast. Example: The f or F in front of strings tells Python to look at the values inside {} and substitute them with the values of the variables if exist.

What is interpolation string?

In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

How is string interpolation performed?

String interpolation is a process substituting values of variables into placeholders in a string. For instance, if you have a template for saying hello to a person like "Hello {Name of person}, nice to meet you!", you would like to replace the placeholder for name of person with an actual name.


Video Answer


3 Answers

When this was asked, F# didn't have string interpolation.

Today though, there is an RFC for it that is merged in in F# 5.0 allowing string interpolation in F#.


The error was because the $ symbol is reserved (for 6 years+ as of writing), and will probably be used for string interpolation when it is added.

like image 98
DaveShaw Avatar answered Oct 20 '22 10:10

DaveShaw


As Dave pointed out, interpolation isn't implemented yet. But for methods that absolutely require an FormattableString or an IFormattable, you can use FormattableStringFactory.Create

let query (sql: FormattableString)  = 
    printfn "%s" (sql.ToString(null, null))

let mname = "Foo"         
let fstr = FormattableStringFactory.Create("Select * FROM User Where name = {0};", mname)

query fstr
like image 27
Asti Avatar answered Oct 20 '22 08:10

Asti


It's available from F# 5.0.

> let mname = "Foo" ;;
val mname : string = "Foo"

> let str = $"Select * FROM User Where name = {mname};" ;;
val str : string = "Select * FROM User Where name = Foo;"

Check this out. https://docs.microsoft.com/en-us/dotnet/fsharp/whats-new/fsharp-50#string-interpolation

like image 38
gh- Avatar answered Oct 20 '22 10:10

gh-