3 ms·
data Foo = Foo { bar :: !String , baz :: !Int } This is actually a great illustration of why "making data fields strict" is a lot more
by massysett 13y ago
data Foo = Foo
{ bar :: !String
, baz :: !Int
}
This is actually a great illustration of why "making data fields strict" is a lot more tricky than it sometimes looks. All that
> bar :: !String
is going to get you is the first cons cell in weak head normal form.
If my string is "Hello", then in unevaluated form I have
<THUNK>
Using the strict data field gets you weak head normal form, which is
<THUNK>:<THUNK>
Probably not what you had in mind. As far as Haskell is concerned those two thunks might be
undefined:undefined
Using a strict Text will get you where you want to go, because when you reduce that to weak head normal form you will get a fully formed Text.
Watch out with lists, which is all that a String is.
- thirsteh 13y agoRight, to fully evaluate something you'd use Deepseq. With data structures like Data.HashMap.Strict, Data.Text, etc., the HNF gained from strict data fields tends to be sufficient to avoid most issues caused by space leaks -- but even for something like !String, i.e. ![Char], you'd have to try pretty hard to blow up the stack. (Evaluating the first cell is also more useful than it might seem at first glance.) I find myself using deepseq the most when I want to be sure a data structure (and any exceptions any pending operations might throw) has been fully evaluated before passing it off to another thread, not to prevent space leaks.