printf-like formatting of floats and such

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

printf-like formatting of floats and such

Perry E. Metzger
Is there at this point a standard way to do things like printf-like
formatting of doubles and floats without having to call in to
Objective C?

Perry
--
Perry E. Metzger [hidden email]

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/20151122165112.7e4fafdf%40jabberwock.cb.piermont.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: printf-like formatting of floats and such

Jens Alfke

On Nov 22, 2015, at 1:51 PM, Perry E. Metzger <[hidden email]> wrote:

Is there at this point a standard way to do things like printf-like
formatting of doubles and floats without having to call in to
Objective C?

Depends what you mean by ‘call into Objective-C’. You can do:
let str = NSString(format: “%d”, args: 123.456)

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/28C3E1AF-A738-4489-9BF3-71E0D1792208%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: printf-like formatting of floats and such

Marco S Hyman
In reply to this post by Perry E. Metzger
On Nov 22, 2015, at 1:51 PM, Perry E. Metzger <[hidden email]> wrote:
>
> Is there at this point a standard way to do things like printf-like
> formatting of doubles and floats without having to call in to
> Objective C?

        String(format: “format string”, val, val, val)

as in

print(String(format: "Value: %3.2f\tResult: %3.2f", arguments: [2.7, 99.8]))

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/422FB904-CBBD-4CB1-B131-225859DF650C%40snafu.org.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: printf-like formatting of floats and such

Perry E. Metzger
On Sun, 22 Nov 2015 15:17:55 -0800 Marco S Hyman <[hidden email]>
wrote:

> On Nov 22, 2015, at 1:51 PM, Perry E. Metzger <[hidden email]>
> wrote:
> >
> > Is there at this point a standard way to do things like
> > printf-like formatting of doubles and floats without having to
> > call in to Objective C?
>
> String(format: “format string”, val, val, val)
>
> as in
>
> print(String(format: "Value: %3.2f\tResult: %3.2f", arguments:
> [2.7, 99.8]))

Very cool! But where is this documented? I don't seem to be able to
find it. Am I just missing the right place?

Perry
--
Perry E. Metzger [hidden email]

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/20151122221420.47ac6747%40jabberwock.cb.piermont.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: printf-like formatting of floats and such

Jens Alfke

On Nov 22, 2015, at 7:14 PM, Perry E. Metzger <[hidden email]> wrote:

Very cool! But where is this documented? I don't seem to be able to
find it. Am I just missing the right place?

http://swiftdoc.org/v2.1/type/String/ … search for “format”

It’s a common NSString method, bridged to Swift. So you can also open NSString.h and see the Swift translation in the Counterparts assistant pane.

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/7F5859F7-EECD-43E8-9547-48D6582F17B0%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: printf-like formatting of floats and such

Perry E. Metzger
On Sun, 22 Nov 2015 19:30:49 -0800 Jens Alfke <[hidden email]>
wrote:

>
> > On Nov 22, 2015, at 7:14 PM, Perry E. Metzger
> > <[hidden email]> wrote:
> >
> > Very cool! But where is this documented? I don't seem to be able
> > to find it. Am I just missing the right place?
>
> http://swiftdoc.org/v2.1/type/String/ … search for “format”
>
> It’s a common NSString method, bridged to Swift. So you can also
> open NSString.h and see the Swift translation in the Counterparts
> assistant pane.

Thank you! This will sound weird, but I had no idea swiftdoc.org
existed. The only documentation I knew of for the string class was in

https://developer.apple.com/library/ios//documentation/Swift/Reference/Swift_String_Structure/index.html#//apple_ref/swift/struct/s:SS

and that doesn't mention format or anything like it.

Perry
--
Perry E. Metzger [hidden email]

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/20151123085848.2eee9b69%40jabberwock.cb.piermont.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: printf-like formatting of floats and such

Daniel T.
On Monday, November 23, 2015 at 8:58:51 AM UTC-5, Perry Metzger wrote:
On Sun, 22 Nov 2015 19:30:49 -0800 Jens Alfke <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="8AWvqx9hBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">je...@...>
wrote:

>
> > On Nov 22, 2015, at 7:14 PM, Perry E. Metzger
> > <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="8AWvqx9hBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">pe...@...> wrote:
> >
> > Very cool! But where is this documented? I don't seem to be able
> > to find it. Am I just missing the right place?
>
> <a href="http://swiftdoc.org/v2.1/type/String/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fswiftdoc.org%2Fv2.1%2Ftype%2FString%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNGVaNzbh9htZerXFBbB2JEnLRZiJQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fswiftdoc.org%2Fv2.1%2Ftype%2FString%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNGVaNzbh9htZerXFBbB2JEnLRZiJQ&#39;;return true;">http://swiftdoc.org/v2.1/type/String/ … search for “format”
>
> It’s a common NSString method, bridged to Swift. So you can also
> open NSString.h and see the Swift translation in the Counterparts
> assistant pane.

Thank you! This will sound weird, but I had no idea <a href="http://swiftdoc.org" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fswiftdoc.org\46sa\75D\46sntz\0751\46usg\75AFQjCNFNZNoe0zxQ0IYYDvzJO3xhMlw8zA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fswiftdoc.org\46sa\75D\46sntz\0751\46usg\75AFQjCNFNZNoe0zxQ0IYYDvzJO3xhMlw8zA&#39;;return true;">swiftdoc.org
existed. The only documentation I knew of for the string class was in

<a href="https://developer.apple.com/library/ios//documentation/Swift/Reference/Swift_String_Structure/index.html#//apple_ref/swift/struct/s:SS" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\75https%3A%2F%2Fdeveloper.apple.com%2Flibrary%2Fios%2F%2Fdocumentation%2FSwift%2FReference%2FSwift_String_Structure%2Findex.html%23%2F%2Fapple_ref%2Fswift%2Fstruct%2Fs%3ASS\46sa\75D\46sntz\0751\46usg\75AFQjCNHYVLXET3rGR1FMte_LMFo4_slGFw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\75https%3A%2F%2Fdeveloper.apple.com%2Flibrary%2Fios%2F%2Fdocumentation%2FSwift%2FReference%2FSwift_String_Structure%2Findex.html%23%2F%2Fapple_ref%2Fswift%2Fstruct%2Fs%3ASS\46sa\75D\46sntz\0751\46usg\75AFQjCNHYVLXET3rGR1FMte_LMFo4_slGFw&#39;;return true;">https://developer.apple.com/library/ios//documentation/Swift/Reference/Swift_String_Structure/index.html#//apple_ref/swift/struct/s:SS

and that doesn't mention format or anything like it.

Well technically, the answer Jens gave you does not answer the original question you asked. `String(format:)` is calling into Objective-C and you asked for a way to do it that doesn't do that.

Still, it exists and does what you need so there's no problem using it. 

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/1385062d-e7c7-447a-8910-bfebf7910186%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: printf-like formatting of floats and such

Perry E. Metzger
On Mon, 23 Nov 2015 06:19:48 -0800 (PST) "Daniel T."
<[hidden email]> wrote:
> Well technically, the answer Jens gave you does not answer the
> original question you asked. `String(format:)` is calling into
> Objective-C and you asked for a way to do it that doesn't do that.
>
> Still, it exists and does what you need so there's no problem using
> it.

Hrm. I note that this takes an array of CVarArgType. Does that mean
that you're potentially type unsafe if there is a mismatch
between the passed argument and the format string, just as in C?

Perry
--
Perry E. Metzger [hidden email]

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/20151123093627.4c435457%40jabberwock.cb.piermont.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: printf-like formatting of floats and such

Daniel T.
On Monday, November 23, 2015 at 9:36:29 AM UTC-5, Perry Metzger wrote:
On Mon, 23 Nov 2015 06:19:48 -0800 (PST) "Daniel T."
<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="6Bc0ay1jBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">danie...@...> wrote:
> Well technically, the answer Jens gave you does not answer the
> original question you asked. `String(format:)` is calling into
> Objective-C and you asked for a way to do it that doesn't do that.
>
> Still, it exists and does what you need so there's no problem using
> it.

Hrm. I note that this takes an array of CVarArgType. Does that mean
that you're potentially type unsafe if there is a mismatch
between the passed argument and the format string, just as in C?
 
The answer is a qualified "no." The compiler understands the format specifiers, checks the types and will emit warnings to let you know if the types don't match the specifiers. Of course if you are in the habit of ignoring warnings you still might have a problem.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/0b0284b4-ae2d-4f20-a170-7238bb2b91d8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: printf-like formatting of floats and such

Perry E. Metzger
On Mon, 23 Nov 2015 06:43:04 -0800 (PST) "Daniel T."
<[hidden email]> wrote:

> On Monday, November 23, 2015 at 9:36:29 AM UTC-5, Perry Metzger
> wrote:
> >
> > On Mon, 23 Nov 2015 06:19:48 -0800 (PST) "Daniel T."
> > <[hidden email] <javascript:>> wrote:
> > > Well technically, the answer Jens gave you does not answer the
> > > original question you asked. `String(format:)` is calling into
> > > Objective-C and you asked for a way to do it that doesn't do
> > > that.
> > >
> > > Still, it exists and does what you need so there's no problem
> > > using it.
> >
> > Hrm. I note that this takes an array of CVarArgType. Does that
> > mean that you're potentially type unsafe if there is a mismatch
> > between the passed argument and the format string, just as in C?
>  
> The answer is a qualified "no." The compiler understands the format
> specifiers, checks the types and will emit warnings to let you know
> if the types don't match the specifiers. Of course if you are in
> the habit of ignoring warnings you still might have a problem.

I'm surprised that they're just warnings and not fatal errors then
(since Swift is supposed to be strictly typed). Is that because
there are times when you need to still violate the type discipline
when using formatting strings?

Perry
--
Perry E. Metzger [hidden email]

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/20151123105758.22d09643%40jabberwock.cb.piermont.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: printf-like formatting of floats and such

Daniel T.
On Nov 23, 2015, at 10:57 AM, Perry E. Metzger <[hidden email]> wrote:

> On Mon, 23 Nov 2015 06:43:04 -0800 (PST) "Daniel T.” <[hidden email]> wrote:
>> On Monday, November 23, 2015 at 9:36:29 AM UTC-5, Perry Metzler wrote:
>>>
>>> Hrm. I note that this takes an array of CVarArgType. Does that
>>> mean that you're potentially type unsafe if there is a mismatch
>>> between the passed argument and the format string, just as in C?
>>
>> The answer is a qualified "no." The compiler understands the format
>> specifiers, checks the types and will emit warnings to let you know
>> if the types don't match the specifiers. Of course if you are in
>> the habit of ignoring warnings you still might have a problem.
>
> I'm surprised that they're just warnings and not fatal errors then
> (since Swift is supposed to be strictly typed). Is that because
> there are times when you need to still violate the type discipline
> when using formatting strings?

I think it is more because it’s an Objective-C thing rather than a Swift thing.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/5C3F47E8-080D-4444-8B0F-6C35D51935C1%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: printf-like formatting of floats and such

Jens Alfke
The open-source release of Swift will need to include a self-contained standard library with printf-type formatting baked into the String class itself. Maybe this will include stronger type-checking.

Also: wouldn't a pure-Swift implementation be able to determine the actual type of each parameter (by using “as?”)? In that case it could detect mismatches at runtime and abort (instead of reading garbage off the stack.) Better yet, it could handle some types of mismatches intelligently — so if you specified “%d” but passed a Float, it could print it correctly. (The Go language’s printf does this.)

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/E2FC2876-11F2-44DE-9616-A2B58019D6B4%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: printf-like formatting of floats and such

Perry E. Metzger
On Mon, 23 Nov 2015 09:04:13 -0800 Jens Alfke <[hidden email]>
wrote:

> The open-source release of Swift will need to include a
> self-contained standard library with printf-type formatting baked
> into the String class itself. Maybe this will include stronger
> type-checking.
>
> Also: wouldn't a pure-Swift implementation be able to determine the
> actual type of each parameter (by using “as?”)? In that case it
> could detect mismatches at runtime and abort (instead of reading
> garbage off the stack.) Better yet, it could handle some types of
> mismatches intelligently — so if you specified “%d” but passed a
> Float, it could print it correctly. (The Go language’s printf does
> this.)

I would prefer, in the latter case, to get a compile error so that the
mistake isn't silently left in the code waiting to cause trouble. (A
language can be *too* helpful.)

That said, it would be interesting if Swift grew a typesafe mechanism
to permit varargs of non-uniform type. Presumably the use of runtime
type information and type inquiry operators would allow this.

Perry
--
Perry E. Metzger [hidden email]

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/20151123123532.0fa47b31%40jabberwock.cb.piermont.com.
For more options, visit https://groups.google.com/d/optout.