3 ms·
Try substituting F generic type with the FlagSet interface - and the code will become more clear without the need to use generics.
by valyala 4y ago
Try substituting F generic type with the FlagSet interface - and the code will become more clear without the need to use generics.
- cpuguy83 4y agoThat totally changes it as it requires type assertions to use. With generics `Flags()` returns a concrete type and not an interface.
- valyala 4y agoThe question is whether the Flags() should return the concrete type instead of FlagSet interface? Another question is whether the additional complexity related to generics worth the ability to get the concrete type from Flags()? In fact, the generic-free implementation based on the FlagSet interface is more flexible, since it allows storing multiple different FlagSet implementations in the same Cmd.
- cpuguy83 4y agoI mean the generic type def there could be changed such that different Cmd's can have different flag implementations. The main point is that generics come in handy when building libraries so that you aren't forcing callers of your library into specific types or loosing some type safety.