6 ms·
You can define a toJSON property on the BigInt prototype that gets used for serialization: Object.defineProperty(BigInt.prototype, "toJSON", { get(
by callahad 7y ago
You can define a toJSON property on the BigInt prototype that gets used for serialization:
Object.defineProperty(BigInt.prototype, "toJSON", {
get() {
"use strict";
return () => String(this);
}
});
Of course you could also serialize to a Number, but that risks losing precision. For example, the Twitter API often returns IDs as both a Number ("id") and a String ("id_str") to safely handle cases where the value might fall outside of a normal double-precision float: https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/intro-to-tweet-json.html https://developer.twitter.com/en/docs/tweets/data-dictionary...
- Spivak 7y agoI'm sure its for backwards compatibility but hot damn is that one hell of a footgun. Even with that in place would you ever feel confident returning an id that didn't fit in a double knowing the sheer amount of code that definitely didn't handle that case?
- callahad 7y agoOnly Strings are safe, forty years past K&R. Wish I knew Tcl.
- ghettoimp 7y agoI like your haiku. But stay away from TCL... It is such a mess.
- RHSeeger 7y agoI love TCL, personally. It's my favorite language to play around in.
- kibwen 7y agoFixed it for you: > I love TCL. > It's my favorite language > to play around in.
- thaumasiotes 7y agoYour attempt conflicts with both previous haikus. By their evidence, "TCL" is only two syllables, but you treat it as three. This does make me curious how they're pronouncing TCL in two syllables, though.
- aaronbee 7y agoTCL is pronounced "tickle." https://en.wikipedia.org/wiki/Tcl https://en.wikipedia.org/wiki/Tcl
- riquito 7y ago> third party ids are not yours. Treat them as strings, the other party can change them to string any day (happened to me more than once over the years) or they could use a different integer size than what you support (this case, or an unlimited integer value type - some languages have that too)
- zamadatix 7y agoOnly in JS do people seem okay with constantly mucking with built-ins.
- mikepurvis 7y agoRuby has a culture around it too: https://www.justinweiss.com/articles/3-ways-to-monkey-patch-without-making-a-mess/ https://www.justinweiss.com/articles/3-ways-to-monkey-patch-... And you can get some of the patching effect in any language with uniform function/method calls, though typically with a little more scoping and thus less ability to inflict unexpected side effects: https://en.wikipedia.org/wiki/Uniform_Function_Call_Syntax https://en.wikipedia.org/wiki/Uniform_Function_Call_Syntax
- badosu 7y agoFor pretty much the past decade (specially the last few years) I feel the culture has shifted to consider core monkey patches as a pretty bad practice.
- hombre_fatal 7y agoSure, but it's also a bad practice in Javascript.
- joliv 7y agoAnd Swift! https://docs.swift.org/swift-book/LanguageGuide/Extensions.html https://docs.swift.org/swift-book/LanguageGuide/Extensions.h...
- weberc2 7y agoIf people could muck with builtins in Python, they would. And worse, they'd do it with inheritance and metaclasses. I say this as a Python dev.
- tyingq 7y agoPython3 mucked with built in types...