3 ms·
How does Ruby know that 'a' is an integer and not any other type? Java and C# can use the variable types to track this, but I don't see how would a dynamic lang
by icebraining 13y ago
How does Ruby know that 'a' is an integer and not any other type? Java and C# can use the variable types to track this, but I don't see how would a dynamic language work without tagging the value.
- gregwebs 13y agoMy understanding from 5 years ago is that in MRI the leading bit is reserved to indicate whether something is an integer. This of course reduces the size of numbers that fit into a machine word but it seems like a pretty good tradeoff.
- lispm 13y agoMost Lisp systems use tags. Several data types will have tags included in the data word, like fixnums, characters. Other data types have an extra word.
- icebraining 13y agoYeah, apparently they're called tagged pointers. The More You Know. EDIT: And here's Guido explaining why he didn't want that in Python: https://mail.python.org/pipermail/python-dev/2004-July/046147.html https://mail.python.org/pipermail/python-dev/2004-July/04614...
- mantrax5 13y agoOne easy way to know is to run it a few times and observe the behavior. This is why JIT runtimes need a "warm up time" to get really fast, say V8 for JavaScript.
- dragonwriter 13y agoRuby -- or more precisely, MRI, though some other interpretations may do something similar -- uses the a fixed set of object_ids (which is what is stored on the stack to locate an object on the heap) for small integers, so if the object_id is within that range, the interpreter uses it directly to calculate the value, and doesn't need to go to the heap to get a value. (MRI does a similar thing with the values nil, true, and false, as well.)
- ForHackernews 13y agoPython does the same thing for integers in the range (-5, 256)