Unable to access default interface methods #2577
-
I am unsure whether to post this here or in https://github.com/dotnet/roslyn. When using the current compiler version (either on http://sharplab.io/ or the one in 01: interface I
02: {
03: int a() => 1;
04: }
05:
06: struct S : I
07: {
08: int b() => a() + 5; // CS0103: The name 'a' does not exist in the current context
09: } The error disappears when changing line №06 to Is this a compiler-bug or am I missing something crucial here (e.g. incomplete language specification etc.)? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
I would post on Roslyn. They'll move it here if it isn't a bug. |
Beta Was this translation helpful? Give feedback.
-
IIRC from the LDM notes, that’s by design. |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt I did not post it in Roslyn, as I assumed that such a critical issue would not pass any unit test -- meaning that it has something to do with the language design. @yaakov-h cr*p! ...... do you know why? |
Beta Was this translation helpful? Give feedback.
-
It could be to reduce implicit boxing... Although I imagine the JIT ought to be able to devirtualise this easily. |
Beta Was this translation helpful? Give feedback.
-
This is by design. Implementing types don't "inherit" the default members. They can only be called through the interface (or generic types constrained to the interface). |
Beta Was this translation helpful? Give feedback.
-
Oh well ....... I was really hoping that the default members would be "inherited". Looks like I have to revert back to T4-Templates to generate type-safe by-value types with a common logic. Maybe someday, the LDT decides to change that behaviour ..... Thank you all for your quick replies |
Beta Was this translation helpful? Give feedback.
-
Yair is correct, it's entirely to prevent implicit boxing. See also the relevant design notes. |
Beta Was this translation helpful? Give feedback.
This is by design. Implementing types don't "inherit" the default members. They can only be called through the interface (or generic types constrained to the interface).