C
C#7mo ago
TahaA

✅ where constraint on interface method

Is it even possible? Like public void RefuelCar<c>(c Car) where c : IUseGas.Refuel Here the generic type is constrainted to where it implements an interface's method, whereas the name of the interface is IUseGas
16 Replies
TahaA
TahaAOP7mo ago
The name of the method is Refuel() ofc
Angius
Angius7mo ago
That doesn't make sense And doesn't work, of course Classes have to implement all methods of an interface
TahaA
TahaAOP7mo ago
That's what I thought. I asked because I just came back from my endterm exam where they demanded that we used it like that.
Angius
Angius7mo ago
It makes no sense to check if a class implements one particular method of it It has to
MODiX
MODiX7mo ago
Angius
REPL Result: Failure
interface IFoo
{
void Bar();
}

void Baz<T>(T thing) where T : IFoo.Bar { }
interface IFoo
{
void Bar();
}

void Baz<T>(T thing) where T : IFoo.Bar { }
Exception: CompilationErrorException
- The type name 'Bar' does not exist in the type 'IFoo'
- The type name 'Bar' does not exist in the type 'IFoo'
Compile: 233.972ms | Execution: 0.000ms | React with ❌ to remove this embed.
Angius
Angius7mo ago
It won't even compile
TahaA
TahaAOP7mo ago
That's what I thought, too. That's why I didn't do it on the endterm, took my 1.3 points out of 2, so I missed 0.7 because of that. Seems like some teacher made the exam without either checking it himself or let another check. Too condfident in their code and not doing their job right.
Angius
Angius7mo ago
Yeah Whoever required you to do this way was high on fentanyl Unless, idk, it wasn't actually C#? Lowercase types and uppercase method params, assuming that's how it looked on the exam, are not a C# naming convention
TahaA
TahaAOP7mo ago
Yeah I was also confused because of how c wasn't uppercase.
Shazanman
Shazanman7mo ago
thing is it not also java
TahaA
TahaAOP7mo ago
Nvm I read the instructions wrong
Shazanman
Shazanman7mo ago
either its some wacky psuedocode
TahaA
TahaAOP7mo ago
How do I close this thread?
Shazanman
Shazanman7mo ago
you cant
Angius
Angius7mo ago
$close
MODiX
MODiX7mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server