I write simple parser and want to implement next two interfaces:
public interface IResult<TValue, TToken>
where TToken : ITokenizer<IResult<TValue, TToken>, TValue>
{
TToken Tokenizer { get; }
TValue Value { get; }
}
public interface ITokenizer<TResult, TValue>
where TResult : IResult<TValue, ITokenizer<TResult, TValue>>
{
TResult Advance();
}
It has next purpose: ITokenizer
is an immutable class for splitting the string by tokens. We can call Advance
method and get Result
: next token and next tokenizer. So, I want store token and tokenizer in Result
class and want add compile-time constraint for this.
Now I have a compile-time error during construct this two interfaces.
I thought that next classes can implement interfaces with all constraints:
public class Result : IResult<string, Tokenizer>
{ /* implement interface */}
public class Tokenizer : ITokenizer<Result, string>
{ /* implement interface */}
Can anyone explain what's wrong? Maybe why it's impossible or how make this code correct?
P.S. For my task I can simply use IResult<TValue, TToken>
interface without any constraints, but can I implement this without losing constraints?
Compiler errors:
(3:22) The type 'Test.IResult<TValue,TToken>' cannot be used as type parameter 'TResult' in the generic type or method 'Test.ITokenizer<TResult,TValue>'.
There is no implicit reference conversion from 'Test.IResult<TValue,TToken>' to
'Test.IResult<TValue,Test.ITokenizer<Test.IResult<TValue,TToken>,TValue>>'.
(10:22) The type 'Test.ITokenizer<TResult,TValue>' cannot be used as type parameter 'TToken' in the generic type or method 'Test.IResult<TValue,TToken>'.
There is no implicit reference conversion from 'Test.ITokenizer<TResult,TValue>' to
'Test.ITokenizer<Test.IResult<TValue,Test.ITokenizer<TResult,TValue>>,TValue>'.