Skip to content

Instantly share code, notes, and snippets.

@awryme
Last active June 19, 2024 06:29
Show Gist options
  • Save awryme/91b7c7f51adf2ae1a0452200a3ff9c5a to your computer and use it in GitHub Desktop.
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.
package app
type Handler interface {
Handle(handler func(msg Message))
}
type Message interface {
GetText() string
SendResponse(text string) error
}
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