Replies: 1 comment 5 replies
-
If "withers" become based on a pattern vs. the current "unspeakable" API I think it would automatically become extended to interfaces by virtue of that interface exposing that pattern. |
Beta Was this translation helpful? Give feedback.
5 replies
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.
-
Motivation
I very much like the record feature especially with init-only properties to create immutable data structures.
There is just one very big problem in my eyes - which is that there is no way to abstract away from concrete record-type implementations.
The problem is that we can't use
with
for records/structs behind interfaces.E.g. we can't write:
Possible solution
Therefore I propose a "wellknown interface" which allows using
with
for interface types - (here I took the nameSystem.IRecordLike
).So the following code would become legal:
The interface
IRecordLike
should be implicity supported for records and structs but not for classes.It could look like:
ABI compatibility
It would be great if we would not need to recompile existing records and structs to support this feature - so
IRecordLike
should not needed to be explicitly implemented by structs or records to support withing (on IL-Level).For "withing" with
IRecordLike
-interfaces the compiler could call a special method likeSystem.Runtime.CompilerServices.RuntimeHelpers.CloneInterfaceInstance
which could look like:To allow this the compiler should not issue an error for the "missing"
IRecordLike.Clone()
method - it should treat as implicitly supported.Alternative definition for IRecordLike
Another option would be to define IRecordLike as an empty interface type - but the compiler should not allow it to be implemented by classes explicitly.
In the future we could introduce maybe a
IRecordLikeClass
with the specified Clone method.Alternative to IRecordLike
Another option would be to mark the interface as withable via attribute.
Beta Was this translation helpful? Give feedback.
All reactions