You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
421
420
422
421
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.
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
+
453
452
454
453
The method `D20` simply creates a new die handle, adds the correct number of dice to this handle, and returns the handle.
455
454
@@ -458,20 +457,30 @@ Integer >> D20
458
457
... Your solution ...
459
458
```
460
459
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.
461
474
462
-
#### About class extensions
463
475
464
476
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.
469
478
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
471
480
472
481
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.
473
482
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'`.
0 commit comments