digitalmars.D.learn - std.algorithm can not be used inside pure functions?
- Szabo Bogdan (19/19) May 06 2017 Hi,
- Adam D. Ruppe (4/5) May 06 2017 I believe that function is not marked pure if it is a SysTime
- Szabo Bogdan (3/8) May 06 2017 oh yes, I get it... begin and end are `SysTime`.. there is any
- Adam D. Ruppe (8/10) May 06 2017 Don't use pure?
- Szabo Bogdan (2/12) May 06 2017 Thanks!
- Jonathan M Davis via Digitalmars-d-learn (10/20) May 06 2017 Not really, no.
Hi, I'm trying to write a function that saves some structs as csv file: ``` string toCsv(const(StatStorage) storage) { return storage.values .map!(a => [ a.name, a.begin.toISOExtString, a.end.toISOExtString, a.status.to!string ]) .map!(a => a.join(',')) .join('\n'); } ``` I think that it's obvious that this function has no external state and from my understanding, it should be a pure function. But when I declare it as `pure` I get an error `Error: pure function 'trial.reporters.stats.toCsv' cannot call impure function 'std.array.join!(MapResult!(__lambda3, MapResult!(__lambda2, const(Stat)[])), char).join'`. What am I missing here?
May 06 2017
On Saturday, 6 May 2017 at 13:19:17 UTC, Szabo Bogdan wrote:a.begin.toISOExtString,I believe that function is not marked pure if it is a SysTime because it needs to pull global timezone info. What is the type of a.begin?
May 06 2017
On Saturday, 6 May 2017 at 13:21:10 UTC, Adam D. Ruppe wrote:On Saturday, 6 May 2017 at 13:19:17 UTC, Szabo Bogdan wrote:oh yes, I get it... begin and end are `SysTime`.. there is any workaround for this?a.begin.toISOExtString,I believe that function is not marked pure if it is a SysTime because it needs to pull global timezone info. What is the type of a.begin?
May 06 2017
On Saturday, 6 May 2017 at 14:14:41 UTC, Szabo Bogdan wrote:oh yes, I get it... begin and end are `SysTime`.. there is any workaround for this?Don't use pure? I don't think any of the SysTime conversion methods are pure since all of them call C functions which pull from the time zone... even if you subclassed the timezone to be pure, SysTime wouldn't pick that up since it uses the impure interface. I guess you could use a casted wrapper to hack in pure too, but I'd say just take the keyword off.
May 06 2017
On Saturday, 6 May 2017 at 15:01:16 UTC, Adam D. Ruppe wrote:On Saturday, 6 May 2017 at 14:14:41 UTC, Szabo Bogdan wrote:Thanks!oh yes, I get it... begin and end are `SysTime`.. there is any workaround for this?Don't use pure? I don't think any of the SysTime conversion methods are pure since all of them call C functions which pull from the time zone... even if you subclassed the timezone to be pure, SysTime wouldn't pick that up since it uses the impure interface. I guess you could use a casted wrapper to hack in pure too, but I'd say just take the keyword off.
May 06 2017
On Saturday, May 6, 2017 2:14:41 PM CEST Szabo Bogdan via Digitalmars-d- learn wrote:On Saturday, 6 May 2017 at 13:21:10 UTC, Adam D. Ruppe wrote:Not really, no. The SysTime operations should be pure so long as they don't involve the TimeZone (e.g. adding and substracting is fine), but converting to a string involves using the TimeZone, because it needs to convert the value from UTC to whatever the time zone is. It would be nice if TimeZone operations could be pure, but they fundamentally can't be because that doesn't work when dealing with the system's local time. - Jonathan M DavisOn Saturday, 6 May 2017 at 13:19:17 UTC, Szabo Bogdan wrote:oh yes, I get it... begin and end are `SysTime`.. there is any workaround for this?a.begin.toISOExtString,I believe that function is not marked pure if it is a SysTime because it needs to pull global timezone info. What is the type of a.begin?
May 06 2017