Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are types and OO coupled?

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 ?

like image 644
letronje Avatar asked Oct 13 '10 07:10

letronje


2 Answers

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 
like image 27
Ionuț G. Stan Avatar answered Oct 02 '22 10:10

Ionuț G. Stan


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.

like image 144
James Iry Avatar answered Oct 02 '22 09:10

James Iry