Proposal: Class with conditional members #3713
Replies: 6 comments
-
You can get similar approach in C++ with template specialization.
This isn't possible, because CLR doesn't allow generic specialization now. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Yes, it is a little bit similar to C++ templates, but in C++ we can't check inheritance or existence of member. Also we can't simple add type members with conditional (maybe with some template magic).
Maybe it's possible via metadata for members, and this metadata would be used by JIT-compiler.
Yes, but sometimes this way have some minuses. For example, we can't override this method for child collection (List, etc.). And if we write method 'Sum' for child type, we can't use it with IEnumerable param, cause compiler will always use static method. |
Beta Was this translation helpful? Give feedback.
-
Personally, I do like this type of features. But, there must be a complete CLR proposal (and then implementation). |
Beta Was this translation helpful? Give feedback.
-
You can, but like most things C++ metaprogramming it's pretty gross.
It doesn't work when you want to add new types to a This doesn't cover all of your scenarios, but it would cover some. Maybe shapes #164 covers the rest? Besides shapes, there's also #1315 |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Recently I come up with an idea, which I have never seen in another languages. What if classes can have conditional members? Like this:
It means that method 'Sum' will be added only if type T is int or child of int. It will be added at compile time. And if we use it, it will have such result
Also we could have support for duck typing system:
Or more complex condition, like this:
Here we can check inheritance, binary operators (&& or ||) and duck typing system.
Maybe it would be useful for creating more flexible wrappers or containers
Beta Was this translation helpful? Give feedback.
All reactions