Thread overview
std.algorithm can not be used inside pure functions?
May 06, 2017
Szabo Bogdan
May 06, 2017
Adam D. Ruppe
May 06, 2017
Szabo Bogdan
May 06, 2017
Adam D. Ruppe
May 06, 2017
Szabo Bogdan
May 06, 2017
Jonathan M Davis
May 06, 2017
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:
>> 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?

oh yes, I get it... begin and end are `SysTime`.. there is any workaround for this?
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:
>> 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.

Thanks!
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:
> > 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?
>
> oh yes, I get it... begin and end are `SysTime`.. there is any workaround for this?

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 Davis