format a la io_lib:format; is it possible?

1,454 views
Skip to first unread message

Alex Shneyderman

unread,
Jul 10, 2013, 7:59:03 AM7/10/13
to elixir-l...@googlegroups.com
I find interpolations "#{s}" very handy. What would be even handier -

n = 3.245
IO.puts "#{s:3.2f}"

I was thinking of writing a sigil but there seems to be a problem with the timing of interpolation.

IO.puts %f"#{s:3.2f}" 

will not work, a naive alternative that would work

IO.puts %f"{s:3.2f}"

but that is inconsistent with how interpolation is done.

So, I wonder how others do it? So, far I saw 2 ways, both rely on erlang :io_lib:format. Joe's #{pp(v)} 
in his blog post and Dave's direct use on :io.format() in his book. Neither one is satisfying TBH. 

Ideally the power of io_lib:format would be supported natively in #{}. Of course if that is not going 
to happen I will settle for a sigil.

OTH, maybe I am missing something in the syntax and such a thing already exists? 

Cheers,
Alex.

José Valim

unread,
Jul 10, 2013, 8:42:58 AM7/10/13
to elixir-l...@googlegroups.com
Why is :io.format/2 not satisfying?

Interpolations are regular elixir code, so it is unlikely they will support any kind of special notation. This limits such syntax sugar but on the other hand it makes it consistent with all other expressions in the language. So even if we have our own formatting, it will go through a regular function, something like IO.format.


José Valim
Skype: jv.ptec
Founder and Lead Developer



--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Alex Shneyderman

unread,
Jul 10, 2013, 10:09:57 AM7/10/13
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br

Why is :io.format/2 not satisfying?

not sugar-coated enough. #{} is a lot shorter and a lot more to the point.

Dave Thomas

unread,
Jul 10, 2013, 11:32:24 AM7/10/13
to elixir-l...@googlegroups.com

The thing I dislike is the ~

Most new languages have adopted sprintf syntax, and I always feel like I'm traveling back to 1986 when I use io:format

But that doesn't answer the original post.

Would there be any support for a native Elixir sprintf?

Dave

Alexei Sholik

unread,
Jul 10, 2013, 12:27:28 PM7/10/13
to elixir-l...@googlegroups.com
Would there be any support for a native Elixir sprintf?

I wonder what you mean by native support. Sprintf is just a function, so you can write it today. If you mean adding fancy syntax support inside #{}, I don't think that'll happen.

I personally find it convenient to use printf-like template strings for precise formatting, but use string interpolation for quick and dirty outputs. Both can coexist in Elixir, so it's just a matter of someone coming up with a lib that implements another style of formatting for Elixir.

Elixir could have a built-in wrapper for formatted output, but there will still be users who won't like it. Because besides Erlang's style and printf-like style, there is also crazy Python style:

>>> '{:*^30}'.format('centered')  # use '*' as a fill char
'***********centered***********'
 and other styles I'm not aware of.

Having a 3rd-party lib that is easy to fetch with mix seems like a good trade-off to me.
Best regards
Alexei Sholik

Dave Thomas

unread,
Jul 10, 2013, 12:48:06 PM7/10/13
to elixir-l...@googlegroups.com
On Wed, Jul 10, 2013 at 11:27 AM, Alexei Sholik <alcos...@gmail.com> wrote:
Would there be any support for a native Elixir sprintf?

I wonder what you mean by native support. Sprintf is just a function, so you can write it today. If you mean adding fancy syntax support inside #{}, I don't think that'll happen.

Right.

I was asking whether y'all would consider a PR that added sprintf to either Kernel or String. 


Dave 

José Valim

unread,
Jul 10, 2013, 12:53:00 PM7/10/13
to elixir-l...@googlegroups.com
For Elixir, we will need to write an IO.format that follows Erlang's io:format(). That's because that format is used by Erlang error logger and Elixir should be able to handle it natively if we ever want to print Elixir terms on gen server errors.

That said, we won't have a sprintf in Elixir per-se.


José Valim
Skype: jv.ptec
Founder and Lead Developer


Geoffrey Clements

unread,
Jul 11, 2013, 9:34:06 AM7/11/13
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
Sometimes you want the string returned not printed to stdout.

Alex Shneyderman

unread,
Jul 11, 2013, 1:49:13 PM7/11/13
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br

Sometimes you want the string returned not printed to stdout.


:io.format/2 is meant all sorts of :io* functions. If you do not want stuff on stdout you can use :io_lib:format().

Nicholas Manning

unread,
Feb 17, 2016, 5:31:31 AM2/17/16
to elixir-lang-talk, da...@pragprog.com
@Dave I agree with you.  Coming from Go, Ruby, Swift, etc, as a new Elixir user, :io.format/2 is quite confusing. 

Robert Virding

unread,
Feb 17, 2016, 7:18:58 PM2/17/16
to elixir-lang-talk, da...@pragprog.com
He he he. If you feel like the ~ takes you back to 1986 then then % takes you back much further, say to 1976. C is old. That it's used in many "modern" languages does not men that it is not old. In this case it is not age as such but what you are use to. :-)

Robert

Jim Menard

unread,
Feb 18, 2016, 6:22:27 AM2/18/16
to elixir-l...@googlegroups.com, da...@pragprog.com
"~" should take you back to the early '60s since it is used in Lisp's
format function.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-talk/6a8f8ae8-d314-4489-8a24-fc26c4dd7681%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Jim Menard, http://www.jimmenard.com/
Reply all
Reply to author
Forward
0 new messages