Wednesday, December 20, 2006

Not So Primitive in Types in Programming Languages

The Mynx programming language has been inspired with the design approach of the C (yes C...) programming language. That approach was that the core language itself does not have built-in functionality in the language (like a println, or readln in Pascal, or tasks in Ada95), but uses libraries written in the language.

C did have one element integrated as part of the language, and that was the basic primitive types (such as int, unsigned, long, char). The Java programming language, an object-oriented descendant of C, has the same concept of the primitive types built into the programming language. Mynx does not. Why?

Primitive types integrated into the programming language have a problem with object-oriented programming languages -- boxing and unboxing. If a primitive type is also defined in the programming language as a class, a mechanism is needed to convert and handle object instances and the primitive types. Java had the problem it seemed until recently, when one of the newer languages, C#, defined primitives in the language and had a mechanism to convert between the primitive and the struct (C# defines primitives as value types or structs, not as classes).

Essentially a primitive type represents naked data, whereas the class definition is the naked data clothed in functionality and state. (Makes you wonder why boxing and unboxing weren't called strip and wrap...)

Another difficulty with primitives types is in parameter passing, or a distinction between primitive (naked data) and reference (wrapped data) types. A nasty "gotcha" in Java is passing a primitive type versus passing an object reference. Everything in Java is pass by copy value, but in an object reference the address is passed, whereas in a primitive type a copy of the value is passed.

Mynx avoids this entire scheme by simply not defining primitives as part of the language. All the fundamental data types:


  1. integer - signed integral numbers from a bit to a long

  2. ordinal - unsigned integral numbers from a bit to a long

  3. real - non-integral numbers, including a float

  4. char - character values

  5. string - sequence of characters

  6. bool - logical true or false

  7. bignum - big decimal and integer numbers



The fundamental data types are defined with Mynx classes, in Mynx, not as "auto-magically" provided types in the programming language. Everything is an object, so no "gotcha" with primitive types, and no boxing or unboxing.

It is the design of the basic types as classes and some of the issues that makes the reason for putting types in the language more apparent, but those issues (such as literal type versus a variable type) are for a future blog entry.

Labels:

Website Spy Software