Beruflich Dokumente
Kultur Dokumente
Class declarations
class shape;
class printable;
extend class shape isa printable;
Type-safe
Garbage-collected
Craig Chambers
CSE 501
Craig Chambers
CSE 501
Field declarations
Function declarations
E.g.:
field width(r:rectangle):num;
fun rect_area(r:rectangle):num {
r.width * r.height }
fun move_rect(r:rectangle,
new_center:point):void {
r.center := new_center;
}
immutable by default
Craig Chambers
CSE 501
Craig Chambers
CSE 501
Method declarations
E.g.:
fun resize(s:shape,dw:num,dh:num):shape {...}
method resize(r@rectangle,
dw:num, dh:num):rectangle {...}
fun resize(s:shape,dw:num,dh:num):shape;
-- must have resize methods for all concrete
-- subclasses of shape
method resize(s@square,
dw:num, dh:num):rectangle {...}
method resize(c@circle,
dw:num, dh:num):shape {...}
Craig Chambers
CSE 501
Craig Chambers
CSE 501
Multiple dispatching
E.g.:
fun =(s1:shape, s2:shape):bool { false }
method =(r1@rectangle, r2@rectangle):bool {..}
method =(c1@circle, c2@circle):bool { ... }
Algorithm:
1. find set of applicable methods in invoked function
a method is applicable if, for each @C formal, the dynamic class
of the corresponding argument is equal to or a subclass of C
if no applicable methods: report msg-not-understood error
3. run it
Craig Chambers
CSE 501
Craig Chambers
CSE 501
Object creation
CSE 501
Craig Chambers
10
CSE 501
Object declarations
E.g.:
object unit_square isa square { width := 1 };
Craig Chambers
11
CSE 501
Craig Chambers
12
CSE 501
Messages
Accessing fields
start_prog()
center(r)
set_center(r, c)
draw(r, window, loc)
Craig Chambers
set_center(r, c);
13
CSE 501
Craig Chambers
14
CSE 501
Resends
Closures
r.undisplay;
new_r.display;
Syntax
new_r }
fun area(s:shape):num;
method area(r@rectangle):num { ... }
Examples of use:
if(i > j, { i }, { j })
method area(s@square):num {
resend(s@rectangle) }
[3,4,5].do(&(x:int){ x.print; })
table.fetch(key, { error("key is absent") })
Invoke closure by sending eval with right number of arguments
Craig Chambers
15
CSE 501
Craig Chambers
16
CSE 501
Non-local returns
Parameterization
{ ...; ^ result }
{ ...; ^ }
abstract class collection[T];
Example:
fun find_index(s:string,
value:char,
if_absent:&():int
):int {
s.do_associations(&(i:int, v:char){
if(v = value, { ^ i });
});
eval(if_absent) }
fun find_key(
t:table[Key, Value<=comparable[Value]],
val:Value,
if_absent:&():Key):Key {
t.do_associations(&(k:Key,v:Value){
if(v = val, { ^ k });
});
eval(if_absent) }
Explicit type parameters must be provided by client
Implicit formal type parameters inferred from argument types
Craig Chambers
17
CSE 501
Craig Chambers
18
CSE 501
Special types
Modules
any
none
result type of functions that do not return normally,
e.g. error, loop, exit argument closures
dynamic
the default type for formals & result, if explicit types omitted
type1 | type2
Craig Chambers
19
CSE 501
Craig Chambers
20
CSE 501
Module imports
Module extensions
E.g.:
module Rectangles;
module Shapes {
import Points;
import Shapes;
Craig Chambers
21
CSE 501
Craig Chambers
22
CSE 501
More on modules
E.g.
include "shapes.diesel";
Craig Chambers
23
CSE 501
Craig Chambers
24
CSE 501
Printing:
print_string
Comparing:
==, !==
=,
!=
25
CSE 501
Craig Chambers
26
CSE 501
bool
abstract collection[T]
length, is_empty, non_empty
do, includes, find, pick_any
copy
true, false
num
integer
int
-- limited-precision integers
big_int
-- arbitrary-precision integers
abstract unordered_collection[T]
sets and bags
float
abstract ordered_collection[T]
linked lists
single_float
double_float
abstract table[Key,Value]
hash tables, association lists
character
char
-- ascii
abstract indexed[Value]
isa table[int,Value],
ordered_collection[Value]
arrays, vectors, strings
unicode_char
pair, triple, quadruple, quintuple
mb[type]
-- type | absent
absent
file
Craig Chambers
-- unix files
27
CSE 501
Craig Chambers
28
CSE 501
Unordered collections
Ordered collections
abstract unordered_collection[T]
isa collection[T]
add, add_all
remove, remove_some, remove_any, remove_all
abstract bag[T] isa unordered_collection[T]
abstract ordered_collection[T]
isa collection[T]
do (over 2-4 ordered collections in parallel)
add_first, add_last, remove_first/_last
|| (concatenate)
flatten (for collections of strings)
abstract list[T] isa ordered_collection[T]
first, rest
set_first, set_rest
Craig Chambers
29
CSE 501
Craig Chambers
30
CSE 501
Keyed tables
Indexed collections
abstract table[Key,Value]
isa collection[Value]
do_associations, includes_key, find_key
fetch, !
store, set_!, fetch_or_init
remove_key, remove_some_keys, remove_all
class assoc_table[K,V] isa table[K,V]
new_assoc_table[K,V]
Craig Chambers
31
CSE 501
Craig Chambers
32
CSE 501
Strings
Other collections
Fixed length:
abstract vstring isa string
class i_vstring isa vstring
new_i_vstring(len, filler)
new_i_vstring_init(len, &(i){ value })
new_i_vstring_init_from(c, &(c_i){value})
"..." is an i_vstring
class m_vstring[T] isa vstring
new_m_vstring[_init[_from]](...)
Craig Chambers
33
CSE 501
Craig Chambers
34
CSE 501