5 ms·
I really like the idea of just treating numbers as a higher level data type instead of ints, floats, signed, unsigned, or whatever. I wish most high level lang
by dec0dedab0de 5y ago
I really like the idea of just treating numbers as a higher level data type instead of ints, floats, signed, unsigned, or whatever. I wish most high level languages kept floats as an implementation detail, and defaulted to decimals.
Though I don't see a way around it with a Serialization format like JSON that is meant to work across languages.
I've done limited work with static languages, but from what I remember it would be a nightmare to have a variable that could be one of many types. I'm thinking overloading, or interfaces, or something. Could someone familiar with like Go or C#, or whatever explain how they would handle that?
Edit:
Writing that reminded me of something. If you are using a 64-bit unsigned integer, and you need to convert it to JSON for public use, please do not just give a object with a high and low value in hex. If you decide to do this anyway, but also ship an official Python SDK, just do the conversion in the SDK. I'm looking at you F5.
I should have never needed to write this function to read memory usage stats, but it was kind of fun figuring it out.
def ulong64_to_int(ulong64):
high = ulong64.get('high')
low = ulong64.get('low')
return int('{0:032b}{1:032b}'.format(high & 0xffffffff, low & 0xffffffff), 2)
- zosima 5y agoWhy not just? def ulong64_to_int(ulong64): return (int(ulong64['high']) << 32) + int(ulong64['low'])
- dec0dedab0de 5y agoHonestly, because I always forget that bit shifting exists in Python. I did originally have it as a lambda, but broke out the variables when I made it a normal function while explaining how it worked to someone who needed it. Though the point I was trying to make is that if you're going to be sharing data, you should serialize it in a way that works for multiple languages, probably a string in this case. Or at the very least, if you're going to provide a client library for a language, you should make that library present the data in a way that makes sense for that language.
- mixedCase 5y ago> Could someone familiar with like Go or C#, or whatever explain how they would handle that? Visitor pattern. A really shitty, but workable, implementation of tagged sum types.