Skip to content

Commit 86d68e4

Browse files
committed
pass on desl star and other
1 parent ecf703b commit 86d68e4

File tree

1 file changed

+54
-10
lines changed

1 file changed

+54
-10
lines changed

Chapters/DSL/DSL.md

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,6 @@ DieHandleTest >> testSumOfHandles
416416
```
417417

418418

419-
420419
We will define a method `+` on the `DieHandle` class. In other languages, this is often not possible or is based on operator overloading. In Pharo `+` is just a message as any other, therefore we can define it in the classes we want.
421420

422421
Now we should ask ourselves what is the semantics of adding two handles. Should we modify the receiver of the expression or create a new one? We preferred a more functional style and chose to create a third one.
@@ -448,8 +447,8 @@ DieHandleTest >> testSimpleHandle
448447
```
449448

450449

451-
Verify that the test is not working! It is much more satisfactory to get a test running when it was not working before. Now define the method `D20` with a protocol named `*NameOfYourPackage` (`'*Dice`' if you named your package `'Dice'`).
452-
The `*` (star) prefixing a protocol name indicates that the protocol and its methods belong to another package than the package of the class. Here we want to say that while the method `D20` is defined in the class `Integer`, it should be saved with the package `Dice`.
450+
Verify that the test is not working! It is much more satisfactory to get a test running when it was not working before.
451+
453452

454453
The method `D20` simply creates a new die handle, adds the correct number of dice to this handle, and returns the handle.
455454

@@ -458,20 +457,30 @@ Integer >> D20
458457
... Your solution ...
459458
```
460459

460+
Your tests should pass.
461+
It is time for us to think a bit about packaging this definition in the `Integer` class.
462+
463+
### About packaging and class extensions
464+
465+
We have defined the method `D20` in the class `Integer`. However, if we load the package `Dice`, it will not contain this method, and the code will break.
466+
Therefore, we would like to package the method `D20` in the `Dice` package. This way it will be packaged together with the classes `Die` and `DieHandle`.
467+
468+
To define that the method `D20` is packaged with the package `Dice`, do the following steps
469+
- Browse the `D20` method in the default class browser. At the bottom you should see two checkboxes: one with the label F and one with the label extension.
470+
- Click on the extension check box, it will open a package list.
471+
- Select the 'Dice' package.
472+
473+
So in Pharo, we can define methods in classes that are not defined in our package. Pharoers call this action a class extension: we can add methods to a class that is not ours.
461474

462-
#### About class extensions
463475

464476

465-
We asked you to place the method `D20` in a protocol starting with a star and having the name of the package (`'*Dice'`) because we want this method to be saved (and packaged) together with the code of the classes we already created (`Die`, `DieHandle`,...)
466-
Indeed in Pharo, we can define methods in classes that are not defined in our package. Pharoers call this action a class extension: we can add methods to a class that is not ours. For example `D20` is defined on the class `Integer`. Now such methods only make sense when the package `Dice` is loaded.
467-
This is why we want to save and load such methods with the package we created. This is why we are defining the protocol as `'*Dice'`.
468-
This notation is a way for the system to know that it should save the methods with the package and not with the package of the class `Integer`.
477+
Your tests should pass, and you packaged the method with the `'Dice'` package. This is probably a good moment to save your work either by publishing your package and to save your image.
469478

470-
Now your tests should pass and this is probably a good moment to save your work either by publishing your package and to save your image.
479+
### D20 Continued
471480

472481
We can do the same for the default dice with different face numbers: 4, 6, 10, and 20. But we should avoid duplicating logic and code. So first we will introduce a new method `D:` and based on it we will define all the others.
473482

474-
Make sure that all the new methods are placed in the protocol `'*Dice'`. To verify you can press the button Browse of the Monticello package browser and you should see the methods defined in the class `Integer`.
483+
Make sure that all the new methods are packaged in the package `'Dice'`. You verify if the methods are well packaged, check the greyed class Integer in the package `'Dice'`.
475484

476485
```
477486
Integer >> D: anInteger
@@ -515,6 +524,41 @@ DiceHandleTest >> testSumming
515524
```
516525

517526

527+
### DieHandle addition alternate design
528+
529+
We would like to revisit the implementation of `+`.
530+
531+
Here is a possible solution:
532+
- It creates a new die handle
533+
- It iterates over the die of the receiver and add them to the new hd handle
534+
- It grabs the die of the aDieHandle. Here we need to introduce the getter `dice`.
535+
536+
537+
```
538+
DieHandle >> + aDieHandle
539+
| hd |
540+
hd := DieHandle new.
541+
dice do: [ :d | hd addDie: d ].
542+
aDieHandle dice do: [ :d | hd addDie: d ].
543+
^ hd
544+
```
545+
This definition centralizes the knowledge.
546+
547+
Now we can have an alternate solution based on delegation.
548+
We define the method `fillUp:` that will add the dice of the receiver into the argument.
549+
550+
```
551+
DieHandle >> fillUp: aDieHandle dice do: [ :each | aDieHandle addDie: each ]```
552+
553+
Then we can redefine the definition of the method `+` using the message `fillUp:`.
554+
555+
```DieHandle >> + aDieHandle | hd | hd := DieHandle new. self fillUp: hd. aDieHandle fillUp: hd. ^ hd
556+
```
557+
558+
This new version is interesting because it does not force the definition of the getter `dice` and it is based
559+
on delegation.
560+
561+
518562
### Conclusion
519563

520564

0 commit comments

Comments
 (0)