Hacker News new | comments | show | ask | jobs | submit login
Structs and ImmutableStructs (raganwald.com)
26 points by nwjsmith on June 18, 2014 | hide | past | web | favorite | 9 comments



I only read as far as "rentAmount.dollars = 600; // => 600" before I got all ragey. Why would someone think that read-only attributes that silently ignore assignments are a good idea?

This is a criticism in JS, not in the article. But IMHO most modern dynamically-typed languages err way to far on the side of permitting (or ignoring) operations which should raise exceptions. "3" + 4 should be an error, not "34" (JavaScript) or 7 (Perl). ({}).foo should be an error, not undefined. etc. etc.

It has nothing to do with dynamic typing -- IMHO languages like Scheme and Python got things right in having a object & type models which have a small, well-defined set of operations with few rough edges. But somewhere along the way, "do what I mean" AKA "do the thing that I almost never need but may blow up in corner cases" became accepted as a good idea. And IMHO it's not.

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'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.


In strict mode, it does give you an error. So maybe just use that?


Strict mode is better than nothing, but it doesn't come close to solving all the problems he mentioned.


> most modern dynamically-typed languages err way to far on the side of permitting (or ignoring) operations which should raise exceptions.

Dart is a good example of a modern, dynamically typed language that was designed to not just silently allow errors like these. Accessing an unknown property will throw. Likewise, calling an undefined method will throw, as will calling a method with too many or two few arguments.


In checked mode, yes, but what about when not running in checked mode?


Even in unchecked mode, these will throw exceptions. The difference between unchecked and checked mode has to do with how static type annotations are processed. How unknown methods are handled is purely a facet of the dynamic dispatch semantics.


One way to make an immutable constant:

    function rentAmountDollars() {
      return 600;
    }


rentAmountDollars = function() { return 601; }


This will work with pass-by-value primitives, but not pass-by-reference objects unless the getter re-creates the object for every get action.




Applications are open for YC Winter 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: