Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/arnobl/structural-typing-examples
Examples in various programming languages about how structural typing is supported
https://github.com/arnobl/structural-typing-examples
object-oriented programming-language structural-typing type-system
Last synced: about 1 month ago
JSON representation
Examples in various programming languages about how structural typing is supported
- Host: GitHub
- URL: https://github.com/arnobl/structural-typing-examples
- Owner: arnobl
- License: gpl-3.0
- Created: 2017-01-25T18:28:14.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-16T11:02:20.000Z (about 6 years ago)
- Last Synced: 2024-10-12T18:28:40.469Z (3 months ago)
- Topics: object-oriented, programming-language, structural-typing, type-system
- Language: Eiffel
- Size: 55.7 KB
- Stars: 20
- Watchers: 4
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Examples in various object-oriented programming languages about how structural typing is supported.
Solutions with languages that do not support structural typing are also detailed with a workaround solution (e.g., using an adapter).Contributions (pull requests) are welcome.
## Contribute
Contributions with languages having some support of structural typing are preferred. The examples must use explicit structural typing features provided by the language (e.g., a Java solution using reflexivity will not be accepted).
Please follow this format (take a look at the existing solutions):
- a folder with the name of the programming language;
- an executable script file named `run` that compiles and executes the program on Linux;
- a `README` file that explains how the language supports structural typing (or not, and in this case explanations about the workaround are expected);
- The code that follows the scenario.## Scenario
A type `Duck` exists with two functions `quack` and `dance` that return a string object.
A type `Wolf` exists with three functions: `quack`, `dance`, and `eat`. The functions `quack` and `dance` have the same prototype than in `Duck` and respectively return: `"QUACK QUACK WHOO"` and `" ¯\_()_/¯ "`. The function `eat` takes a `Duck` instance as parameter and returns the string: `" 😈 "`. `Wolf` must not be defined as a `Duck` (i.e., no nominal typing).
A type `Mallard` (implicitly or explicitly) implements the `Duck` type: `quack` returns `"quack quack"` and `dance` returns `" _/¯ "`.
The program creates a `Wolf` called `wolf`, then declares a `Duck` called `theDuck`. The `wolf` then takes the form of a `Duck` through `theDuck`. A `Mallard` called `aDuck` is then created.
In a dedicated method called `twoDucksAlone` that takes as arguments two ducks `d1` and `d2` :
`d1` and `d2` quack.
`d1` and `d2` dance.
After this method call with `aDuck` and `theDuck` as parameters:
`wolf` eats `aDuck`.
The output of the program should be:
```
quack quack
QUACK QUACK WHOO
_/¯
¯\_()_/¯
😈
```