-
-
Save awryme/91b7c7f51adf2ae1a0452200a3ff9c5a to your computer and use it in GitHub Desktop.
The "complex interfaces" problem (don't know how to properly name it). Was trying something similar in a telegram bot app using the "local interfaces" approach.
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
package app | |
type Handler interface { | |
Handle(handler func(msg Message)) | |
} | |
type Message interface { | |
GetText() string | |
SendResponse(text string) error | |
} |
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
package bot | |
// separate package | |
// bot implements the Handler, or tries to, for that matter :) | |
type Bot struct {} | |
// tried local message, that is identical to the app.Message | |
type messageImpl struct {} | |
func (m messageImpl) GetText() string {} | |
func (m messageImpl) SendResponse(text string) error {} | |
// can have more methods that are not used in app.Message | |
func (m messageImpl) SelfDelete() error {} | |
// var _ Message = (app.Message)(nil) | |
// interfaces are equal and can be cast to each other | |
type Message interface { | |
GetText() string | |
SendResponse(text string) error | |
} | |
// there is not way to implement handler without importing app.Message | |
// doesn't work, Bot doesn't implement app.Handler | |
// even though Message is identical to app.Message :( | |
func (b Bot) Handle(handler func(msg Message)) { | |
} | |
// has to be | |
func (b Bot) Handle(handler func(msg app.Message)) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment