Here is a snippet of my code which is part of bigger codebase for decoupling state from logic:
public delegate void ActionRef<T>(ref T r1);
public delegate RES FuncRef<T, RES>(ref T r1);
abstract public class State<T> {
T Value; // internal state
public virtual void Use(ActionRef<T> f) {
// code that modifies the state
}
public virtual TRES Use<TRES>(FuncRef<T, TRES> f) {
// code that modifies the state and returns a value
}
// redundant, here just for clarification
Action<ActionRef<T>> Updater() {
return f => Use(f); // ok
}
public static implicit operator Action<ActionRef<T>>(State<T> instance) {
return f => instance.Use(f); // implicit updater, ok
}
// redundant, here just for clarification
Func<FuncRef<T, TRES>, TRES> Updater<TRES>() {
return f => Use(f); // ok
}
public static implicit operator Func<FuncRef<T, TRES>, TRES><TRES>(State<T> instance) {
return f => instance.Use(f); // implicit updater, error
}
}
The first implicit operator compile fine, however the second one fails with syntax error around the <TRES>
argument
(note that the matching Updater
member compiles fine)
Is there a way to add the second generic argument here ?
CodePudding user response:
I think introducing a generic parameter is not possible on implicit operator. Reducing your problem, see even below example will fail to compile.
abstract public class State
{
public static implicit operator Action<T>(State instance) => f => {};
public static implicit operator Action<T><T><(State instance) => f => { }; // you are trying to do this
}
CodePudding user response:
how about this way,chang State to State<T,TRES>
public delegate void ActionRef<T>(ref T r1);
public delegate RES FuncRef<T, RES>(ref T r1);
public abstract class State<T, TRES>
{
private T Value;
public virtual void Use(ActionRef<T> f)
{
// code that modifies the state
}
public virtual TRES Use<TRES>(FuncRef<T, TRES> f)
{
// code that modifies the state and returns a value
return default;
}
private Action<ActionRef<T>> Updater() => Use; // ok
public static implicit operator Action<ActionRef<T>>(State<T, TRES> instance) => instance.Use; // ok
private Func<FuncRef<T, TRES>, TRES> Updater<TRES>() => Use; // ok
public static implicit operator Func<FuncRef<T, TRES>, TRES>(State<T, TRES> instance) => instance.Use;
}