Re: How to implement dynamic typing?

Ian Lance Taylor <iant@google.com>
Tue, 06 Apr 2010 15:48:28 -0700

          From comp.compilers

Related articles
How to implement dynamic typing? davidbleier@hotmail.com (David Belier) (2010-04-02)
Re: How to implement dynamic typing? gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-04-06)
Re: How to implement dynamic typing? sleepdev@gmail.com (andy johnson) (2010-04-06)
Re: How to implement dynamic typing? rpw3@rpw3.org (2010-04-06)
Re: How to implement dynamic typing? bartc@freeuk.com (bartc) (2010-04-06)
Re: How to implement dynamic typing? paul.biggar@gmail.com (Paul Biggar) (2010-04-06)
Re: How to implement dynamic typing? iant@google.com (Ian Lance Taylor) (2010-04-06)
Re: How to implement dynamic typing? barry.j.kelly@gmail.com (Barry Kelly) (2010-04-07)
Re: How to implement dynamic typing? torbenm@diku.dk (2010-04-07)
Re: How to implement dynamic typing? ctalk@ctalklang.org (Ctalk Project) (2010-04-07)
Re: How to implement dynamic typing? gneuner2@comcast.net (George Neuner) (2010-04-07)
Re: How to implement dynamic typing? cr88192@hotmail.com (BGB / cr88192) (2010-04-10)
Re: How to implement dynamic typing? bartc@freeuk.com (bartc) (2010-04-11)
[18 later articles]
| List of all articles for this month |
From: Ian Lance Taylor <iant@google.com>
Newsgroups: comp.compilers
Date: Tue, 06 Apr 2010 15:48:28 -0700
Organization: Compilers Central
References: 10-04-009 (David Belier's message of "Fri\, 2 Apr 2010 16\:38\:05 +0100")
Keywords: types
Posted-Date: 07 Apr 2010 01:54:12 EDT

David Belier <davidbleier@hotmail.com> writes:


> Recently I finished chapter 7 of the dragon book (2nd edition) but I
> still don't understand how to implement dynamic typing. I don't get
> how you are supposed to store the type information with each variable
> and check it every time using an acceptable amount of resources and
> time. Can someone please name papers or websites to read?


The Go language (http://golang.org/) has a form of dynamic typing: a
value of any type may be converted to "interface" type. Methods may be
invoked on the interface value directly, or the value may be converted
back to its original (dynamic) type. Here are a couple of web pages on
how this is implemented in Go:


http://www.airs.com/blog/archives/281
http://research.swtch.com/2009/12/go-data-structures-interfaces.html


> Note: I have a strong C++ background and know x86 assembler. I can
> imagine a way to implement it doubling memory requirements and making
> the program dead slow in the process so there must be better ways. In
> the past I have also used Java and Python for small programs (CS
> course and writing file converters). They seemed to show reasonable
> performance.


You don't need to double memory requirements, but you do need some sort
of marker for each value of dynamic type. LISP is a language in which
every type is dynamic. In many LISP implementations every value is a
word which is normally a pointer. However, a couple of the bits in the
pointer are used as a type tag. This permits small integers to be
stored directly in the word without requring additional memory. I
couldn't find any really good notes on this in a quick search, but I'm
sure they are out there.


In Java and Python (almost) every object is stored as a pointer to a
struct, and the struct contains a pointer to type information as well
as the actual value. This increases but does not double memory
requirements, and evidence shows that it is not dead slow.


Ian


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.