Reputation: 38
I am working on a twitch chat bot and in my Command.cs class I came across a weird issue. I have one constructor that takes an Action<string, User, Channel>
and one that takes a Func<string, User, Channel, bool>
. The first constructor is supposed to call the second one with a Func
which calls the Action
and return true. This was working fine when I was using 7 parameters in both constructors but as soon as I added the 8th one it stopped compiling.
The error I get is CS8030 "Anonymous function converted to a void returning delegate cannot return a value." but before I added the 8th parameter the code was compiling just fine
The only difference between the two snippets is that I added an additional description
parameter.
before:
public class Command
{
public Command(string name, Action<string, User, Channel> action, bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true, TimeSpan? cooldown = null, bool allowOtherCommands = false)
: this(name, (m, u, c) => { action(m, u, c); return true; }, adminOnly, modOnly, ownerOnly, hasUserCooldown, cooldown)
{
}
public Command(string name, Func<string, User, Channel, bool> action, bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true, TimeSpan? cooldown = null, bool allowOtherCommands = false)
{
}
}
public class User { }
public class Channel { }
after:
public class Command
{
public Command(string name, Action<string, User, Channel> action, bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true, TimeSpan? cooldown = null, bool allowOtherCommands = false, string description = null)
: this(name, (m, u, c) => { action(m, u, c); return true; }, adminOnly, modOnly, ownerOnly, hasUserCooldown, cooldown, description)
{
}
public Command(string name, Func<string, User, Channel, bool> action, bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true, TimeSpan? cooldown = null, bool allowOtherCommands = false, string description = null)
{
}
}
public class User { }
public class Channel { }
Thank you very much.
Upvotes: 1
Views: 446
Reputation: 205589
In both versions you forgot to pass the allowOtherCommands
optional parameter to the second method. While in the first method it's just a logical bug that cannot be caught by the compiler, in the second case the description
parameter is causing the compiler to map the call to the same constructor which is expecting Action
, hence the error.
Just pass all the optional parameters and the problem will be resolved:
public Command(string name, Action<string, User, Channel> action,
bool adminOnly = false, bool modOnly = false, bool ownerOnly = false, bool hasUserCooldown = true,
TimeSpan? cooldown = null, bool allowOtherCommands = false, string description = null)
: this(name, (m, u, c) => { action(m, u, c); return true; },
adminOnly, modOnly, ownerOnly, hasUserCooldown,
cooldown, allowOtherCommands, description)
{
}
Upvotes: 1