Nim: [RFC] String interpolation

Created on 24 Mar 2017  路  14Comments  路  Source: nim-lang/Nim

From time to time the questions arise about the string interpolation. I have it done already here, more examples can be found in the library tests.

I can make a PR that will add this feature to standard library. What do you think about it?

The example:

import boost.richstring

let s = "string"
assert fmt"${s[0..2].toUpper}" == "STR"
assert fmt"${-10}%04d" == "-010"
assert fmt"0x${10}%02x" == "0x0a"
assert fmt"""${"test"}%-5s""" == "test "
assert fmt"${1}%.3f" == "1.000"
assert fmt"Hello, $s!" == "Hello, string!"
RFC Stdlib

Most helpful comment

@Yardanico If you don't have time for it, I'm happy to volunteer as well (without promises on when I get to it ;)).

All 14 comments

Found another variant of string interpolation in https://bitbucket.org/lyro/strfmt. It uses python syntax, my variant uses scala syntax. For nim maybe python syntax will be preferred

Anyway, I think that string interploation must present in the standard library.

Yes, stdlib would be nice. We already have parseutils.interpolatedStringFragments or similar that sets the syntax for us.

fmt macro uses parseutils.interpolatedStringFragments :-)

What differences are there between the Python and Scala formatting syntaxes?

FWIW I fully support including this in the stdlib.

Not that much. As for me, the main difference is that python syntax is richer but harder to understand, scala syntax prefers the use of simple formatters for standard cases and the use of standard library functions for the more special cases.

@vegansk any progress on this ? :)

@Yardanico , sorry, no time for that. Maybe you can make the PR using the code from nimboost

Whatever happens, just don't make boost a dependency for this please...

@Yardanico If you don't have time for it, I'm happy to volunteer as well (without promises on when I get to it ;)).

strformat is now part of the stdlib, feel free to create PRs to improve it.

It's great to see some work on this (again sorry for not having time to work on it more)!

However, the validation of formatters happens entirely at runtime instead of compile time :(. That was my big worry with the Python-like approach. My first attempts at using the DSL all end up in runtime crashes which would have been caught at compile time in the old Scala-like approach.

Will have to see if this can be fixed.

Also, the default behavior of fixed-width number formatting should left-pad like in any other string formatting library, i.e., in Python (and prinft/Scala...)

f"{1:10.1f}" == '       1.0'

but currently Nim would produce a right padded 1.0 (or actually it would crash at runtime if the 1 is not explicitly converted to float).

Also, the default behavior of fixed-width number formatting should left-pad like in any other string formatting library, i.e., in Python (and prinft/Scala...)

I wanted to do this but Python's f-strings do not do this, afaict.

Will have to see if this can be fixed.

I have some ideas about this but I don't consider it critical.

I wanted to do this but Python's f-strings do not do this, afaict.

Gah, Python does do it but its docs are silent about it...

@vegansk

Found another variant of string interpolation in https://bitbucket.org/lyro/strfmt.

That's not just a variant.. lyro/strfmt implements the like of Python .format in Nim, whereas your strformat implements (well, as you obviously know :) ) the Python f-string string interpolation mechanism. Unfortunately both libraries have the fmt identifier which do quite different things.. and they conflict and cannot be used in the same code.

Was this page helpful?
0 / 5 - 0 ratings