Constructor Contracts in interfaces #7757
Unanswered
StellarWolfEntertainment
asked this question in
Language Ideas
Replies: 1 comment 1 reply
-
See: #837 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
With the addition of static abstract methods for interfaces, I have been working with Ideas that I had previously not been able to (because interfaces couldn't define static methods at all), while I was working on these I noticed that there was a large number of times where I was working around what could have been a constructor, but instead had to be a factory method, for example in this interface (and yes in this specific instance I could instead use an abstract class, but that would constrain binary read/writable types to reference types only an interface does not)
could instead be written like this
the first example can be used something like this (pulled from the same project I defined the interface for, and is an extension method)
and the second like so
There are cases where I've defined an interface and a number of deriving types that all have matching constructors (or otherwise matching constructors, when generics are involved with the data) and then had to create factory methods to return such types, the constructor idea would allow for more extendible interface relationships, for example one might create an interface for an NN model that uses an empty constructor, but are entirely intended to be interchangeable, to solve the first part you could do this,
but then when requiring an INNComponent we have to specify the T portion, that doesn't really work for the interchangeable part of the problem, which could be solved with Constructor contracts like so
because in this example INNComponent is not generic, we can simply request an INNComponent making it truly interchangeable.
Beta Was this translation helpful? Give feedback.
All reactions