Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 04, 2015 pure format | ||||
---|---|---|---|---|
| ||||
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. |
June 04, 2015 Re: pure format | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oleg B | 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
|
June 04, 2015 Re: pure format | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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.to_purity_68957.html and if it try it's not a bug... |
June 04, 2015 Re: pure format | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oleg B | 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. Ali |
June 04, 2015 Re: pure format | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 |
June 04, 2015 Re: pure format | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 |
July 01, 2015 Re: pure format | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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.
|
July 02, 2015 Re: pure format | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oleg B | 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.
|
July 02, 2015 Re: pure format | ||||
---|---|---|---|---|
| ||||
Posted in reply to HaraldZealot | 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
|
July 02, 2015 Re: pure format | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | 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 |
Copyright © 1999-2021 by the D Language Foundation