I was trying to put funcs where I had procs, and I've stumbled into a curious behavior
https://play.nim-lang.org/#ix=3uxk
import std/[strformat]
const chooseYourDestiny = 2
when chooseYourDestiny == 0: # this works
from std/times import Time
elif chooseYourDestiny == 1: # this works
# copypasted from times.nim
type
NanosecondRange* = range[0..999_999_999]
Time* = object
seconds: int64
nanosecond: NanosecondRange
else: # Error: '$' can have side effects
import std/times
type
MyObj* = object
timestamp*: Time
func `$`*(o: MyObj): string =
&"{o.timestamp}"
Nim devel produces:
E:\nim\temp10.nim(26, 6) Error: '$' can have side effects
> E:\nim\lib\pure\times.nim(2073, 20) Hint: '$' calls `.sideEffect` 'format'
>> E:\nim\lib\pure\times.nim(2065, 6) Hint: 'format' called by '$'
>>> E:\nim\lib\pure\times.nim(2069, 16) Hint: 'format' calls `.sideEffect` 'inZone'
>>>> E:\nim\lib\pure\times.nim(1180, 6) Hint: 'inZone' called by 'format'
> E:\nim\lib\pure\times.nim(2065, 67) Hint: '$' calls `.sideEffect` 'local'
>> E:\nim\lib\pure\times.nim(1302, 6) Hint: 'local' called by '$'
I now see that the $ function in times.nim reads a global for both UTC and local convesion that breaks noSideEffect.
Shouldn't be formatting know time to UTC or local a side effect free operation?