www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Purity of std.conv.to!string

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Why is the following code not pure:

     float x = 3.14;
     import std.conv : to;
     auto y = x.to!string;


???

Is there a reason for it not being pure? If not, this is a 
serious problem as this is such a fundamental function.
Sep 26 2015
next sibling parent reply cym13 <cpicard openmailbox.org> writes:
On Saturday, 26 September 2015 at 17:08:00 UTC, Nordlöw wrote:
 Why is the following code not pure:

     float x = 3.14;
     import std.conv : to;
     auto y = x.to!string;


 ???

 Is there a reason for it not being pure? If not, this is a 
 serious problem as this is such a fundamental function.
Maybe because of floating point numbers uncertainty that would cause the resulting string to be different for two equivalent inputs? I can't seem to put an example together though.
Sep 26 2015
parent Daniel Murphy <yebbliesnospam gmail.com> writes:
On 27/09/2015 3:14 AM, cym13 wrote:
 On Saturday, 26 September 2015 at 17:08:00 UTC, Nordlöw wrote:
 Why is the following code not pure:

     float x = 3.14;
     import std.conv : to;
     auto y = x.to!string;


 ???

 Is there a reason for it not being pure? If not, this is a serious
 problem as this is such a fundamental function.
Probably because it uses C's float formatting functions, and they probably set errno and therefore aren't pure.
 Maybe because of floating point numbers uncertainty that would cause the
 resulting string to be different for two equivalent inputs? I can't seem
 to put an example together though.
No, it doesn't work like that.
Sep 27 2015
prev sibling next sibling parent Xinok <xinok live.com> writes:
On Saturday, 26 September 2015 at 17:08:00 UTC, Nordlöw wrote:
 Why is the following code not pure:

     float x = 3.14;
     import std.conv : to;
     auto y = x.to!string;


 ???

 Is there a reason for it not being pure? If not, this is a 
 serious problem as this is such a fundamental function.
I don't know the exact reason but I found a couple of functions which could be marked pure which are not, including strippedOctalLiteral and isOctalLiteralString. It's possible that some function was simply overlooked and needs to be marked pure or infer purity. The larger issue at hand is that the compiler doesn't tell you where an impurity lies, merely that it exists. I mentioned this issue not too long ago while experiencing my own difficulties respecting purity.
Sep 26 2015
prev sibling parent reply Jack Stouffer <jack jackstouffer.com> writes:
On Saturday, 26 September 2015 at 17:08:00 UTC, Nordlöw wrote:
 Why is the following code not pure:

     float x = 3.14;
     import std.conv : to;
     auto y = x.to!string;


 ???

 Is there a reason for it not being pure? If not, this is a 
 serious problem as this is such a fundamental function.
Please make an issue on https://issues.dlang.org and I'll take a look a this later. Most of the functions in std.conv are templated so it must be some internal function that's not properly annotated, or it's using manual memory management.
Sep 26 2015
parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Sunday, 27 September 2015 at 05:52:26 UTC, Jack Stouffer wrote:
 Please make an issue on https://issues.dlang.org and I'll take 
 a look a this later. Most of the functions in std.conv are 
 templated so it must be some internal function that's not 
 properly annotated, or it's using manual memory management.
Already file here: https://issues.dlang.org/show_bug.cgi?id=3437 https://issues.dlang.org/show_bug.cgi?id=4850
Sep 28 2015