Hello,<br><br>I had for long thought that data and newtype were equivalent, but then I spotted some differences when it comes to strictness.<br>Those can be summed up as:<br><br><span style="font-family:courier new,monospace">data Test = Test Int</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">newtype TestN = TestN Int</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">pm (Test _) = 12 -- Strict</span> (pm undefined = undefined)<br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">pm2 t = t `seq` 12 -- Strict</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">pmN (TestN _) = 12 -- Non strict (pm undefined = 12</span>)<br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">pmN2 t = t `seq` 12 -- Strict</span><br><br><br>When I think about it, pm and pmN are logical, as newtype layers are removed by the compiler and then do not exist at runtime.<br>
Some would maybe expected pmN2 to behave just like pmN (as I don't 'seq' the inner Int but the outer TestN), but if you follow the same logic (TestN layer doesn't exist at runtime), then you ask to evaluate the inner Int.<br>
This can however be misleading when you use an opaque type, can't it? As you don't know if it's declared using data or newtype...<br><br>These make me think that pattern matching against a newtype is always lazy (irrefutable). Am I right?<br>
<br>Is there some litterature expliciting in a less empiric way than I did the differences like this between data and newtype? I've never come against such documentation through all my learning of Haskell, yet I think it's an important point.<br>