logo
Tags down

shadow

Is it possible to pass NULL as argument in ${...} expression?


By : Tony
Date : October 18 2020, 03:08 PM
Any of those help You are calling a varargs method. The null is wrapped into an array with one null element (which is not considered empty). The solution is to omit the null parameter:
code :
urlBuilder.replaceQueryParam('page')


Share : facebook icon twitter icon

How to pass an expression as an argument/parameter?


By : Michael Cullum
Date : March 29 2020, 07:55 AM
it fixes the issue You want a function pointer.
Make the expression a function.
code :
float fexpression( float x )
{
   return (5*(pow(x,4))) + (3*(pow(x, 3))) + (10 * x) - 5
}
   if (/* gexpression == true */)
        cout << gfunction(fexpression);
   else /* h expression == true */
        cout << hfunction(fexpression);

Why I can pass null as argument to method without null being instance of accepted parameters?


By : Uzair_a
Date : March 29 2020, 07:55 AM
hope this fix your issue The Java Language Specification states

Pass expression parameter as argument to another expression


By : user3431879
Date : March 29 2020, 07:55 AM
hop of those help? If I understand correctly, you want to reuse an expression tree inside another one, and still allow the compiler to do all the magic of building the expression tree for you.
This is actually possible, and I have done it in many occasions.
code :
 public static Expression<Func<Quote,QuoteProductImage, bool>> FilterQuoteProductImagesByQuote()
 {
     return (q,qpi) => q.User.Id == qpi.ItemOrder;
 }
  public static TFunc AsQuote<TFunc>(this Expression<TFunc> exp)
  {
      throw new InvalidOperationException("This method is not intended to be invoked, just as a marker in Expression trees!");
  }
  public static Expression<TFunc> ResolveQuotes<TFunc>(this Expression<TFunc> exp)
  {
      var visitor = new ResolveQuoteVisitor();
      return (Expression<TFunc>)visitor.Visit(exp);
  }
    private class ResolveQuoteVisitor : ExpressionVisitor
    {
        public ResolveQuoteVisitor()
        {
            m_asQuoteMethod = typeof(Extensions).GetMethod("AsQuote").GetGenericMethodDefinition();
        }
        MethodInfo m_asQuoteMethod;
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            if (IsAsquoteMethodCall(node))
            {
                // we cant handle here parameters, so just ignore them for now
                return Visit(ExtractQuotedExpression(node).Body);
            }
            return base.VisitMethodCall(node);
        }

        private bool IsAsquoteMethodCall(MethodCallExpression node)
        {
            return node.Method.IsGenericMethod && node.Method.GetGenericMethodDefinition() == m_asQuoteMethod;
        }

        private LambdaExpression ExtractQuotedExpression(MethodCallExpression node)
        {
            var quoteExpr = node.Arguments[0];
            // you know this is a method call to a static method without parameters
            // you can do the easiest: compile it, and then call:
            // alternatively you could call the method with reflection
            // or even cache the value to the method in a static dictionary, and take the expression from there (the fastest)
            // the choice is up to you. as an example, i show you here the most generic solution (the first)
            return (LambdaExpression)Expression.Lambda(quoteExpr).Compile().DynamicInvoke();
        }
    }
    private class MultiParamReplaceVisitor : ExpressionVisitor
    {
        private readonly Dictionary<ParameterExpression, Expression> m_replacements;
        private readonly LambdaExpression m_expressionToVisit;
        public MultiParamReplaceVisitor(Expression[] parameterValues, LambdaExpression expressionToVisit)
        {
            // do null check
            if (parameterValues.Length != expressionToVisit.Parameters.Count)
                throw new ArgumentException(string.Format("The paraneter values count ({0}) does not match the expression parameter count ({1})", parameterValues.Length, expressionToVisit.Parameters.Count));
            m_replacements = expressionToVisit.Parameters
                .Select((p, idx) => new { Idx = idx, Parameter = p })
                .ToDictionary(x => x.Parameter, x => parameterValues[x.Idx]);
            m_expressionToVisit = expressionToVisit;
        }

        protected override Expression VisitParameter(ParameterExpression node)
        {
            Expression replacement;
            if (m_replacements.TryGetValue(node, out replacement))
                return Visit(replacement);
            return base.VisitParameter(node);
        }

        public Expression Replace()
        {
            return Visit(m_expressionToVisit.Body);
        }
    }
        protected override Expression VisitInvocation(InvocationExpression node)
        {
            if (node.Expression.NodeType == ExpressionType.Call && IsAsquoteMethodCall((MethodCallExpression)node.Expression))
            {
                var targetLambda = ExtractQuotedExpression((MethodCallExpression)node.Expression);
                var replaceParamsVisitor = new MultiParamReplaceVisitor(node.Arguments.ToArray(), targetLambda);
                return Visit(replaceParamsVisitor.Replace());
            }
            return base.VisitInvocation(node);
        }
  public IEnumerable<FilteredViewModel> GetFilteredQuotes()
  {
      Expression<Func<Quote, FilteredViewModel>> selector = q => new FilteredViewModel
      {
          Quote = q,
          QuoteProductImages = q.QuoteProducts.SelectMany(qp => qp.QuoteProductImages.Where(qpi => ExpressionHelper.FilterQuoteProductImagesByQuote().AsQuote()(q, qpi)))
      };
      selector = selector.ResolveQuotes();
      return _context.Context.Quotes.Select(selector);
  }
    public static IQueryable<T> ResolveQuotes<T>(this IQueryable<T> query)
    {
        var visitor = new ResolveQuoteVisitor();
        return query.Provider.CreateQuery<T>(visitor.Visit(query.Expression));
    }

Why do I need to pass null as an argument in function.apply(null, arguments)?


By : user2382601
Date : March 29 2020, 07:55 AM
I hope this helps . The first argument of apply is the this object. You don't have to pass null, you could pass this or any other object as well, but you can't just pass arguments by itself because then arguments will become your this object in the function and name won't get a value.
Note that, for this example, you could just call fun(name) instead of fun.apply(null, arguments).

How to pass a function or expression as an argument?


By : user2485912
Date : March 29 2020, 07:55 AM
it should still fix some issue It sounds like you want to pass a "formula" that computes Z from X and Y. Rather than using exec or eval and running into issues with namespaces, a better way to do that is to pass in a function. As user s3cur3 commented, an easy way to do that is with a lambda expression:
code :
def graph(func):
    # set up X and Y up here

    Z = func(X, Y)

    # do stuff with Z after computing it?

graph(lambda X, Y: X+Y)
def my_func(x, y):  # this could be done in a lambda too, but lets pretend it couldn't
    if random.random() < 0.5: 
        return x + y
    return x - y

graph(my_func)
shadow
Privacy Policy - Terms - Contact Us © soohba.com