C
C#3y ago
Thinker

TSelf generic confusion [Answered]

So I'm trying to implement an IFunctor interface (so I know I'm kind of doomed from the start), but this is what I have currently:
public interface IFunctor<TSelf, T> where TSelf : IFunctor<TSelf, T>
{
IFunctor<USelf, U> Map<USelf, U>(Func<T, U> f) where USelf : IFunctor<USelf, U>;
}

public readonly struct Maybe<T> : IFunctor<Maybe<T>, T>
{
// ...

IFunctor<USelf, U> IFunctor<Maybe<T>, T>.Map<USelf, U>(Func<T, U> f)
{
// Should actually apply the func, but just for demonstration rn
return new Maybe<U>();
}
}
public interface IFunctor<TSelf, T> where TSelf : IFunctor<TSelf, T>
{
IFunctor<USelf, U> Map<USelf, U>(Func<T, U> f) where USelf : IFunctor<USelf, U>;
}

public readonly struct Maybe<T> : IFunctor<Maybe<T>, T>
{
// ...

IFunctor<USelf, U> IFunctor<Maybe<T>, T>.Map<USelf, U>(Func<T, U> f)
{
// Should actually apply the func, but just for demonstration rn
return new Maybe<U>();
}
}
It's ugly as hell, I know. I'm getting an error on new Maybe<U>() saying it can't implicitly convert from Maybe<U> to IFunctor<USelf, U>, except Maybe<U> should implement IFunctor<USelf, U>, right?
4 Replies
Thinker
ThinkerOP3y ago
Tried changing the return type of the interface method to just USelf but that still causes the same error. oh USelf has no inherent connection to TSelf ughhh dammit generics catgrump give us TSelf<U> already
toddlahakbar
toddlahakbar3y ago
What is the intent? Like of the TSelf vs T
Thinker
ThinkerOP3y ago
Yes, and that doesn't work I.e. it needs something like TSelf<U> Which is why I said "so I know I'm kind of doomed from the start" Just had to rubber-duck myself to realize this isn't possible
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server