VB.NET/VBHTML Multiple Expressions - asp.net-mvc

Helper:
<Extension()>
Public Function InputRow(Of TModel, TProperty)(ByVal helper As HtmlHelper(Of TModel),
ByVal exp1 As Expression(Of Func(Of TModel, TProperty)),
ByVal exp2 As Expression(Of Func(Of TModel, TProperty)),
ByVal exp3 As Expression(Of Func(Of TModel, TProperty)),
Optional cl As CL = Nothing,
Optional split As List(Of Integer) = Nothing) As MvcHtmlString
Dim expressions As New List(Of Expression(Of Func(Of TModel, TProperty))) From {expression1, expression2, expression3}
Return InputRow(helper, expressions, cl, split)
End Function
VBHTML:
#Html.InputRow(Function(x) Model.Test, Function(y) Model.Test1, Function(z) Model.Test2)
I don't understand TModel and TProperty very well and I can't find much on it. The issue I'm having is that variable Model.Test is an nullable Interger while Model.Test1 and Model.Test2 are nullable decimals.
When I try to get the metadata of Model.Test it throws an exception.
Get Metadata:
metaData = ModelMetadata.FromLambdaExpression(exp, helper.ViewData)
Exception:
System.InvalidOperationException: 'Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.'
Behind the scenes it's trying to convert the Integer? to a Decimal?, but I don't know why. The Model.Test expression is:
{x => Convert(value(ASP._Page_Views_Test_vbhtml).Model.Test)}
The value wrapped in the Convert is throwing the exception, but I don't know why it's getting wrapped in a convert. If I remove exp2 and exp3 from the method parameters. The convert doesn't happen.
Side Note:
Is it wrong to pass multiple expressions to one method? If not, is there a way to pass a list of TModel, TProperty expressions to the helper. Instead of having separate variables?
Thanks in advance for any assistance.

I think the issue here is that you want to pass multiple expressions of arbitrary type with only a single generic type parameter. Because there's only one TProperty, your three expressions have to involve properties of the same type, e.g. all String or all Integer. If you want to be able to use three different types then you need three different generic type parameters:
Public Function InputRow(Of TModel, TProperty1, TProperty2, TProperty3)(
helper As HtmlHelper(Of TModel),
exp1 As Expression(Of Func(Of TModel, TProperty1)),
exp2 As Expression(Of Func(Of TModel, TProperty2)),
exp3 As Expression(Of Func(Of TModel, TProperty3)),
Optional cl As CL = Nothing,
Optional split As List(Of Integer) = Nothing) As MvcHtmlString

Related

F# passing in a C# method into another C# method expecting Func<> parameter

In a c# dll I have a method, that takes func parameters:
public static AttrDiffRule Create<T>(string a_attr, string b_attr, Func<IAttrProxy,IAttrProxy,T,bool> parametricRule, T ruleParam, string desc = null)
and some predefined default methods intended for it:
public static bool NumberEqualsWithTolerance(IAttrProxy a, IAttrProxy b, double tolerance)
Now when using this in C#, I can write the following and it works:
var tmp = DefaultRules.Create("fds", "fds", DefaultRules.NumberEqualsWithTolerance, 10.0);
But, in F# this:
let attrRule = DefaultRules.Create("fd","fdsa", DefaultRules.NumberEqualsWithTolerance, 89.)
gives syntax error: "Error FS0002 This function takes too many arguments, or is used in a context where a function is not expected"
What would be the correct way to pass a C# static method into a parameter expecting a Func<> in F#?
It is important to actually pass in the function, and not a lambda wrapper, because the Create method's job is to use the argument function's MethodInfo, which gets hidden by the lambda wrapper's one.
The passed in function does not have overloads, also tried with specifying the type in place like
(DefaultRules.NumberEqualsWithTolerance : Func<IAttrProxy,IAttrProxy,float,bool>)
This is a case of F# being very thoughtful on your behalf - by helping you write more idiomatic F#.
In .NET, you are not actually passing in the function, as if it's a member reference, rather you are passing in a delegate object of type Func<>. The construction of the delegate object is done implicitly by C# when it has the necessary type information.
We can see this more clearly if we refactor this into an actual delegate type:
public delegate bool ParametricRule<T>(IAttrProxy a, IAttrProxy b, T value);
public static AttrDiffRule Create<T>(string a_attr, string b_attr, ParametricRule<T> parametricRule, T ruleParam, string desc = null)
{
return default;
}
If you try to construct a ParametricRule in F#, you'll see that its type is:
ParametricRule(IAttrProxy -> IAttrProxy -> 'a -> bool)
The rationale is that this way you can use regular F# functions, instead of some un-F#ish tupled input function. And this why it doesn't work in your case.
Because you're trying to throw the tupled version from C# right back at it.
So if you refactor your C# implementation to:
protected static bool NumberEqualsWithToleranceImpl(IAttrProxy a, IAttrProxy b, float tolerance)
{
return default;
}
public static ParametricRule<float> NumberEqualsWithTolerance => NumberEqualsWithToleranceImpl;
you'll see that it works like you'd expect it to, both from F# and C#.
let attrRule = DefaultRules.Create("fd","fdsa", DefaultRules.NumberEqualsWithTolerance, 89.0f) //compiles, yay!
Sometimes the type resolution has trouble when passing a method as a function parameter, because there can be overloads on the method that make the signature ambiguous. You can just wrap the function in a lambda that passes the parameters.
let attrRule =
DefaultRules.Create(
"fd",
"fdsa",
(fun a b tolerance -> DefaultRules.NumberEqualsWithTolerance(a, b, tolerance)),
89.0)

Idiomatic `is not` in Dart

Is there a more idiomaic way to express "is not" than !(o is T)?
/// Object (o) is not type (T)
///
/// Syntax that works but is not concise: `!(o is T)`
final word = 'drow';
if (!(word is String)) print('undoable');
You can use o is! T:
if (word is! String) print('undoable');
See Type test operators.

F# type providers - how to assign a value to an Out parameter?

The constructor for ProvidedParameter accepts a value isOut:bool, which I take to mean that provided functions can support C# out parameters.
However, I can't find any way to construct an Expr to set the value of the out parameter in the InvokeCode. Here's an example:
testType.AddMember(
let outParam = ProvidedParameter("outDate", typeof<DateTime>, true)
let outFunc = ProvidedMethod("GetTomorrow", [outParam], typeof<unit>)
do outFunc.InvokeCode <-
fun p ->
// "Invalid expression on left of assignment"
<## do %%(p.[1]) <- DateTime.Today.AddDays 1.0 ##>
outFunc)
I also wasn't able to do it using the static functions in the Microsoft.FSharp.Quotations.Expr type. I can't figure out how to get from p.[1], which is an Expr, to a Var, which appears to be required for every sort of let/set expression.

How does F# compile functions that can take multiple different parameter types into IL?

I know virtually nothing about F#. I don’t even know the syntax, so I can’t give examples.
It was mentioned in a comment thread that F# can declare functions that can take parameters of multiple possible types, for example a string or an integer. This would be similar to method overloads in C#:
public void Method(string str) { /* ... */ }
public void Method(int integer) { /* ... */ }
However, in CIL you cannot declare a delegate of this form. Each delegate must have a single, specific list of parameter types. Since functions in F# are first-class citizens, however, it would seem that you should be able to pass such a function around, and the only way to compile that into CIL is to use delegates.
So how does F# compile this into CIL?
This question is a little ambiguous, so I'll just ramble about what's true of F#.
In F#, methods can be overloaded, just like C#. Methods are always accessed by a qualified name of the form someObj.MethodName or someType.MethodName. There must be context which can statically resolve the overload at compile-time, just as in C#. Examples:
type T() =
member this.M(x:int) = ()
member this.M(x:string) = ()
let t = new T()
// these are all ok, just like C#
t.M(3)
t.M("foo")
let f : int -> unit = t.M
let g : string-> unit = t.M
// this fails, just like C#
let h = t.M // A unique overload for method 'M' could not be determined
// based on type information prior to this program point.
In F#, let-bound function values cannot be overloaded. So:
let foo(x:int) = ()
let foo(x:string) = () // Duplicate definition of value 'foo'
This means you can never have an "unqualified" identifier foo that has overloaded meaning. Each such name has a single unambiguous type.
Finally, the crazy case which is probably the one that prompts the question. F# can define inline functions which have "static member constraints" which can be bound to e.g. "all types T that have a member property named Bar" or whatnot. This kind of genericity cannot be encoded into CIL. Which is why the functions that leverage this feature must be inline, so that at each call site, the code specific-to-the-type-used-at-that-callsite is generated inline.
let inline crazy(x) = x.Qux(3) // elided: type syntax to constrain x to
// require a Qux member that can take an int
// suppose unrelated types U and V have such a Qux method
let u = new U()
crazy(u) // is expanded here into "u.Qux(3)" and then compiled
let v = new V()
crazy(v) // is expanded here into "v.Qux(3)" and then compiled
So this stuff is all handled by the compiler, and by the time we need to generate code, once again, we've statically resolved which specific type we're using at this callsite. The "type" of crazy is not a type that can be expressed in CIL, the F# type system just checks each callsite to ensure the necessary conditions are met and inlines the code into that callsite, a lot like how C++ templates work.
(The main purpose/justification for the crazy stuff is for overloaded math operators. Without the inline feature, the + operator, for instance, being a let-bound function type, could either "only work on ints" or "only work on floats" or whatnot. Some ML flavors (F# is a relative of OCaml) do exactly that, where e.g. the + operator only works on ints, and a separate operator, usually named +., works on floats. Whereas in F#, + is an inline function defined in the F# library that works on any type with a + operator member or any of the primitive numeric types. Inlining can also have some potential run-time performance benefits, which is also appealing for some math-y/computational domains.)
When you're writing C# and you need a function that can take multiple different parameter sets, you just create method overloads:
string f(int x)
{
return "int " + x;
}
string f(string x)
{
return "string " + x;
}
void callF()
{
Console.WriteLine(f(12));
Console.WriteLine(f("12"));
}
// there's no way to write a function like this:
void call(Func<int|string, string> func)
{
Console.WriteLine(func(12));
Console.WriteLine(func("12"));
}
The callF function is trivial, but my made-up syntax for the call function doesn't work.
When you're writing F# and you need a function that can take multiple different parameter sets, you create a discriminated union that can contain all the different parameter sets and you make a single function that takes that union:
type Either = Int of int
| String of string
let f = function Int x -> "int " + string x
| String x -> "string " + x
let callF =
printfn "%s" (f (Int 12))
printfn "%s" (f (String "12"))
let call func =
printfn "%s" (func (Int 12))
printfn "%s" (func (String "12"))
Being a single function, f can be used like any other value, so in F# we can write callF and call f, and both do the same thing.
So how does F# implement the Either type I created above? Essentially like this:
public abstract class Either
{
public class Int : Test.Either
{
internal readonly int item;
internal Int(int item);
public int Item { get; }
}
public class String : Test.Either
{
internal readonly string item;
internal String(string item);
public string Item { get; }
}
}
The signature of the call function is:
public static void call(FSharpFunc<Either, string> f);
And f looks something like this:
public static string f(Either _arg1)
{
if (_arg1 is Either.Int)
return "int " + ((Either.Int)_arg1).Item;
return "string " + ((Either.String)_arg1).Item;
}
Of course you could implement the same Either type in C# (duh!), but it's not idiomatic, which is why it wasn't the obvious answer to the previous question.
Assuming I understand the question, in F# you can define expressions which depend on the availability of members with particular signatures. For instance
let inline f x a = (^t : (member Method : ^a -> unit)(x,a))
This defines a function f which takes a value x of type ^t and a value a of type ^a where ^t has a method Method taking an ^a to unit (void in C#), and which calls that method. Because this function is defined as inline, the definition is inlined at the point of use, which is the only reason that it can be given such a type. Thus, although you can pass f as a first class function, you can only do so when the types ^t and ^a are statically known so that the method call can be statically resolved and inserted in place (and this is why the type parameters have the funny ^ sigil instead of the normal ' sigil).
Here's an example of passing f as a first-class function:
type T() =
member x.Method(i) = printfn "Method called with int: %i" i
List.iter (f (new T())) [1; 2; 3]
This runs the method Method against the three values in the list. Because f is inlined, this is basically equivalent to
List.iter ((fun (x:T) a -> x.Method(a)) (new T())) [1; 2; 3]
EDIT
Given the context that seems to have led to this question (C# - How can I “overload” a delegate?), I appear not to have addressed your real question at all. Instead, what Gabe appears to be talking about is the ease with which one can define and use discriminated unions. So the question posed on that other thread might be answered like this using F#:
type FunctionType =
| NoArgument of (unit -> unit)
| ArrayArgument of (obj[] -> unit)
let doNothing (arr:obj[]) = ()
let doSomething () = printfn "'doSomething' was called"
let mutable someFunction = ArrayArgument doNothing
someFunction <- NoArgument doSomething
//now call someFunction, regardless of what type of argument it's supposed to take
match someFunction with
| NoArgument f -> f()
| ArrayArgument f -> f [| |] // pass in empty array
At a low level, there's no CIL magic going on here; it's just that NoArgument and ArrayArgument are subclasses of FunctionType which are easy to construct and to deconstruct via pattern matching. The branches of the pattern matching expression are morally equivalent to a type test followed by property accesses, but the compiler makes sure that the cases have 100% coverage and don't overlap. You could encode the exact same operations in C# without any problem, but it would be much more verbose and the compiler wouldn't help you out with exhaustiveness checking, etc.
Also, there is nothing here which is particular to functions; F# discriminated unions make it easy to define types which have a fixed number of named alternatives, each one of which can have data of whatever type you'd like.
I'm not quite sure that understand your question correctly... F# compiler uses FSharpFunc type to represent functions. Usually in F# code you don't deal with this type directly, using fancy syntactic representation instead, but if you expose any members that returns or accepts function and use them from another language, line C# - you will see it.
So instead of using delegates - F# utilizes its special type with concrete or generic parameters.
If your question was about things like add something-i-don't-know-what-exactly-but-it-has-addition-operator then you need to use inline keyword and compiler will emit function body in the call site. #kvb's answer was describing exactly this case.

Multiple parameters to methods in F#

I've got a class written in F# that I'm consuming in C#, that defines a method Render:
member this.Render template (context: IContext) =
let tokens = Lexer.tokenize template
let parser = new DefaultParser([for filter in _filters -> filter])
let resp = new StringBuilder()
for node in parser.Parse tokens None do
ignore <| resp.Append(node.render context)
resp.ToString()
The signature of this method is template:string -> (IContext -> string), which of course reads as "member Render takes a string parameter, then returns a function that takes an IContext and produces a string.
If I change the declaration from "member" to a let binding, defining it as a function local to the class definition:
let Render template (context: IContext) = ...
Then the signature becomes what you would expect it to be - string -> IContext -> string, which reads "Render takes a string, then an IContext and produces a string".
Is there a way to make a member behave like the let binding? This is causing issues consuming this member from C#, as the signature becomes Render(string, FastFunc<IContext, string>), which is not overly usable.
If you want to expose to C#, you should write it tupled style:
> type Foo =
- member this.Bar (param1, param2) = param1 + param2;;
type Foo =
class
member Bar : param1:int * param2:int -> int
end
That'll expose a normal .NET style method.

Resources