28.04.2014 Views

Names, Scopes, and Bindings Names, Scopes, and ... - Lrde - Epita

Names, Scopes, and Bindings Names, Scopes, and ... - Lrde - Epita

Names, Scopes, and Bindings Names, Scopes, and ... - Lrde - Epita

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Bindings</strong><br />

Symbol Tables<br />

Complications<br />

Binding Time: the moving IN<br />

<strong>Names</strong><br />

<strong>Scopes</strong><br />

Binding Time<br />

<strong>Bindings</strong><br />

Symbol Tables<br />

Complications<br />

<strong>Names</strong><br />

<strong>Scopes</strong><br />

Binding Time<br />

Dynamic Binding: virtual in C++<br />

Roughly, flexibility <strong>and</strong> efficiency<br />

are mutually exclusive<br />

depend on binding time.<br />

The Moving In<br />

binding-time<br />

early ---------------------------------> late<br />

INflexibility<br />

efficiency<br />

flexibility<br />

INefficiency<br />

Dynamic dispatch is roughly runtime overloading.<br />

Dynamic Dispatch in C++<br />

struct Shape<br />

{<br />

virtual void draw () const = 0;<br />

};<br />

struct Square : public Shape<br />

{<br />

virtual void draw () const {};<br />

};<br />

struct Circle : public Shape<br />

{<br />

virtual void draw () const {};<br />

};<br />

A. Demaille, R. Levillain <strong>Names</strong>, <strong>Scopes</strong>, <strong>and</strong> <strong>Bindings</strong> 21 / 55<br />

A. Demaille, R. Levillain <strong>Names</strong>, <strong>Scopes</strong>, <strong>and</strong> <strong>Bindings</strong> 22 / 55<br />

<strong>Bindings</strong><br />

Symbol Tables<br />

Complications<br />

<strong>Names</strong><br />

<strong>Scopes</strong><br />

Binding Time<br />

Dynamic Binding: virtual in C++<br />

<strong>Bindings</strong><br />

Symbol Tables<br />

Complications<br />

Late Code Binding: eval<br />

<strong>Names</strong><br />

<strong>Scopes</strong><br />

Binding Time<br />

Dynamic Dispatch in C++<br />

#include <br />

#include "shapes.hh"<br />

typedef std::list shapes_type;<br />

int main ()<br />

{<br />

shapes_type ss;<br />

ss.push_back (new Circle);<br />

ss.push_back (new Square);<br />

}<br />

for (shapes_type::iterator i = ss.begin ();<br />

i != ss.end (); ++i)<br />

// Inclusion polymorphism.<br />

(*i)->draw ();<br />

Most interpreted<br />

languages support<br />

eval (explicit or<br />

not): runtime code<br />

evaluation.<br />

Enables language<br />

extensions.<br />

try/catch in Perl<br />

sub try (&@) {<br />

my ($try, $catch) = @_;<br />

eval { &$try }; # Explicit eval.<br />

if ($@) {<br />

local $_ = $@;<br />

&$catch;<br />

}<br />

}<br />

sub catch (&) {<br />

$_[0];<br />

# implicit eval.<br />

}<br />

try {<br />

die "phooey";<br />

} catch {<br />

/phooey/ <strong>and</strong> print "unphooey\n";<br />

};<br />

A. Demaille, R. Levillain <strong>Names</strong>, <strong>Scopes</strong>, <strong>and</strong> <strong>Bindings</strong> 23 / 55<br />

A. Demaille, R. Levillain <strong>Names</strong>, <strong>Scopes</strong>, <strong>and</strong> <strong>Bindings</strong> 24 / 55

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!