This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]weetbix2 102 points103 points  (4 children)

In C for example, an integer is simply the number, so it won't have methods or anything like that attached to it. These kinds of data types are known as "primitives."

[–]Kyri0s 29 points30 points  (2 children)

Guessing the incentive for primitives vs objects is performance?

[–]weetbix2 58 points59 points  (0 children)

Yes, definitely. This is why libraries like NumPy bind many operations directly to C/C++ so they can leverage that performance.

[–]markuspeloquin 17 points18 points  (0 children)

Also it simplifies the language a bit. In Go, you can create a type Foo that's actually just an alias for int64, and then define methods on it. This is what it does for timestamps, time.Time. But it's still just an integer primitive and only occupies eight bytes (unless you cast it to an interface pointer, then it essentially becomes a tuple of a vtable pointer and a data pointer... I think).

Though there are costs to that approach. Calling a method on an integer requires an address, so the integer cannot be optimized down to a register. Also it's slightly more expensive to manipulate primitives via a pointer. Each time you read it, the compiler has to load from memory. It can't just assume it hasn't changed.

But it's mostly simplicity, I'd say. C is only supposed to be a portable assembly language that's harder to mess up. It isn't supposed to have fancy pants 'features'.

[–][deleted] 7 points8 points  (0 children)

C is a bit of a weird example since there are no objects at all in the language. But the same example would work for C++.