std.conv.to purity

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 14 12:32:03 PST 2015


On Sat, 14 Feb 2015 19:59:58 +0000, Gary Willoughby wrote:

> On Saturday, 14 February 2015 at 12:24:51 UTC, ketmar wrote:
>> On Sat, 14 Feb 2015 11:29:28 +0000, Jack Applegame wrote:
>>
>>> why std.conv.to is not pure?
>>> 
>>> string foo(real v) pure { return v.to!string; }
>>> // Error: pure function 'foo' cannot call impure function
>>> 'std.conv.to!string.to!(real).to'
>>
>> 'cause float->string conversion is damned hard task. to perform this
>> conversion `to!` falls back to `formatValue` from "std.format",
>> which in turn using `snprintf()` from libc.
> 
> Forgive me being a bit dense but that doesn't necessarily mean it's
> impure though, right?

yes, you are right, it *may* be pure. yet `snprintf()` can't be marked as 
`pure`, so it contaminates everything. and there are no guarantees that 
`snprintf()` is pure even for known arguments (compiler can't prove that, 
and libc is free to do any funny things in it), so it will be wrong to 
just force-reimport it as `pure`.

tl;dr: we need native D float->string converter to allow purity in this 
case. you are free to force-hack purity flag on function if you want, but 
this can be wrong and may lead to various heisenbugs.

native pure D converter will also allow CTFE, btw, which is impossible 
now.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150214/0e005ba9/attachment.sig>


More information about the Digitalmars-d-learn mailing list