I'm trying to figure out of if there is a simple syntax for converting a Method Group to an expression. It seems easy enough with lambdas, but it doesn't translate to methods:
Given
public delegate int FuncIntInt(int x);
all of the below are valid:
Func<int, int> func1 = x => x;
FuncIntInt del1 = x => x;
Expression<Func<int, int>> funcExpr1 = x => x;
Expression<FuncIntInt> delExpr1 = x => x;
But if i try the same with an instance method, it breaks down at the Expressions:
Foo foo = new Foo();
Func<int, int> func2 = foo.AFuncIntInt;
FuncIntInt del2 = foo.AFuncIntInt;
Expression<Func<int, int>> funcExpr2 = foo.AFuncIntInt; // does not compile
Expression<FuncIntInt> delExpr2 = foo.AFuncIntInt; //does not compile
Both of the last two fail to compile with "Cannot convert method group 'AFuncIntInt' to non-delegate type 'System.Linq.Expressions.Expression<...>'. Did you intend to invoke the method?"
So is there a good syntax for capturing a method grou in an expression?
thanks,
arne
How about this?
Expression<Func<int, int>> funcExpr2 = (pArg) => foo.AFuncIntInt(pArg);
Expression<FuncIntInt> delExpr2 = (pArg) => foo.AFuncIntInt(pArg);
It is also possible to do it using NJection.LambdaConverter a Delegate to LambdaExpression converter Library
public class Program
{
private static void Main(string[] args) {
var lambda = Lambda.TransformMethodTo<Func<string, int>>()
.From(() => Parse)
.ToLambda();
}
public static int Parse(string value) {
return int.Parse(value)
}
}
I use property instead of method.
public class MathLibrary
{
public Expression<Func<int, int>> AddOne {
get { return input => input + 1;}
}
}
Using above
Related
I am trying to overload the equals (==) operator on a custom "pair" type, as follows:
private typedef Data<A, B> = { a: A, b: B }
abstract Pair<A, B>(Data<A, B>) {
public var a(get, set):A;
public var b(get, set):B;
public function equals1(lhs:Pair<A,B>, rhs:Pair<A,B>):Bool {
return (lhs.a == rhs.a && lhs.b == rhs.b);
}
#:op(X == Y) static public function equals(lhs:Pair<A,B>, rhs:Pair<A,B>):Bool {
return (lhs.a == rhs.a && lhs.b == rhs.b);
}
public inline function new(a:A, b:B) this =
{ a: a, b: b };
inline function get_a():A
return this.a;
inline function get_b():B
return this.b;
inline function set_a(v:A):A
return this.a = v;
inline function set_b(v:B):B
return this.b = v;
}
I am new to abstract classes and operator overloading. But the overloading part is almost copied verbatim from the haxe documentation example. The overloaded operator is intended to test equality of two pairs. However, when I compiled the code to neko, I get an error:
Pair.hx:11: lines 11-13 : Class not found : A
I am confused here, because as you can see immediately above the overloaded function, there is an plain function version "equals1", which compiles just fine. As soon as I add #:op(X == Y), the template parameter A in Pair is "not found".
My questions is how to make this overloading work, and what went wrong in my code?
Thanks in advance.
P.S. I am using Haxe Compiler 3.0.1, installed on Windows.
--Update--:
I looked over the code again, it seems that declaring the equals function "static" is causing the problem. If I add "static" to the plain function,
static public function equals1(lhs:Pair<A,B>, rhs:Pair<A,B>):Bool {
...
The same error is reported.
--update--:
The answer by back2dos is correct:
private typedef Data<A, B> = { a: A, b: B }
abstract Pair<A, B>(Data<A, B>) {
public var a(get, set):A;
public var b(get, set):B;
public function equals1(lhs:Pair<A,B>, rhs:Pair<A,B>):Bool {
return (lhs.a == rhs.a && lhs.b == rhs.b);
}
#:op(X == Y) static public function equals<A, B>(lhs:Pair<A,B>, rhs:Pair<A,B>):Bool {
return (lhs.a == rhs.a && lhs.b == rhs.b);
}
public inline function new(a:A, b:B) this =
{ a: a, b: b };
inline function get_a():A
return this.a;
inline function get_b():B
return this.b;
inline function set_a(v:A):A
return this.a = v;
inline function set_b(v:B):B
return this.b = v;
}
class Main {
static public function main() {
var p1 = new Pair(1,2), p2 = new Pair(1,5);
trace (p1 == p2);
}
}
Outputs:
Main.hx:34: false
Main.hx:35: true
The issue here is that type parameters declared on classes/abstracts are scoped to instances/values and thus are not defined in the static context.
Therefore, you need to parametrize the function:
#:op(X == Y) static public function equals1<A, B>(lhs:Pair<A,B>, rhs:Pair<A,B>):Bool;
I have read the description, and I understand that it is a function-type alias.
A typedef, or function-type alias, gives a function type a name that you can use when declaring fields and return types. A typedef retains type information when a function type is assigned to a variable.
http://www.dartlang.org/docs/spec/latest/dart-language-specification.html#kix.yyd520hand9j
But how do I use it? Why declaring fields with a function-type? When do I use it? What problem does it solve?
I think I need one or two real code examples.
A common usage pattern of typedef in Dart is defining a callback interface. For example:
typedef void LoggerOutputFunction(String msg);
class Logger {
LoggerOutputFunction out;
Logger() {
out = print;
}
void log(String msg) {
out(msg);
}
}
void timestampLoggerOutputFunction(String msg) {
String timeStamp = new Date.now().toString();
print('${timeStamp}: $msg');
}
void main() {
Logger l = new Logger();
l.log('Hello World');
l.out = timestampLoggerOutputFunction;
l.log('Hello World');
}
Running the above sample yields the following output:
Hello World
2012-09-22 10:19:15.139: Hello World
The typedef line says that LoggerOutputFunction takes a String parameter and returns void.
timestampLoggerOutputFunction matches that definition and thus can be assigned to the out field.
Let me know if you need another example.
Dart 1.24 introduces a new typedef syntax to also support generic functions. The previous syntax is still supported.
typedef F = List<T> Function<T>(T);
For more details see https://github.com/dart-lang/sdk/blob/master/docs/language/informal/generic-function-type-alias.md
Function types can also be specified inline
void foo<T, S>(T Function(int, S) aFunction) {...}
See also https://www.dartlang.org/guides/language/language-tour#typedefs
typedef LoggerOutputFunction = void Function(String msg);
this looks much more clear than previous version
Just slightly modified answer, according to the latest typedef syntax, The example could be updated to:
typedef LoggerOutputFunction = void Function(String msg);
class Logger {
LoggerOutputFunction out;
Logger() {
out = print;
}
void log(String msg) {
out(msg);
}
}
void timestampLoggerOutputFunction(String msg) {
String timeStamp = new Date.now().toString();
print('${timeStamp}: $msg');
}
void main() {
Logger l = new Logger();
l.log('Hello World');
l.out = timestampLoggerOutputFunction;
l.log('Hello World');
}
Typedef in Dart is used to create a user-defined function (alias) for other application functions,
Syntax: typedef function_name (parameters);
With the help of a typedef, we can also assign a variable to a function.
Syntax:typedef variable_name = function_name;
After assigning the variable, if we have to invoke it then we go as:
Syntax: variable_name(parameters);
Example:
// Defining alias name
typedef MainFunction(int a, int b);
functionOne(int a, int b) {
print("This is FunctionOne");
print("$a and $b are lucky numbers !!");
}
functionTwo(int a, int b) {
print("This is FunctionTwo");
print("$a + $b is equal to ${a + b}.");
}
// Main Function
void main() {
// use alias
MainFunction number = functionOne;
number(1, 2);
number = functionTwo;
// Calling number
number(3, 4);
}
Output:
This is FunctionOne
1 and 2 are lucky numbers !!
This is FunctionTwo
3 + 4 is equal to 7
Since dart version 2.13 you can use typedef not only with functions but with every object you want.
Eg this code is now perfectly valid:
typedef IntList = List<int>;
IntList il = [1, 2, 3];
For more details see updated info:
https://dart.dev/guides/language/language-tour#typedefs
https://www.tutorialspoint.com/dart_programming/dart_programming_typedef.htm
typedef ManyOperation(int firstNo , int secondNo); //function signature
Add(int firstNo,int second){
print("Add result is ${firstNo+second}");
}
Subtract(int firstNo,int second){
print("Subtract result is ${firstNo-second}");
}
Divide(int firstNo,int second){
print("Divide result is ${firstNo/second}");
}
Calculator(int a,int b ,ManyOperation oper){
print("Inside calculator");
oper(a,b);
}
main(){
Calculator(5,5,Add);
Calculator(5,5,Subtract);
Calculator(5,5,Divide);
}
I am trying to order an IQueryable of entities by date from a passed in Expression< Func< T, object>> and am getting the error: "Unable to cast the type 'System.Nullable`1' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types." The entity has a nullable datetime property on it on which I am trying to sort:
Example: (where e.Date is a nullable DateTime)
Expression<Func<T,object>> sorter = (e) => e.Date;
IOrderedQueryable<T> sortedData = data.OrderBy(sorter);
Thanks in advance!
I wrote a simple class for ordering entities based on a lambda expression at runtime.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace DataModeling
{
public class QueryOrderer<TEntity>
where TEntity : class
{
private LambdaExpression defaultSortExpression;
private Dictionary<string, LambdaExpression> orderFieldLookup;
public QueryOrderer()
{
orderFieldLookup = new Dictionary<string, LambdaExpression>();
}
public void AddOrderMapping<TProp>(string fieldName, Expression<Func<TEntity, TProp>> selector)
{
orderFieldLookup[fieldName] = selector;
}
public void SetDefaultSortExpression<TProp>(Expression<Func<TEntity, TProp>> selector)
{
defaultSortExpression = selector;
}
public IQueryable<TEntity> GetOrderedEntries(string field, bool isDescending, IQueryable<TEntity> entries)
{
return orderEntries(entries, field, isDescending);
}
private IQueryable<TEntity> orderEntries(IQueryable<TEntity> entries, string fieldName, bool isDescending)
{
dynamic lambda = getOrderByLambda(entries, fieldName);
if (lambda == null)
{
return entries;
}
if (isDescending)
{
return Queryable.OrderByDescending(entries, lambda);
}
else
{
return Queryable.OrderBy(entries, lambda);
}
}
private dynamic getOrderByLambda(IQueryable<TEntity> entries, string fieldName)
{
if (!String.IsNullOrWhiteSpace(fieldName) && orderFieldLookup.ContainsKey(fieldName))
{
return orderFieldLookup[fieldName];
}
else
{
return defaultSortExpression;
}
}
}
}
You use this class by initially setting up all of the fields:
QueryOrderer<User> orderer = new QueryOrderer<User>();
orderer.SetDefaultSortExpression(u => u.FullName);
orderer.AddOrderMapping("UserId", u => u.UserId);
orderer.AddOrderMapping("Name", u => u.FullName);
orderer.AddOrderMapping("Email", u => u.Email);
orderer.AddOrderMapping("CreatedOn", u => u.CreatedOn);
...
var users = orderer.GetOrderedEntries("CreatedOn", isDescending: false, context.Users);
I nice feature of this code is that it handles look-up values perfectly. For instance, if you're trying to sort using the description rather than a key, you can use the outer context when building up the sort expression:
orderer.AddOrderMapping("UserType",
u => context.UserTypes
.Where(t => t.UserTypeId == u.UserTypeId)
.Select(t => t.Description)
.FirstOrDefault());
Entity Framework is smart enough to just fold the sub-query right into the outer query.
Two problem here: First you use object in your sorter, you should use DateTime. Secondly every element must have a place in the order so you have to define what should happen with elements where Date is null:
Expression<Func<T, DateTime>> sorter = (e) => e.Date ?? DateTime.MaxValue;
IOrderedQueryable<T> sortedData = data.OrderBy(sorter);
Try to reconstruct expression body
private LambdaExpression CreateLambdaPropertyGetter(Expression<Func<TEntity, object>> expression)
{
Expression body;
if (expression.Body is UnaryExpression && ((UnaryExpression)expression.Body).NodeType == ExpressionType.Convert)
body = ((UnaryExpression)expression.Body).Operand;
else
body = expression.Body;
var lambda = Expression.Lambda(body, expression.Parameters);
return lambda;
}
Try using Func delegate instead on Expression<Func>
Func<T,object> sorter = (e) => e.Date;
IOrderedEnumerable<T> sortedData = data.OrderBy(sorter);
class p {
public string Name { get; set; }
public int Age { get; set; }
};
static List<p> ll = new List<p>
{
new p{Name="Jabc",Age=53},new p{Name="Mdef",Age=20},
new p{Name="Exab",Age=45},new p{Name="G123",Age=19}
};
protected static void SortList()
{
IComparer<p> mycomp = (x, y) => x.Name.CompareTo(y.Name); <==(Line 1)
ll.Sort((x, y) => x.Name.CompareTo(y.Name));<==(Line 2)
}
Here the List.sort expects an IComparer<p> as parameter. And it works with the lambda
as shown in Line 2. But when I try to do as in Line 1, I get this error:
Cannot convert lambda expression to
type
System.Collections.Generic.IComparer'
because it is not a delegate type
I investigated this for quite some time but I still don't understand it.Maybe my understanding of IComparer is not quite good.Can somebody give me a hand ?
When you do ll.Sort((x, y) => x.Name.CompareTo(y.Name)); it uses the overload for Comparison<T>, not IComparer. Comparison<T> is a delegate, so you can use a lambda expression for it.
Comparison<p> mycomp = (x, y) => x.Name.CompareTo(y.Name); will work.
There's an existing solution you might refer to: https://stackoverflow.com/a/16839559/371531
This one uses Comparer<T>.Create introduced in .NET Framework 4.5.
IComparer is an interface, not a delegate.
You'll want to use the lambda expression on its .CompareTo(), not on the interface itself.
Use the following simple class:
public static class ComparerUtilities
{
class _Comparer<T> : Comparer<T>
{
Comparison<T> _comparison;
public _Comparer(Comparison<T> comparison)
{
_comparison = comparison;
}
public override int Compare(T x, T y)
{
return _comparison(x, y);
}
}
public static IComparer<T> FromComparison<T>(Comparison<T> comparison)
{
return new _Comparer<T>(comparison);
}
}
I have the following extension method
public static class ListExtensions
{
public static IEnumerable<T> Search<T>(this ICollection<T> collection, string stringToSearch)
{
foreach (T t in collection)
{
Type k = t.GetType();
PropertyInfo pi = k.GetProperty("Name");
if (pi.GetValue(t, null).Equals(stringToSearch))
{
yield return t;
}
}
}
}
What it does is by using reflection, it finds the name property and then filteres the record from the collection based on the matching string.
This method is being called as
List<FactorClass> listFC = new List<FactorClass>();
listFC.Add(new FactorClass { Name = "BKP", FactorValue="Book to price",IsGlobal =false });
listFC.Add(new FactorClass { Name = "YLD", FactorValue = "Dividend yield", IsGlobal = false });
listFC.Add(new FactorClass { Name = "EPM", FactorValue = "emp", IsGlobal = false });
listFC.Add(new FactorClass { Name = "SE", FactorValue = "something else", IsGlobal = false });
List<FactorClass> listFC1 = listFC.Search("BKP").ToList();
It is working fine.
But a closer look into the extension method will reveal that
Type k = t.GetType();
PropertyInfo pi = k.GetProperty("Name");
is actually inside a foreach loop which is actually not needed. I think we can take it outside the loop.
But how?
PLease help. (C#3.0)
Using reflection in this way is ugly to me.
Are you sure you need a 100% generic "T" and can't use a base class or interface?
If I were you, I would consider using the .Where<T>(Func<T, Boolean>) LINQ method instead of writing your own Search function.
An example use is:
List<FactorClass> listFC1 = listFC.Where(fc => fc.Name == "BKP").ToList();
public static IEnumerable<T> Search<T>(this ICollection<T> collection, string stringToSearch)
{
Type k = typeof(T);
PropertyInfo pi = k.GetProperty("Name");
foreach (T t in collection)
{
if (pi.GetValue(t, null).Equals(stringToSearch))
{
yield return t;
}
}
}
There's a couple of things you could do -- first you could institute a constraint on the generic type to an interface that has a name property. If it can only take a FactorClass, then you don't really need a generic type -- you could make it an extension to an ICollection<FactorClass>. If you go the interface route (or with the non-generic version), you can simply reference the property and won't have a need for reflection. If, for some reason, this doesn't work you can do:
var k = typeof(T);
var pi = k.GetProperty("Name");
foreach (T t in collection)
{
if (pi.GetValue(t, null).Equals(stringToSearch))
{
yield return t;
}
}
using an interface it might look like
public static IEnumerable<T> Search<T>(this ICollection<T> collection, string stringToSearch) where T : INameable
{
foreach (T t in collection)
{
if (string.Equals( t.Name, stringToSearch))
{
yield return t;
}
}
}
EDIT: After seeing #Jeff's comment, this is really only useful if you're doing something more complex than simply checking the value against one of the properties. He's absolutely correct in that using Where is a better solution for that problem.
Just get the type of T
Type k = typeof(T);
PropertyInfo pi = k.GetProperty("Name");
foreach (T t in collection)
{
if (pi.GetValue(t, null).Equals(stringToSearch))
{
yield return t;
}
}