digitalmars.D.learn - std.conv.to purity
- Jack Applegame (4/4) Feb 14 2015 why std.conv.to is not pure?
- ketmar (4/9) Feb 14 2015 'cause float->string conversion is damned hard task. to perform this=20
- Gary Willoughby (3/14) Feb 14 2015 Forgive me being a bit dense but that doesn't necessarily mean
- ketmar (11/26) Feb 14 2015 yes, you are right, it *may* be pure. yet `snprintf()` can't be marked a...
- Brad Roberts via Digitalmars-d-learn (5/31) Feb 14 2015 While snprintf might be one thing that provides to be an interesting
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'
Feb 14 2015
On Sat, 14 Feb 2015 11:29:28 +0000, Jack Applegame wrote:why std.conv.to is not pure? =20 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=20 conversion `to!` falls back to `formatValue` from "std.format", which in=20 turn using `snprintf()` from libc.=
Feb 14 2015
On Saturday, 14 February 2015 at 12:24:51 UTC, ketmar wrote:On Sat, 14 Feb 2015 11:29:28 +0000, Jack Applegame wrote:Forgive me being a bit dense but that doesn't necessarily mean it's impure though, right?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.
Feb 14 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:yes, you are right, it *may* be pure. yet `snprintf()` can't be marked as=20 `pure`, so it contaminates everything. and there are no guarantees that=20 `snprintf()` is pure even for known arguments (compiler can't prove that,=20 and libc is free to do any funny things in it), so it will be wrong to=20 just force-reimport it as `pure`. tl;dr: we need native D float->string converter to allow purity in this=20 case. you are free to force-hack purity flag on function if you want, but=20 this can be wrong and may lead to various heisenbugs. native pure D converter will also allow CTFE, btw, which is impossible=20 now.=On Sat, 14 Feb 2015 11:29:28 +0000, Jack Applegame wrote:=20 Forgive me being a bit dense but that doesn't necessarily mean it's impure though, right?why std.conv.to is not pure? =20 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.
Feb 14 2015
While snprintf might be one thing that provides to be an interesting obstacle, the better answer to why std.conv.to isnt pure is that no one has invested the time to work through issues like that to make it so. It _should_ be pure. On 2/14/2015 12:32 PM, ketmar via Digitalmars-d-learn wrote:On Sat, 14 Feb 2015 19:59:58 +0000, Gary Willoughby wrote:On Saturday, 14 February 2015 at 12:24:51 UTC, ketmar wrote: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.On Sat, 14 Feb 2015 11:29:28 +0000, Jack Applegame wrote:Forgive me being a bit dense but that doesn't necessarily mean it's impure though, right?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.
Feb 14 2015