SugarCpp icon indicating copy to clipboard operation
SugarCpp copied to clipboard

Ruby style string formater via stringstream.

Open curimit opened this issue 12 years ago • 6 comments

In ruby we can format string in this way:

k = 123
str = "x=#{k+1}" 

So in SugarCpp, we can just write this

import "iostream"

int main()
    k := 123
    str := "x=#{k+1}"

Which compiles into this

#include <iostream>
#include <sstream>

int main()
{
    auto k = 123;
    auto str = ({
        std::stringstream _t_sstream;
        _t_sstream << "x=" << k + 1;
        _t_sstream.str();
    });
}

curimit avatar Jun 04 '13 11:06 curimit

Amazing feature.!

ppwwyyxx avatar Jun 04 '13 11:06 ppwwyyxx

Amazing! plz pay attention to the left part for that sometimes it's easy to use these if you brought out this feature.

cout << "x=#{k + 1}"

It shall be compiled to

cout << ({
        std::stringstream _t_sstream;
        _t_sstream << "x=" << k + 1;
        _t_sstream.str();
});

Seems it's easy for only dealing with "#{}", plz make sure there are some way to get exactly #{.*}.

Please check it when finished, THX.

vuryleo avatar Jun 04 '13 11:06 vuryleo

这一坨feature等我完成编译器自举以后再继续写……

curimit avatar Jun 06 '13 12:06 curimit

I've used this all the time in LS, but there's this one thing that's always nagged me about it: The assymetry of the notation - I find it often is a bit cluttered and unclear. A notation I do find to be very clear in this regard is:

interpolation := "elephant"
text := "room"
 "Here we have an {{interpolation}} in the {{text}}"

Ofcourse it doesn't follow the Ruby or LiveScript syntax, but still, it is much clearer imho.

ozra avatar Dec 09 '14 23:12 ozra

Seems good, I'll check this.

curimit avatar Dec 10 '14 03:12 curimit

Yeah, the where clauses from Haskell is neat, would also be for making localized functions etc. Generally. In the case of the interpolations, I mean ofcourse it should interpolate variables in the scope as usual, just with the change (or addition) of the double braces notation..

2014-12-10 4:42 GMT+01:00 curimit [email protected]:

How about this?

text := "Here we have an {{interpolation}} in the {{text}}" where interpolation := "elephant" text := "room"

— Reply to this email directly or view it on GitHub https://github.com/curimit/SugarCpp/issues/25#issuecomment-66400441.

ozra avatar Dec 11 '14 07:12 ozra