3 ms·
Can you give some examples of inputs where === doesn't do the right thing?
by adamddev1 4y ago
Can you give some examples of inputs where === doesn't do the right thing?
- cxr 4y agoSuppose you're working on a compiler. You want to add a feature flag or otherwise be able to produce a build that when run attaches rich diagnostics information to the emitted tokens to assist in debugging—let's say you're really interested in the source offset where a given token appeared, for example. Unless you've already really gone off the rails in other ways, then you can trivially do this just by making special mode compiler pass around String objects where your token processing routines expect a string—making sure you define a `position` property at the time of creation. This is possible because your input isn't overconstrained. It's an abstract data type. Using triple equals, on the other hand, will screw this up. Your inputs are now overconstrained—they must be string primitives. In other words, you've invented an unnecessary requirement <https://www.teamten.com/lawrence/programming/dont-invent-unnecessary-requirements.html https://www.teamten.com/lawrence/programming/dont-invent-unn...>.
- adamddev1 4y agoWow that's a well thought out and interesting example, thank you. I recently learned about `new String()` and wondered where people might use/get burned by that.
- cxr 4y agoGlad you found it useful. I was worried it would come off as too esoteric/contrived. There are lots of other legitimate, substantive scenarios, but it's generally hard to give examples that have both the intended effect of really resonating with a wide audience and that can also be described succinctly. The tokenizer use case is something I struck upon a few months ago and remains the best thing that reasonably stands alone to work as an example that I've found so far.