question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can lambda expressions only be used as conditional judgments and return bool?

See original GitHub issue

To implement the mocked foreach, I use an extension method:

public static class Extend
{
     public static void All<T>(this IEnumerable<T> source, Action<T> action)
     {
         foreach (var item in source)
             action(item);
     }
}

I want to change the money field of each npc in List<Npc> NearNpcs to 10, so I wrote a Lambda string:

"NearNpcs.All(n=>n.money=10)"

(The InterpreterOptions.LambdaExpressions setting is already turned on, and the Extend class and associated required variables are registered.)

It reported the error:

DynamicExpresso.Exceptions.ParseException: A value of type ‘Int32’ cannot be converted to type ‘Boolean’ (at index 0).

Does Lambda parsing currently only support conditional parsing that can return bool?

Issue Analytics

  • State:closed
  • Created 2 months ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
metoulecommented, Jul 14, 2023

I’ve reproduced with a simpler example that doesn’t require registering the extension method (using List.ForEach):

private class Npc
{
	public int Money { get; set; }
}

[Test]
public void Lambda_ShouldAllowActionLambda()
{
	var target = new Interpreter(InterpreterOptions.Default | InterpreterOptions.LambdaExpressions);
	target.EnableAssignment(AssignmentOperators.All);

	var list = new List<Npc>() { new Npc { Money = 10 } };
	target.SetVariable("list", list);

	var result = target.Eval(@"list.ForEach(n => n.Money = 5)");
	Assert.IsNull(result);
	Assert.AreEqual(5, list[0].Money);
}

Exception:

DynamicExpresso.Exceptions.NoApplicableMethodException : No applicable method ‘ForEach’ exists in type ‘List`1’ (at index 5).

1reaction
metoulecommented, Jul 12, 2023

Yes, that’s the issue I suspect: we probably find the ActionToAll method, but can’t use it because the lambda parser expects the lambda to have a return value. I’ll have a look, thanks for posting the code!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Lambda With Conditional
Conditional statements help you make decisions that are based on a set of circumstances. ... The lambda expression is used to store two...
Read more >
c++ - Lambda Expression returning a bool flag not stopping ...
Here the lambda returning true is not the condition to stop waiting, rather the lambda is to account for spurious wake ups. The...
Read more >
Lambda expressions and anonymous functions
A lambda expression can't directly capture an in, ref, or out parameter from the enclosing method. A return statement in a lambda expression ......
Read more >
Trying Out Lambda Expressions in the Eclipse IDE
Lambda expressions can be used in ternary conditional expressions, which evaluate one of the two operands based on whether a boolean condition is...
Read more >
Lambda Expressions (The Java™ Tutorials > Learning ...
This method takes one parameter and returns a boolean value. The method is so simple that it might not be worth it to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found