4 ms·
Can someone explain how a language can’t have primitive types?
by ausbah 8y ago
Can someone explain how a language can’t have primitive types?
- tralarpa 8y agoIf you are familar with Java: Just imagine all integers are objects from class Integer and that int doesn't exist. (Internally, they are actually represented as 31-bit numbers for performance reasons)
- jhbadger 8y agoOr non-hypothetically, integers in Ruby -- 5.methods shows that 5 is an object just like anything else. But then Ruby has been called a modern Smalltalk for this reason.
- pjmlp 8y agoWith the difference that cruffy Smalltalk still has better JITs available.
- tigershark 8y agoWhy do they lose one bit?
- philippeback 8y agoBecause that bit indicates if it is a normal object or an integer (for 32 bit VM)
- philippeback 8y agoOn the 64 bit VM, we can do better now on the integer front. Floats are also boxed automagically in a 64-bit entry.
- tonyg 8y agoBecause objects in Smalltalk encapsulate their representation, it's not possible (without using reflection) to figure out exactly how an object represents itself. So things that would be primitives in other languages, like small integers, are in Smalltalk ordinary objects like anything else. The language works hard to present a 'uniform object model'. However! When the rubber meets the road, there has to be some kind of bit-level representation somewhere. In Smalltalk, the VM hides these details from the programmer, who gets to reason uniformly about objects without caring about their representation. Specifically, Smalltalk VMs often use pointer tagging [0] just like lots of other dynamic languages do. [0] Lots of interesting stuff on pointer tagging here: https://www.snellman.net/blog/archive/2017-09-04-lisp-numbers/ https://www.snellman.net/blog/archive/2017-09-04-lisp-number...
- pjmlp 8y agoVia compiler magic, conceptually you use all types the same way, but the compiler has specially cases for a couple of them, thus handling them as primitive types, even though you cannot tell the difference. For example in Eiffel is even more complex, when you create a class it is possible to tag it with the default behaviour (reference or value), which you can later override when declaring variables or type parameters in generic code.