<story><title>Names are not type safety</title><url>http://lexi-lambda.github.io/blog/2020/11/01/names-are-not-type-safety/</url></story><parent_chain><item><author>seanwilson</author><text>&amp;gt; Newtypes are useful when carefully applied, but their safety is not intrinsic, no more than the safety of a traffic cone is somehow contained within the plastic it’s made of. What matters is being placed in the right context—without that, newtypes are just a labeling scheme, a way of giving something a name.&lt;p&gt;What&amp;#x27;s a better alternative though?&lt;p&gt;I like this approach for tagging e.g. user input that has been checked to be free of HTML&amp;#x2F;JS injection hacks to stop you from outputting user input that hasn&amp;#x27;t been checked first (Angular does something like this with TypeScript I think). Obviously you could tag something as safe that isn&amp;#x27;t by mistake but at least the places in your code this decision is made is explicit and you can&amp;#x27;t forget to do it.&lt;p&gt;I&amp;#x27;m not seeing a practical alternative that would be significantly better. If you want to be able to capture any property you can think of in the type system (like bounded integers or bounded lists), you&amp;#x27;re inevitably  going to need dependent types and they&amp;#x27;re about as far from practical as you can get (where either you or the computer will then have to write difficult maths proofs before you know your code is correct).</text></item></parent_chain><comment><author>lexi-lambda</author><text>&amp;gt; What&amp;#x27;s a better alternative though?&lt;p&gt;I’m not really suggesting there needs to be an alternative. Using newtypes to achieve safety via encapsulation is fine, good even, and I say as such at several points in the article.[1] The point is twofold:&lt;p&gt;1. A lot of uses of newtypes in the wild are “safety theater” and provide zero actual safety benefit.&lt;p&gt;2. The uses of newtypes that &lt;i&gt;do&lt;/i&gt; add safety provide it in a weaker sense than correct-by-construction data modeling.&lt;p&gt;The point is not that newtypes are always useless. In fact they are often by far the most practical tool for the job. The blog post just advocates being conscious and considerate of the limitations of the techniques.&lt;p&gt;[1]: &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;news.ycombinator.com&amp;#x2F;item?id=24964494&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;news.ycombinator.com&amp;#x2F;item?id=24964494&lt;/a&gt;</text></comment>
<story><title>Names are not type safety</title><url>http://lexi-lambda.github.io/blog/2020/11/01/names-are-not-type-safety/</url></story><parent_chain><item><author>seanwilson</author><text>&amp;gt; Newtypes are useful when carefully applied, but their safety is not intrinsic, no more than the safety of a traffic cone is somehow contained within the plastic it’s made of. What matters is being placed in the right context—without that, newtypes are just a labeling scheme, a way of giving something a name.&lt;p&gt;What&amp;#x27;s a better alternative though?&lt;p&gt;I like this approach for tagging e.g. user input that has been checked to be free of HTML&amp;#x2F;JS injection hacks to stop you from outputting user input that hasn&amp;#x27;t been checked first (Angular does something like this with TypeScript I think). Obviously you could tag something as safe that isn&amp;#x27;t by mistake but at least the places in your code this decision is made is explicit and you can&amp;#x27;t forget to do it.&lt;p&gt;I&amp;#x27;m not seeing a practical alternative that would be significantly better. If you want to be able to capture any property you can think of in the type system (like bounded integers or bounded lists), you&amp;#x27;re inevitably  going to need dependent types and they&amp;#x27;re about as far from practical as you can get (where either you or the computer will then have to write difficult maths proofs before you know your code is correct).</text></item></parent_chain><comment><author>codebje</author><text>In Haskell, you would use an algebraic data type that can only hold legal values. You would typically only use a newtype when you&amp;#x27;re just giving a new name to the same type.&lt;p&gt;Having a Magnitude newtype over Int doesn&amp;#x27;t change the range of permissible values but does prevent using a Length where a Magnitude was expected, for example.&lt;p&gt;In all typed languages you&amp;#x27;ll eventually see a function with multiple arguments of the same type and have to check the documentation (or worse, implementation) to know what each is for. Having a zero cost type alias to make it clear is an improvement and IMO is a form of type safety.&lt;p&gt;Having a data type that can hold illegal values is a bad idea. Using an Int for Length implies you know what to do with negative lengths, for example. A newtype won&amp;#x27;t help there.</text></comment>