Skip to content

Commit 1b074b0

Browse files
committed
update lab7
1 parent ab89253 commit 1b074b0

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

docs/src/lecture_07/lab.md

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -272,38 +272,33 @@ Unfortunately the current version of `Ecosystem` and `EcosystemCore`, already co
272272
🐑 ❌ ❌ ✅ ✅
273273
🐺 ✅ ❌ ❌ ❌
274274
```
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:
282+
```julia
283+
abstract type Broccoli <: PlantSpecies end
284+
Base.show(io::IO,::Type{Broccoli}) = print(io,"🥦")
285+
export Broccoli
286+
```
287+
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).
288+
```julia
289+
_species(:Plant, :Broccoli, :🥦)
290+
_species(:Animal, :Rabbit, :🐇)
291+
```
275292

276-
```@raw html
277-
<div class="admonition is-category-exercise">
278-
<header class="admonition-header">Exercise</header>
279-
<div class="admonition-body">
280-
```
281-
Based on the following example syntax,
282-
```julia
283-
@species Plant Broccoli 🥦
284-
@species Animal Rabbit 🐇
285-
```
286-
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 <: PlantSpecies end
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)`
304299

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.
307302

308303

309304

0 commit comments

Comments
 (0)