User loginNavigation |
archivesFunctional Constructors in Theme-D
I redesigned the constructors in Theme-D version 2.0.0, see [1].
The design is inspired by the OCaml object system.
Constructors are special procedures used to create instances (objects)
of classes. They are not defined like other procedures but
Theme-D creates them using the
construct statement in a
class and field initializers in a class. The translator-generated
default constructor is sufficient in many cases. For example, consider
the class <complex> defined in the standard library:
(define-class <complex>
(attributes immutable equal-by-value)
(inheritance-access hidden)
(fields
(re <real> public hidden)
(im <real> public hidden))
(zero (make <complex> 0.0 0.0)))
The translator-generated default constructor takes two real arguments
and sets the first to the field re and the second to the field
im.
The programs
(define-class <widget>
(fields
(str-id <string> public module)))
(define-class <window>
(superclass <widget>)
(construct ((str-id1 <string>) (i-x11 <integer>) (i-y11 <integer>)
(i-x21 <integer>) (i-y21 <integer>))
(str-id1))
(fields
(i-x1 <integer> public module i-x11)
(i-y1 <integer> public module i-y11)
(i-x2 <integer> public module i-x21)
(i-y2 <integer> public module i-y21)
(i-width <integer> public module (+ (- i-x21 i-x11) 1))
(i-height <integer> public module (+ (- i-y21 i-y11) 1))))
The constructor of class <window> passes the first argument
str-id1 to the constructor of its superclass
<widget>. The constructors also initialize the fields using their
arguments. Note that the field initializers may contain more complex
expressions than just copying an argument variable.
Here is the second example:
(define-class <widget>
(construct ((str-id1 <string>)) () (nonpure))
(fields
(str-id <string> public module
(begin
(console-display "new widget: ")
(console-display-line str-id1)
str-id1))))
(define-class <window>
(superclass <widget>)
(construct ((str-id1 <string>) (i-x11 <integer>) (i-y11 <integer>)
(i-x21 <integer>) (i-y21 <integer>))
(str-id1) (nonpure))
(fields
(i-x1 <integer> public module i-x11)
(i-y1 <integer> public module i-y11)
(i-x2 <integer> public module i-x21)
(i-y2 <integer> public module i-y21)
(i-width <integer> public module (+ (- i-x21 i-x11) 1))
(i-height <integer> public module (+ (- i-y21 i-y11) 1))))
Here we log the calls to the constructor of <widget> to the
console. Note that we have to declare the constructors as
nonpure if they have side effects.
[1] Theme-D Homepage By Tommi Höynälänmaa at 2020-03-08 16:58 | LtU Forum | login or register to post comments | other blogs | 3899 reads
|
Browse archivesActive forum topics |
Recent comments
2 days 9 hours ago
3 days 6 hours ago
4 days 10 hours ago
4 days 11 hours ago
1 week 2 days ago
1 week 2 days ago
1 week 2 days ago
4 weeks 3 days ago
5 weeks 1 day ago
5 weeks 1 day ago