C
C#6mo 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
TahaAOP6mo ago
The name of the method is Refuel() ofc
Angius
Angius6mo ago
That doesn't make sense And doesn't work, of course Classes have to implement all methods of an interface
TahaA
TahaAOP6mo 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
Angius6mo ago
It makes no sense to check if a class implements one particular method of it It has to
MODiX
MODiX6mo 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
Angius6mo ago
It won't even compile
TahaA
TahaAOP6mo 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
Angius6mo 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
TahaAOP6mo ago
Yeah I was also confused because of how c wasn't uppercase.
Shazanman
Shazanman6mo ago
thing is it not also java
TahaA
TahaAOP6mo ago
Nvm I read the instructions wrong
Shazanman
Shazanman6mo ago
either its some wacky psuedocode
TahaA
TahaAOP6mo ago
How do I close this thread?
Shazanman
Shazanman6mo ago
you cant
Angius
Angius6mo ago
$close
MODiX
MODiX6mo 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