Variables in strings

13 views
Skip to first unread message

Pavel Roskin

unread,
Jul 11, 2014, 8:55:13 PM7/11/14
to ideal-de...@googlegroups.com
Hello!

I have noticed this code in circle.i:

return "ellipse(" ++ width ++ "*" ++ height ++ ")";

and then

println("Printing as ellipse: width ", e.width, ", height ", e.height);

Both uses too much syntax to join constant strings and variables.  There are better ways.

The Perl way:
print "width: $width, height: $height\n";

The Python way:
print("width = %s, height = %s")%(width, height)

The Swift way:
println("width = \(width), height = \(height)")

The Perl way would be confusing, as we have no $ in variable names.  The Python way makes me count %s numbers.  So the Swift way is probably the winner.

Pavel

M Dynin

unread,
Jul 11, 2014, 9:50:56 PM7/11/14
to Pavel Roskin, ideal-de...@googlegroups.com
Hi Pavel,

Yes, out of the three I prefer the Swift way as well. Python reuses
the remainder operator for an unrelated purpose, which I tried to
avoid. The only issue is that I don't see an elegant way to specify
the formatting parameters, such as the field width for padding.

The idea is that a language feature such as string interpolation can
be a "plugin" and can be developed incrementally.

=Misha.
> --
> You received this message because you are subscribed to the Google Groups
> "ideal.developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ideal-develope...@googlegroups.com.
> To post to this group, send email to ideal-de...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ideal-developers/c84b1035-de99-4ca7-aa3d-6497b58db2c2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://mishadynin.com/ · mdy...@gmail.com · @mishadynin · +1.404.mishady

M Dynin

unread,
Jul 12, 2014, 12:12:28 AM7/12/14
to Pavel Roskin, ideal-de...@googlegroups.com
Pavel,

On Fri, Jul 11, 2014 at 7:30 PM, Pavel Roskin <plro...@gmail.com> wrote:
> Misha,
>
> I think formatting should be done by the object. Objects could have
> methods for printing with optional arguments.
>
> class double {
> string to_string(width = 100, precision = 15);
> }
>
> By the way, I like infinity as in in Scheme. The default width could
> be positive infinity (even if it's reduced to the maximal integer).
>
> If we want expressions in parentheses, this should work:
>
> println("val = \(val.to_string(precision = 5))");
>
> Those would be equivalent:
>
> println("val = \(val)");
> println("val = \(val.to_string())");
>
> Most objects are only printed in one way. Printing floating point
> numbers is rare. So I don't worry much about having to use
> to_string() explicitly in those rare cases.

Yes, I think this addresses the most common use case.

In FORTRAN one formats and prints floating numbers a lot. Today on the
web, not so much. :)

=Misha.


>
> Pavel
Reply all
Reply to author
Forward
0 new messages