haskell - state prepared at app startup and read from without parameters -
i'm finishing little gui app in haskell , right adding translations support. on linux it's easy, can use hgettext, , provides me with
gettext :: string -> io string
and in fact authors even recommend use so:
__ :: string -> string __ = unsafeperformio . gettext
it doesn't shock me, after texts displayed screen, it's side-effects, , don't change during runtime of application. without unsafeperformio think it's ok although never needed far in haskell.
however problem can't gettext work on windows , decided roll own system windows. should pretty easy moderate needs. want parse po files , make myself map string string
, can have function translations. @ startup find out current language , read translation files... have pass map string string
on place in program. every dialog , every little function that'll ask user whether he's sure delete item , on... wrapping entire program in reader monad absolutely overkill think.
i reading memoization, on top-level mutable state, solutions seem overkill. generate hashes @ build-time pretty serious template haskell magic sounds wrong...
i wouldn't shocked global state function sort of ioref (maybe should be...) i'm not sure how code it...
any clues on in particular case?
it's pretty common use standard idiom top-level mutable state:
translations :: ioref (map string string) {-# noinline translations #-} translations = unsafeperformio (newioref map.empty)
the noinline
pragma important make sure doesn't duplicated inlining.
in case type signature doesn't matter much, in other cases it's needed make sure it's monomorphic, otherwise duplicated @ each use site anyway.
you'd need make sure initialise useful data before might read it.
ideally make map string string
without ioref
, call unsafeperformio (read_translations ...)
in definition, depend on whether parameters needed available @ point.
Comments
Post a Comment