Tarmil / FSharp.Data.LiteralProviders

F# Type providers generating literals from the compile-time environment
MIT License
135 stars 6 forks source link

Feature Request, TP based sprintf #5

Open Swoorup opened 4 years ago

Swoorup commented 4 years ago

Since this package is named literal providers, would it feasible to add sprintf like functionality using and get a string literal?

I am not entirely sure if it is possible to add specify varying arguments to TP though?

let [<Literal>] string = SPrintfTP<"Hello %s", "blalal">
Tarmil commented 4 years ago

I don't think it's possible to add varying arguments to a TP unfortunately. But I think it should be possible to make a nested class whose number of arguments (and their types) depends on the string passed at the root:

let [<Literal>] string = SPrintfFP<"Hello %s! 1 + 1 = %i">.Args<"world", 2>.Value
Swoorup commented 4 years ago

Interesting, that definitely makes sense given the lack of current support for the mechanism. My use case for this is to compose various SQL fragments as string literals and still consume it as literals so it can be fed to the SQL type provider

Tarmil commented 4 years ago

A sprintf would definitely be nicer, but in the meantime you should be able to just + the strings:

let [<Literal>] connectionString = "Hello " + Env<"MyVar">.Value + "!"