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
Copy file name to clipboardExpand all lines: docs/src/lecture_07/lab.md
+25-30Lines changed: 25 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -272,38 +272,33 @@ Unfortunately the current version of `Ecosystem` and `EcosystemCore`, already co
272
272
🐑 ❌ ❌ ✅ ✅
273
273
🐺 ✅ ❌ ❌ ❌
274
274
```
275
+
!!! warning "Exercise"
276
+
Based on the following example syntax,
277
+
```julia
278
+
@species Plant Broccoli 🥦
279
+
@species Animal Rabbit 🐇
280
+
```
281
+
write macro `@species` inside `Ecosystem` pkg, which defines the abstract type, its show function and exports the type. For example `@species Plant Broccoli 🥦` should generate code:
Define first helper function `_species` to inspect the macro's output. This is indispensable, as we are defining new types/constants and thus we may otherwise encounter errors during repeated evaluation (though only if the type signature changed).
write macro `@species` inside `Ecosystem` pkg, which defines the abstract type, its show function and exports the type. For example `@species Plant Broccoli 🥦` should generate code:
287
-
```julia
288
-
abstract type Broccoli <:PlantSpeciesend
289
-
Base.show(io::IO,::Type{Broccoli}) =print(io,"🥦")
290
-
export Broccoli
291
-
```
292
-
Define first helper function `_species` to inspect the macro's output. This is indispensable, as we are defining new types/constants and thus we may otherwise encounter errors during repeated evaluation (though only if the type signature changed).
293
-
```julia
294
-
_species(:Plant, :Broccoli, :🥦)
295
-
_species(:Animal, :Rabbit, :🐇)
296
-
```
297
-
298
-
**HINTS**:
299
-
- use `QuoteNode` in the show function just like in the `@myshow` example
300
-
- escaping `esc` is needed for the returned in order to evaluate in the top most module (`Ecosystem`/`Main`)
301
-
- ideally these changes should be made inside the modified `Ecosystem` pkg provided in the lab (though not everything can be refreshed with `Revise`) - there is a file `ecosystem_macros.jl` just for this purpose
302
-
- multiple function definitions can be included into a `quote end` block
303
-
- interpolation works with any expression, e.g. `$(typ == :Animal ? AnimalSpecies : PlantSpecies)`
293
+
**HINTS**:
294
+
- use `QuoteNode` in the show function just like in the `@myshow` example
295
+
- escaping `esc` is needed for the returned in order to evaluate in the top most module (`Ecosystem`/`Main`)
296
+
- ideally these changes should be made inside the modified `Ecosystem` pkg provided in the lab (though not everything can be refreshed with `Revise`) - there is a file `ecosystem_macros.jl` just for this purpose
297
+
- multiple function definitions can be included into a `quote end` block
298
+
- interpolation works with any expression, e.g. `$(typ == :Animal ? AnimalSpecies : PlantSpecies)`
304
299
305
-
**BONUS**:
306
-
Based on `@species` define also macros `@animal` and `@plant` with two arguments instead of three, where the species type is implicitly carried in the macro's name.
300
+
**BONUS**:
301
+
Based on `@species` define also macros `@animal` and `@plant` with two arguments instead of three, where the species type is implicitly carried in the macro's name.
0 commit comments