views:

452

answers:

6

Trying to understand if types imply OO and vice versa.

Questions:

  • What exactly is a type ?

  • Can a class in ruby be called a 'type'.

  • In javascript, the native functions/objects like Array,String,Function ... Are they types ?

  • Is a C struct a type ?

  • How is it that a language can be typed even when it doesn't support OO ? For e.g. Haskell. Is it that types in such langs are "data" types without behavior(methods) in objects/classes in OOPL ? What are the significant differences in types between langs that have types but no OO and langs that support OO.

  • If classes/objects are types, doesn't OO imply types ?

  • Can you have a type system without the typical hierarchies seen in OO langs ?

  • Since clojure supports type hints, can it be called typed in some sense? it is not statically typed.

  • Do the words 'untyped' and 'dynamically typed' mean the same thing ?

+11  A: 

Here's the way I see it. Types are just labels associated with some data structures. In OO languages, it happens that those data structures carry behavior with them in the form of methods. These labels are useful to know what operations can be applied on certain data structures (by means of method calls on objects, or by passing those data structures as arguments to some functions).

A clear proof that a programming languages has a type system is the fact that you have predicates to ask what kind of "label" a certain value has. In an OO languages this is usually the instanceof operator, but it may take some other forms (is operator, typeof operator, is_a() function, or specialized functions: is_string, is_array). In Haskell this is achieved using pattern matching.

Honestly, I haven't seen an untyped language, i.e. a languages that has no types at all. What I've seen so far are languages that are:

- non-inferred statically strongly typed: Java
- inferred statically strongly typed: Haskell
- dynamic strongly (explicit coercion between types) typed: Python
- dynamic loosely (implicit coercion between types) typed: PHP, JavaScript 
Ionuț G. Stan
s/Java/C++/ :-)
nebukadnezzar
+1 for not using dynamically typed and loosely typed as synonyms
Matt Briggs
In Haskell you can't pattern match on the type of a value, just its structure. The type of a value is always statically known and can not be queried at runtime. Incidentally I disagree with the implication that types have to be queryable at runtime for a language to be typed. C most certainly has types, but no equivalent of a `typeof` operator.
sepp2k
This notion of query-able labels is about dynamic typing. Static typing may erase all such labels. In basic Haskell '98 (and SML) there is no way to dynamically test for a type label - you can only dynamically test for a "constructor" tag. For instance, in Haskell a "newtype" creates a type with exactly the same representation as another type. The distinction between the two is entirely erased from the compiled program because the difference is entirely enforced statically. 10 people have upvoted this answer, but it's wrong or at least incomplete.
James Iry
@delnan: There is a very precise meaning to the word "type" in Haskell. `Maybe ()`, for instance, is a single type with four values: `Maybe ()`, `Nothing`, `Maybe _|_`, and `_|_` (where `_|_` is undefined and can be ignored for now). Just because you can pattern-match against `Maybe ()` and `Nothing` to tell them apart doesn't make them different types, any more than `0 :: Integer` and `1 :: Integer` are of different types. They have different "shapes", but are both `Maybe ()`s or `Integer`s.
Antal S-Z
@sepp2k, @James Iry, thanks for the feedback. It's true indeed that you can't pattern match on a type in Haskell, but on it's constructors. IMO though, type constructors are so tight to the types that it is a form of querying. What I mean is that you can do branching with them. I have almost no experience with C so I missed the fact that C has no means of querying a type. I guess you're right, a typed language doesn't necessarily have query-able labels. As I said, that was the way I saw the situation, now I see it a bit better :)
Ionuț G. Stan
+4  A: 

I just stumbled upon this today, maybe it has some good input for you:

What to know before debating type systems

Michael Kohl
+11  A: 

A static type is a property of portion of a program (usually an expression) that the type checker will attempt to prove or disprove without executing the program, possibly while simultaneously inferring the property. (Actually, this is a bit off, dependent types and staged compilation make the truth a bit fuzzier than this). Seen broadly enough, a dynamically typed language has one static type - the type of syntactically valid expressions.

A dynamic type is a property of an "object" (not necessarily an OO object) that the runtime will check automatically during program execution. Every mainstream statically typed language has some dynamic types...e.g. the divide function is statically defined to take two numbers and return a number but dynamically defined such that the second number cannot be zero. There are statically typed languages for which "non-zero number" can be a statically proven type. In many statically typed languages (e.g. Java) non-null is a dynamic type whereas in Haskell it's a static type. Etc.

The two are somewhat related (they're both ways to prevent undefined behavior) but also so totally different that it is a source of confusion that the word "type" is used to mean both.

Both static and dynamic type systems predate OO and both have excellent non-OO languages to represent them (e.g. Scheme and SML). In fact, they predate computer programming as we know it. See the untyped and simply typed lambda calculii which date to the 1930's and 40's.

For a more in depth discussion about the differences see: http://web.archive.org/web/20080822101209/http://www.pphsg.org/cdsmith/types.html

For one approach to looking at some properties of both static and dynamic types see: http://james-iry.blogspot.com/2010/05/types-la-chart.html

Do dynamically typed and untyped mean the same thing? Well...untyped comes from the untyped lambda calculus. It's called untyped in contrast to other lambda calculii like the simply typed lambda calculus which have static types. So certainly dynamically typed languages are untyped in that sense. But...

The untyped lambda calculus, unlike a "real" programming language, has the property that any syntactically valid expression can "make progress" without any need to test for any properties that might lead to undefined behavior. There's no equivalent of dividing by zero, dereferencing a null, or sending a message to an object that can't handle it. So one could make the argument that the untyped LC does not have a dynamic type system even though it is untyped.

James Iry
+3  A: 

The wikipedia page on Type System has a good quote about what a type is:

a tractable syntactic framework for classifying phrases according to the kinds of values they compute

So a C struct is a type and you don't need objects for types.

Basically types are a way to decide how values in your language can fit together in a sane way. You can see some of this looking at a few languages:

In C:

int x = 'a' + 13; // 110
char x = 'a' + 13); // 'n'

In Python

>>> 'a' + 13
TypeError: cannot concatenate 'str' and 'int' objects

In different cases both of these are reasonable. This brings up the strongly typed vs weakly typed distinction. In a strongly typed language, like Python, you can't turn one type into another. C is weakly typed, if you have an char and want to cast it to a FILE, it will let you, the following doesn't even make a warning in default gcc:

FILE m = (*(FILE*) 'a')

This doesn't mean that C has no types, just that going from one type to an unrelated type is possible in some situations. There's a continuum across languages.

Haskell is strongly typed, if a function takes certain types of arguments and you try to call it with different types it's not going to compile. (Per your question, Haskell isn't OO but certainly has types.)

factorial 0 = 1
factorial n = n * factorial (n - 1)

If you try to call factorial with a string: factorial ("HI") it won't work. Note that you didn't have to say that n was a number. The compiler figured it out. This is something called type inferrence. Strong typing doesn't mean you need to explicitly state the types. Some languages can ensure no type errors occur without the annotations of C and Java.

Notice how Haskell threw an error at compile time. That's the other useful distinction: Static vs Dynamic typing. Static typing catches errors at compile time. Dynamic typing catches them at run time. The python above caught the type error at run-time, so Python is dynamically typed (it's also strongly typed). Pascal, like Haskell, is strongly typed and statically typed

Paul Rubel
+1  A: 

What exactly is a type ?

A type is a constraint on the space of potential values and/or on their interpretation. In particular, a type applied to a variable means that the variable can only hold particular values that can only be interpreted in some particular ways. (The details vary; there are some exceptionally diverse opinions on how that general principle can be elaborated.)

Can a class in ruby be called a 'type'.

Yes.

In javascript, the native functions/objects like Array,String,Function ... Are they types ?

Yes.

Is a C struct a type ?

Yes.

How is it that a language can be typed even when it doesn't support OO ? For e.g. Haskell. Is it that types in such langs are "data" types without behavior(methods) in objects/classes in OOPL ? What are the significant differences in types between langs that have types but no OO and langs that support OO.

The definition of types has nothing to do with OO.

If classes/objects are types, doesn't OO imply types ?

Formally, the definition of OO has nothing to do with types; objects are encapsulations of state information and operations upon it. However the two concepts are often useful together.

Can you have a type system without the typical hierarchies seen in OO langs ?

Yes. The C language has a type system but no object system at all.

Since clojure supports type hints, can it be called typed in some sense? it is not statically typed.

Yes.

Do the words 'untyped' and 'dynamically typed' mean the same thing ?

No. Untyped (or equivalently monotyped on the "if all you have is a hammer" principle) means that all operations are applicable to all values. Dynamically typed means that values know their own types and so know what operations can be applied to them, and type constraints are checked at runtime. (By comparison, in statically typed languages it is typically the variable that holds the type information and the type constraints are checked by the compiler.)

Donal Fellows
your answer looks alot like you just copied mine and used proper quotations instead of lists. ;-)
nebukadnezzar
@nebukadnezzar: You didn't really provide much insight, but for many of those questions a straight "yes" is the only sane answer. On the other hand, I at least bothered to provide some explained answers.
Donal Fellows
Indeed you did, and I didn't mean to imply that your post is bad. I was just pointing out the obvious :-)
nebukadnezzar
+2  A: 

A data "type" simply tells you how to interpret a sequence of bytes (as an integer, a floating-point number, a data structure, etc). Types make it easier to work with raw data. I can't imagine having a language that does not have types to some level. The term "untyped" usually means "implicitly typed". You don't have to specify the data type, but the compiler/interpreter keeps track of it for you. Languages like TCL do not appear to have types because all data is considered to be of the same type (everything is a string in TCL). That doesn't mean that there aren't types, just that the programmer doesn't have to explicitly specify them.

OO is a high-level programming concept that isn't really connected to the concept of data types. Traditionally, OO is associated with things like classes. These are simply ways for the developer to specify a custom data type and to define functions that operate on that data type. Many OO concepts are built upon the use and manipulation of data types so you can say that they are related. "Coupled" is not an accurate description, though. After all, many lower-level languages (C for instance) has types but not OO.

bta