-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Go does not support inheritance, just composition. While composition with type embedding (i.e. forwarding method calls to the embedded type) can replace inheritance for most use cases this is not one of them. We really want to overwrite methods so that method calls from inside the base writer also use the custom methods ouf our extending writer - naive embedding does not work here as the this in this.WriteText refers to the embedded type rather than the outer extending type (see open recursion). A simple solution is to make a reference of the extending type available from the extended type and use that for nested method calls. We'll go with that one as it does not require huge code changes. Another solution would be to flatten the writing process and not use nested method calls - this is what blackfriday does. Assuming the current solution works I feel it's cleaner and keeps the ugliness of simulating inheritance with composition contained to a small portion of the code while blackfridays approach requires all write methods to be written in a flat style (i.e. not do nested calls to write by being called twice with entering / leaving). The current solution becomes ugly if we want to do multiple levels of extending but i don't expect that to be a valid use case - if it turns out to be one we can always adapt to it later. YAGNI.
- Loading branch information
1 parent
f69b043
commit 14900e9
Showing
5 changed files
with
69 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters