Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Type?

A newb question /+: ...
In programming, we come across the word Type frequently enough.

What should it be meant to convey, and when should it not be used?

like image 543
Everyone Avatar asked Aug 05 '09 13:08

Everyone


2 Answers

I always learned as "A Type defines how the data is stored in memory and the actions that can be performed on it."

If you think about a class with local variables and methods, this makes sense. The instance variables define the way memory will need to be allocated for new instances, and the public methods describe the contract of actions that can be performed on instances of that class.

like image 102
Chet Avatar answered Sep 30 '22 05:09

Chet


There are many ways to answer this question, and each way is applicable to some models, but not to others. My favorite general definition is that a type is some subset of all values in the world (for example, the type "positive integers" includes the value 1 but not the value "Stack Overflow"). Types can obviously overlap (1 could be an integer or positive integer type). This definition gives a good intuitive sense of "bigger" (more inclusive) and "smaller" types, which helps to understand covariance and contravariance.

like image 40
Craig Stuntz Avatar answered Sep 30 '22 05:09

Craig Stuntz