www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - pure format

reply "Oleg B" <code.viator gmail.com> writes:
Hello. I not found realization of pure format function (in phobos 
it not pure) and write minimal custom realization.
https://github.com/dexset/desstdx/blob/master/source/des/stdx/pformat.d#L472
Now it works with numbers and strings, but in future I improve it.
Maybe my realization can help someone =)
Contained in the dub package 
http://code.dlang.org/packages/desstdx

If pure format exists tell about it, please.
Jun 04 2015
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Thursday, 4 June 2015 at 14:33:39 UTC, Oleg B wrote:
 Hello. I not found realization of pure format function (in 
 phobos it not pure) and write minimal custom realization.
 https://github.com/dexset/desstdx/blob/master/source/des/stdx/pformat.d#L472
 Now it works with numbers and strings, but in future I improve 
 it.
 Maybe my realization can help someone =)
 Contained in the dub package 
 http://code.dlang.org/packages/desstdx

 If pure format exists tell about it, please.
std.format.format should be pure if its arguments have pure toString methods or are built-in types. There may be implementation issues preventing it for one reason or another (I don't know), but if so, then that's a bug that needs to be fixed. Regardless, there should be no need for a separate format function which is pure. - Jonathan M Davis
Jun 04 2015
parent reply "Oleg B" <code.viator gmail.com> writes:
On Thursday, 4 June 2015 at 16:21:54 UTC, Jonathan M Davis wrote:
 On Thursday, 4 June 2015 at 14:33:39 UTC, Oleg B wrote:
 Hello. I not found realization of pure format function (in 
 phobos it not pure) and write minimal custom realization.
 https://github.com/dexset/desstdx/blob/master/source/des/stdx/pformat.d#L472
 Now it works with numbers and strings, but in future I improve 
 it.
 Maybe my realization can help someone =)
 Contained in the dub package 
 http://code.dlang.org/packages/desstdx

 If pure format exists tell about it, please.
std.format.format should be pure if its arguments have pure toString methods or are built-in types. There may be implementation issues preventing it for one reason or another (I don't know), but if so, then that's a bug that needs to be fixed. Regardless, there should be no need for a separate format function which is pure. - Jonathan M Davis
I think toString for float must be pure, but in practice it's not. import std.stdio; import std.string; string test( float abc ) pure { return format( "abc: % 6.3f", abc ); } void main() { writeln( test( 13.3 ) ); } $ rdmd purefmtfloating.d purefmtfloating.d(3): Error: pure function 'purefmtfloating.test' cannot call impure function 'std.format.format!(char, float).format' Failed: ["dmd", "-v", "-o-", "purefmtfloating.d", "-I."] DMD64 v2.067.1 It's a bug? I read what std.format.format using libc sprintf http://www.digitalmars.com/d/archives/digitalmars/D/learn/std.conv. o_purity_68957.html and if it try it's not a bug...
Jun 04 2015
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/04/2015 10:02 AM, Oleg B wrote:

 I think toString for float must be pure, but in practice it's not.

 import std.stdio;
 import std.string;
 string test( float abc ) pure { return format( "abc: % 6.3f", abc ); }
 void main() { writeln( test( 13.3 ) ); }

 $ rdmd purefmtfloating.d
 purefmtfloating.d(3): Error: pure function 'purefmtfloating.test' cannot
 call impure function 'std.format.format!(char, float).format'
 Failed: ["dmd", "-v", "-o-", "purefmtfloating.d", "-I."]

 DMD64 v2.067.1

 It's a bug? I read what std.format.format using libc sprintf
 
http://www.digitalmars.com/d/archives/digitalmars/D/learn/std.conv. o_purity_68957.html
 and if it try it's not a bug...
Floating point operations share global state ("flags" or "attributes") for rounding mode, exception and trap handling. Perhaps that's why floating point format cannot be pure. Ali
Jun 04 2015
next sibling parent "Meta" <jared771 gmail.com> writes:
On Thursday, 4 June 2015 at 18:08:09 UTC, Ali Çehreli wrote:
http://www.digitalmars.com/d/archives/digitalmars/D/learn/std.conv.to_purity_68957.html
 and if it try it's not a bug...
Floating point operations share global state ("flags" or "attributes") for rounding mode, exception and trap handling. Perhaps that's why floating point format cannot be pure. Ali
That is not it (if it is, it's a bug), as D allows pure functions to set and check those flags. http://dlang.org/function.html#pure-functions
Jun 04 2015
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/4/15 2:08 PM, Ali Çehreli wrote:
 On 06/04/2015 10:02 AM, Oleg B wrote:

  > I think toString for float must be pure, but in practice it's not.
  >
  > import std.stdio;
  > import std.string;
  > string test( float abc ) pure { return format( "abc: % 6.3f", abc ); }
  > void main() { writeln( test( 13.3 ) ); }
  >
  > $ rdmd purefmtfloating.d
  > purefmtfloating.d(3): Error: pure function 'purefmtfloating.test' cannot
  > call impure function 'std.format.format!(char, float).format'
  > Failed: ["dmd", "-v", "-o-", "purefmtfloating.d", "-I."]
  >
  > DMD64 v2.067.1
  >
  > It's a bug? I read what std.format.format using libc sprintf
  >
 http://www.digitalmars.com/d/archives/digitalmars/D/learn/std.conv.to_purity_68957.html

  > and if it try it's not a bug...

 Floating point operations share global state ("flags" or "attributes")
 for rounding mode, exception and trap handling. Perhaps that's why
 floating point format cannot be pure.
That is a terrible excuse if that is what the issue is. I recalled Don discussing this, found these threads (they are old, maybe something has gotten better since then): http://forum.dlang.org/post/gpddrm$20u7$1 digitalmars.com http://forum.dlang.org/post/h8ad5r$22jj$1 digitalmars.com From that, it does seem that D does not care about the rounding modes, and just considers the code pure. -Steve
Jun 04 2015
parent "Yuxuan Shui" <yshuiv7 gmail.com> writes:
On Thursday, 4 June 2015 at 18:27:16 UTC, Steven Schveighoffer 
wrote:
 On 6/4/15 2:08 PM, Ali Çehreli wrote:
 [...]
That is a terrible excuse if that is what the issue is. I recalled Don discussing this, found these threads (they are old, maybe something has gotten better since then): http://forum.dlang.org/post/gpddrm$20u7$1 digitalmars.com http://forum.dlang.org/post/h8ad5r$22jj$1 digitalmars.com From that, it does seem that D does not care about the rounding modes, and just considers the code pure. -Steve
Is it possible to get this fixed in mainline phobos? Not having a pure format() is really annoying.
Jul 01 2015
prev sibling parent reply "HaraldZealot" <harald_zealot tut.by> writes:
On Thursday, 4 June 2015 at 17:02:46 UTC, Oleg B wrote:
 On Thursday, 4 June 2015 at 16:21:54 UTC, Jonathan M Davis 
 wrote:

 I think toString for float must be pure, but in practice it's 
 not.


 It's a bug? I read what std.format.format using libc sprintf 
 http://www.digitalmars.com/d/archives/digitalmars/D/learn/std.conv.
o_purity_68957.html and if it try it's not a bug...
And I also remember that format doesn't work in CT, so it was really uncomfortable that a can't initialize array of special values for algorithm in CT. It was a more than year ago, possible something changes.
Jul 01 2015
parent reply "Nicholas Wilson" <iamthewilsonator hotmail.com> writes:
On Thursday, 2 July 2015 at 04:41:51 UTC, HaraldZealot wrote:
 On Thursday, 4 June 2015 at 17:02:46 UTC, Oleg B wrote:
 On Thursday, 4 June 2015 at 16:21:54 UTC, Jonathan M Davis 
 wrote:

 I think toString for float must be pure, but in practice it's 
 not.


 It's a bug? I read what std.format.format using libc sprintf 
 http://www.digitalmars.com/d/archives/digitalmars/D/learn/std.conv.
o_purity_68957.html and if it try it's not a bug...
And I also remember that format doesn't work in CT, so it was really uncomfortable that a can't initialize array of special values for algorithm in CT. It was a more than year ago, possible something changes.
pretty sure std.metastring(s?) was deprecated 'cos format works at CT
Jul 02 2015
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 02-Jul-2015 12:53, Nicholas Wilson wrote:
 On Thursday, 2 July 2015 at 04:41:51 UTC, HaraldZealot wrote:
 On Thursday, 4 June 2015 at 17:02:46 UTC, Oleg B wrote:
 On Thursday, 4 June 2015 at 16:21:54 UTC, Jonathan M Davis wrote:

 I think toString for float must be pure, but in practice it's not.


 It's a bug? I read what std.format.format using libc sprintf
 http://www.digitalmars.com/d/archives/digitalmars/D/learn/std.conv.to_purity_68957.html
 and if it try it's not a bug...
And I also remember that format doesn't work in CT, so it was really uncomfortable that a can't initialize array of special values for algorithm in CT. It was a more than year ago, possible something changes.
pretty sure std.metastring(s?) was deprecated 'cos format works at CT
Yeah, formattedWrite definitely works at CTFE these days. -- Dmitry Olshansky
Jul 02 2015