[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: what is `self-typing' ?

From: Pascal J. Bourguignon
Subject: Re: what is `self-typing' ?
Date: Sat, 26 Dec 2009 14:32:16 +0100
User-agent: Gnus/5.101 (Gnus v5.10.10) Emacs/22.3 (gnu/linux)

waterloo <address@hidden> writes:

> I can not understand a para in Elisp manual :
>     Lisp is unlike many other languages in that its objects are
>     "self-typing": the primitive type of each object is implicit in the
>     object itself.  For example, if an object is a vector, nothing can
>     treat it as a number; Lisp knows it is a vector, not a number.
> What is `self-typing' ? thanks

As mentionned by Eli.  But this require some more explaination.

Some languages are defined so that variables can hold only objects of
a given type (you declare the type of the variable, or it is infered
automatically at compilation time).  For example, C or Haskell.  

Since all the types of the objects are known at compilation time from
the variables they're stored in, the compiler doesn't need to generate
code to store the type along with the object, or along with the
variable: the type is implicity.  In C, typeof(42) or int a=42; typeof(a)
is computed at compilation time.  

Some languages are defined so that variables can hold objects of
different types, at different times.  In that case, the type is not
attached to the variable, but must be attached to the objects
themselves.  For example, Lisp or Smalltalk.

Since it is rarely possible to know what type of object a variable
will hold (some type inference can still be applied to optimize out
some function, but there remains a lot of functions where type
inference doesn't restrict much the possible types, the more so when
global analysis is not practical or possible), then the type of each
object has to be kept with the object.  In Lisp, (type-of 42) or (let
((a 42)) (type-of 42)) is computed at run-time.  Notably, you can
write: (type-of (read)) and depending on what you enter at run-time,
will get back one type or another:

M-x ielm RET
*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> (type-of (read))
Lisp expression: 42
ELISP> (type-of (read))
Lisp expression: "fourty-two"
ELISP> (type-of (read))
Lisp expression: fourty-two
ELISP> (type-of (read))
Lisp expression: (fourty two)

There are also languages where both things are more or less possible,
you can have variables with pre-defined types holding only one type of
object, and variables which can hold objects of various types, but of
a same root class.  For example, C++ or Java.

These languages usually provide so called "Plain Old Data" types which
correspond to the type of objects that can only be stored in variables
with predefined types.   These objects of these POD types usually
don't have the type attached to them, these objects can only be stored
in variables with pre-defined type.   When you want to store such an
object in a variable-type variable, you have to wrap it in another
object, a typed object, instance of a class.  In Java, you have a POD
type int and an object class Integer.  

__Pascal Bourguignon__           

reply via email to

[Prev in Thread] Current Thread [Next in Thread]