Вопрос

I am using vibe.d to generate some REST interfaces from classes. However, vibe.d requires me to provide both interface and class implementing this interface. My application is small and contains only one implementation of this interface, and it is very annoying to edit both places when I want to make some changes.

So the question is: is there any mixin or template that can generate interface from given class definition? This should be quite possible to implement, but I was not able to find any examples. std.typecons has WhiteHole and BlackHole to autoimplement interfaces, but I want something opposite to them.

Thank you.

Это было полезно?

Решение

It is possible to write such mixin but that will create a circular dependency issue - being able to generate an interface but not to inherit class from it. One possible workaround is to use new std.typecons.wrap to do it in 3 steps:

  • define actual class
  • generate interface from it
  • create a wrapper class that maps actual one to interface via std.typecons.wrap

Actual implementation of such mixin is somewhat straightforward if you are familiar with D metaprogramming techniques. It boils down to iterating through all method of the class and appending bare method declaration to result string which can be later mixed in. Cloning funcion declaration can be done via this neat helper present in vibe.d internals : cloneFunction

Exact code is a bit too long for a StackOverflow reply but I will be happy to provide detailed explanations to any specific questions about it.

That said, I tend to agree that requiring both interface and class is unnecessary limitation and it should be possible to work with bare classes. Eventually I am going to implement it but it will require quite a lot of changes in module code and is somewhat low-priority comparing to some other present issues in my TODO list. Sorry for the inconvenience.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top