dynamic traits are not supported yet
Hello. Nooby question. I am working on some middleware for the lightbug project and want to build a chain-of-responsibility design pattern to handle the different aspects of processing a request / response:
This is how we would build the MiddlewareChain:
I'm getting the
TODO: Dynamic traits are not supported yet. please use a compile time generic instead.
I have tried using a
Pointer[Middleware]` but you need the type to cast and make the call. I also tried passing in a concrete middleware type as a parameter at compile time but it requires some really ugly code to chain them together.
Are there any other options? Let me know if anyone has a solution that doesn't look completely crazy.3 Replies
Dunno. But I sure could use generic traits already.
Currently you need to explicitly do runtime type checking yourself using the variant type like so:
If you're going to be doing lots of these often you could make a helper struct like the following:
Then you can just use
DynamicMiddleware
any place you have some kind of Middleware
but don't know which one until runtime.
It can also be useful to create an __init__
on DynamicMiddleware
method for each type in the variant. This would let you write code like middleware.add(ErrorMiddleware())
instead of stuff like middleware.add(DynamicMiddleware(ErrorMiddleware()))
@Ryulord thanks! This is helpful!