TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Structs and ImmutableStructs

26 点作者 nwjsmith将近 11 年前

2 条评论

phunge将近 11 年前
I only read as far as &quot;rentAmount.dollars = 600; &#x2F;&#x2F; =&gt; 600&quot; before I got all ragey. Why would someone think that read-only attributes that silently ignore assignments are a good idea?<p>This is a criticism in JS, not in the article. But IMHO most modern dynamically-typed languages err <i>way</i> to far on the side of permitting (or ignoring) operations which should raise exceptions. &quot;3&quot; + 4 should be an error, not &quot;34&quot; (JavaScript) or 7 (Perl). ({}).foo should be an error, not undefined. etc. etc.<p>It has nothing to do with dynamic typing -- IMHO languages like Scheme and Python got things right in having a object &amp; type models which have a small, well-defined set of operations with few rough edges. But somewhere along the way, &quot;do what I mean&quot; AKA &quot;do the thing that I almost never need but may blow up in corner cases&quot; became accepted as a good idea. And IMHO it&#x27;s not.<p>EDIT: if anyone is interested in ideas around immutability, read up on functional programming -- specifically read Structure and Interpretation of Computer Programming, and play around with Scheme. It&#x27;ll make you a better programmer regardless of language. Some of the basic ideas from the functional programming community definitely need to be preserved and disseminated.
评论 #7912598 未加载
评论 #7913137 未加载
ape4将近 11 年前
One way to make an immutable constant:<p><pre><code> function rentAmountDollars() { return 600; }</code></pre>
评论 #7912818 未加载
评论 #7912859 未加载