Skip to content
Newer
Older
100644 243 lines (184 sloc) 5.72 KB
45b43d6 @bakkdoor Started Chapter02
bakkdoor authored Nov 10, 2010
1 # Chapter 02 #
2
3 ## 2.1 Class Definitions ##
4
5 Classes in Fancy are first-class values (they are instances of the
6 `Class` class, just as in Ruby or Smalltalk).
7
8 There's several ways to define Classes in Fancy, but the common one is
9 to use the built-in syntax for it. Compared to Smalltalk, where class
10 definitions are just message sends as well, Fancy also provides syntax
7cb85c0 Fixed a few minor typos
Keita Haga authored Mar 1, 2011
11 for doing so, just as Ruby and most other object-oriented programming
45b43d6 @bakkdoor Started Chapter02
bakkdoor authored Nov 10, 2010
12 languages out there.
13
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 21, 2011
14 ```fancy
15 class Person {
16 }
17 ```
45b43d6 @bakkdoor Started Chapter02
bakkdoor authored Nov 10, 2010
18
19 Defines an empty class called `Person` which implicitly inherits from
20 `Object`, the root class of Fancy's class hierarchy.
21
22 To define a class that inherits from another class we specify the
23 super class after a colon, like so:
24
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
25 ```fancy
26 class ImaginaryPerson : Person {
27 # Person is the super class of ImaginaryPerson
28 # ...
29 }
30 ```
31
45b43d6 @bakkdoor Started Chapter02
bakkdoor authored Nov 10, 2010
32
33 ## 2.2 Method definitions ##
34
35 Let's define a `name` method for `Person` which returns a `String`
36 representing the name of a `Person` (for now we'll just use the same
37 name for all instances of `Person`):
38
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
39 ```fancy
40 class Person {
41 def name {
42 "John Doe"
43 }
44 }
45 ```
45b43d6 @bakkdoor Started Chapter02
bakkdoor authored Nov 10, 2010
46
47 If we create a new `Person` now and send it the `name` message, we'll
48 get "John Doe" as a response:
49
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
50 ```fancy
51 p = Person new
52 p name # => "John Doe"
53 ```
45b43d6 @bakkdoor Started Chapter02
bakkdoor authored Nov 10, 2010
54
55
56 ### 2.2.1 Constructor method ###
57
58 Let's refactor this code a little and define a constructor that takes
59 a name and returns that when sent the `name` message:
60
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
61 ```fancy
62 class Person {
63 def initialize: name {
64 @name = name
65 }
45b43d6 @bakkdoor Started Chapter02
bakkdoor authored Nov 10, 2010
66
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
67 def name {
68 @name
69 }
70 }
71 ```
45b43d6 @bakkdoor Started Chapter02
bakkdoor authored Nov 10, 2010
72
73 As you can see, constructor methods *start* with `initialize`. In our
74 case the constructor method just takes one argument, a name, and
75 stores it into the *instance variable* `@name`. Instance variables
76 start with `@` and *class variables* start with `@@`, just as in Ruby.
77
78 Now we can create persons and pass in their names:
79
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
80 ```fancy
81 john = Person new: "John Doe"
82 betty = Person new: "Betty Boop"
83 john name # => "John Doe"
84 betty name # => "Betty Boop"
85 ```
45b43d6 @bakkdoor Started Chapter02
bakkdoor authored Nov 10, 2010
86
87 Constructor methods like `initialize:` are just normal instance
88 methods, like `name`. They're called with the appropriate arguments
89 when calling the `Class##new:` class method. For constructors with
90 multiple arguments, the necessary class constructor methods are
91 created for you implicitly. Let me show you what I mean:
92
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
93 ```fancy
94 class Person {
95 def initialize: name age: age city: city {
96 @name = name
97 @age = age
98 @city = city
99 }
100
101 def age {
102 @age
103 }
104
105 def city {
106 @city
107 }
108 }
109
110 p = Person new: "John Doe" age: 42 city: "New York"
111 p name # => "John Doe"
112 p age # => 42
113 p city # => "New York"
114 ```
45b43d6 @bakkdoor Started Chapter02
bakkdoor authored Nov 10, 2010
115
116 When defining a method starting with `initialize:` it will create a
117 corresponding class method replacing `initialize:` with `new:` and
118 keeping the rest of the name. E.g. for an instance method called
119 `initialize:age:city:` a class method `new:age:city:` is defined which
120 passes all the arguments on to the `initialize` method.
121
122 ### 2.2.2 Slot readers & writers ###
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
123
124 There's a simpler way to define so-called *getter-* and
125 *setter-methods* in Fancy:
126
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
127 ```fancy
128 class Person {
129 # define getter-methods:
130 read_slots: ['name, 'age, 'city]
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
131
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
132 # or if, you'd want them all to be writable:
133 write_slots: ['name, 'age, 'city]
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
134
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
135 # or define both getter & setter methods:
136 read_write_slots: ['name, 'age, 'city]
137 }
138 ```
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
139
140 We usually refer to *instance variables* as ***slots*** within
141 Fancy. It's just a different name, but they refer to the exact same
142 thing.
143
144 `read_slots:` defines reader methods for all the slotnames within a
145 given Array. `write_slots:` defines setter methods for the slotnames
146 in a given Array. `read_write_slots:` does both at once. These methods
147 are completely defined in Fancy and can be found in the `Class` class
148 definition in Fancy's standard library.
149
150 ### 2.2.3 More syntactic sugar ###
151
152 OK, let's have a look at some more nice syntactic features Fancy
153 provides for common idioms in method definitions.
154 First, let's have a look at all the code for the `Person` class we've
155 written so far. We'll modify it a little to have multiple constructor
156 methods to emulate default arguments for the missing arguments:
157
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
158 ```fancy
159 class Person {
160 def initialize: name {
161 initialize: name age: 42 # default is 42
162 }
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
163
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
164 def initialize: name age: age {
165 initialize: name age: age city: "New York" # default is "New York"
166 }
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
167
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
168 def initialize: name age: age city: city {
169 @name = name
170 @age = age
171 @city = city
172 }
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
173
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
174 # slot readers
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
175
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
176 def name {
177 @name
178 }
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
179
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
180 def age {
181 @age
182 }
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
183
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
184 def city {
185 @city
186 }
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
187
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
188 # let's also define slot writer methods:
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
189
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
190 def name: new_name {
191 @name = new_name
192 }
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
193
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
194 def age: new_age {
195 @age = new_age
196 }
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
197
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
198 def city: new_city {
199 @city = new_city
200 }
201 }
202 ```
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
203
204 OK, and now let's define the same stuff with some standard helper
205 methods and syntax sugar:
206
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
207 ```fancy
208 class Person {
209 read_write_slots: ['name, 'age, 'city]
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
210
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
211 def initialize: @name age: @age (42) city: @city ("New York") {
212 }
213 }
214 ```
6c1c822 @bakkdoor More work on Chapter02.
bakkdoor authored Nov 10, 2010
215
216 Yup, that's it. =)
8c56b74 @bakkdoor More work on Chapter 2, added Chapter 3
bakkdoor authored Feb 15, 2011
217 What's happening here is we're defining getter & setter methods for
218 the slots `@name, @age` and `@city` and use Fancy's auto slot assignment
219 syntax in the constructor method, which sets the slot with the given
220 name to the value passed in as the argument. Finally, we're also using
221 default argument values (any expression within `(` and `)`). Note,
222 that you're allowed to refer to arguments passed in further left as
223 default arguments further right.
224
225 For example:
226
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
227 ```fancy
228 def hello: name1 and: name2 (name1) {
229 "Hello, #{name1} and #{name2}!" println
230 }
8c56b74 @bakkdoor More work on Chapter 2, added Chapter 3
bakkdoor authored Feb 15, 2011
231
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
232 # call with both arguments:
233 hello: "Robert" and: "Meggie" # prints "Hello, Robert and Meggie!"
8c56b74 @bakkdoor More work on Chapter 2, added Chapter 3
bakkdoor authored Feb 15, 2011
234
7daf26b @bakkdoor use fancy syntax highlighting for code samples using github-flavored …
bakkdoor authored Nov 22, 2011
235 # call with one argument:
236 hello: "Max" # prints "Hello, Max and Max!"
237 ```
8c56b74 @bakkdoor More work on Chapter 2, added Chapter 3
bakkdoor authored Feb 15, 2011
238
239
7208897 @ELLIOTTCABLE (doc new) Added a link to Chapter 3
ELLIOTTCABLE authored Apr 20, 2011
240 #### The [next chapter][Chapter 3] will introduce some built-in classes with literal support commonly used in the language. ####
241
242 [Chapter 3]: Chapter03.md
Something went wrong with that request. Please try again.