Final call for GHC String Interpolation proposal!

How can the normal s"..." yield a Text? According to the spec the interpolateFinalize always returns a String. I’d be happy if there was a fromString on the result so we could get a Text, but that’s not in the spec I used when I implemented interpolation in MicroHs.

Has there been any thought on how to format things more nicely? I mean things like width of a field, left/right justified, number of decimals for a float, etc. I think we could make something simple and nice, but we should do it now, before everyone invents their own way of doing it.

For instance
data LAdj a = LAdj Int a
instance Interpolate a => Interpolate (LAdj a) where
interpolate (LAdj n a) = ... interpolate a ... pad to get width n ...

1 Like

The proposal should mention that fromString is added with -XOverloadedStrings. Otherwise, it will only ever be String, as normal.

No, nothing like that right now. I was never a fan of hardcoded format specifiers that bless certain types, and it would move the feature even further towards magic implicit conversions. You can easily implement in user-land, perhaps in an interpolate-contrib package if necessary

1 Like

Left/right adjusting is not in any way type specific. It’s very useful for aligning data over multiple lines.

1 Like

Oh sure, I was mostly referring to things like decimal places for floats.

I can see how it’d be useful, but I also dislike having a mini format DSL just for this. It can also get more complicated when you want to do things like “left justify with width from a variable instead of a num literal”. I actually think the user-land solution is more readable anyway; we could always add it to base after if it’s so desired.

let width = maximum $ map (length . fst) vals
 in [s"${ljust width name}: ${type_}" | (name, type_) <- vals]
3 Likes

I never intended justification etc. to be by a literal. It’s an Int, you can get it from anywhere.

I’ll add some extras to base for MicroHs. Without a little formatting there is a near zero chance that I’ll use interpolation; I’d just use printf instead.

If you want to test the string interpolation, you can do it with the latest MicroHs.

git clone -- depth=1 git@github.com:augustss/MicroHs.git
make install
mhs
Welcome to interactive MicroHs, version 0.16.5.0
Integer implemented with imath
Loading package /Users/augustss/.mcabal/bin/../mhs-0.16.5.0/packages/base-0.16.5.0.pkg
Type ':quit' to quit, ':help' for help
> x=42
> putStrLn s"x=${x}, x^2=${x^2}"
x=42, x^2=1764
> import Data.String.Interpolate  -- for some bonus MicroHs stuff
> mapM_ (\ x -> putStrLn s"x=${RightAdj 5 x}, x^2=${RightAdj 10 $ x^2}") [3,42,12345,88]
x=    3, x^2=         9
x=   42, x^2=      1764
x=12345, x^2= 152399025
x=   88, x^2=      7744
15 Likes