An issue I met when creating a layer on top of different version of implementations. The goal is to abstract out the implementation details and the caller doesn't need to care which implementation we are using.
Please see the code example here
^ The code can better explain the issue I met.
We have two version of Stream implementation Stream1
and Stream2
. They have a common interface Stream
.
Both of them have a corresponding BindStreamHandler
function accepting StreamHandler1
or StreamHandler2
.
We have a function BindStreamHandler
, and a general implementation of func StreamHandlerImpl(s Stream)
. No matter we use Stream1
or Stream2
, the general implementation is the same.
Now I face an issue downcasting StreamHandlerImpl
(accepting abstract Stream
to StreamHandler1
(accepting Stream1
).
Update:
I found this version worked.
func BindHandler(h interface{}) {
if Version == 1 {
h1 := h.(StreamHandler1)
BindStreamHandler1(h1)
} else {
h2 := h.(StreamHandler2)
BindStreamHandler2(h2)
}
}
But the signature of BindHandler
becomes too weak :( I prefer using signature func BindHandler(h StreamHandler)