How do I implement an F# high order function in C#?
up vote
0
down vote
favorite
How do I implement an F# high order function in C#?
public ICommand RequestAccount =
new DelegateCommand(FuncConvert.ToFSharpFunc( _ => Debug.WriteLine() ),
FuncConvert.ToFSharpFunc( _ => return true )
);
Error CS0411 The type arguments for method
'FuncConvert.ToFSharpFunc(Action)' cannot be inferred from the
usage. Try specifying the type arguments explicitly.
Based on the error, I'm not aware of how to express the type parameters explicitly. Hence, I don't think C# understands what a unit is that's to be returned on the first lambda.
DelegateCommand
type DelegateCommand (action:(obj -> unit), canExecute:(obj -> bool)) =
let event = new DelegateEvent<EventHandler>()
interface ICommand with
[<CLIEvent>]
member this.CanExecuteChanged = event.Publish
member this.CanExecute arg = canExecute(arg)
member this.Execute arg = action(arg
f#
add a comment |
up vote
0
down vote
favorite
How do I implement an F# high order function in C#?
public ICommand RequestAccount =
new DelegateCommand(FuncConvert.ToFSharpFunc( _ => Debug.WriteLine() ),
FuncConvert.ToFSharpFunc( _ => return true )
);
Error CS0411 The type arguments for method
'FuncConvert.ToFSharpFunc(Action)' cannot be inferred from the
usage. Try specifying the type arguments explicitly.
Based on the error, I'm not aware of how to express the type parameters explicitly. Hence, I don't think C# understands what a unit is that's to be returned on the first lambda.
DelegateCommand
type DelegateCommand (action:(obj -> unit), canExecute:(obj -> bool)) =
let event = new DelegateEvent<EventHandler>()
interface ICommand with
[<CLIEvent>]
member this.CanExecuteChanged = event.Publish
member this.CanExecute arg = canExecute(arg)
member this.Execute arg = action(arg
f#
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
How do I implement an F# high order function in C#?
public ICommand RequestAccount =
new DelegateCommand(FuncConvert.ToFSharpFunc( _ => Debug.WriteLine() ),
FuncConvert.ToFSharpFunc( _ => return true )
);
Error CS0411 The type arguments for method
'FuncConvert.ToFSharpFunc(Action)' cannot be inferred from the
usage. Try specifying the type arguments explicitly.
Based on the error, I'm not aware of how to express the type parameters explicitly. Hence, I don't think C# understands what a unit is that's to be returned on the first lambda.
DelegateCommand
type DelegateCommand (action:(obj -> unit), canExecute:(obj -> bool)) =
let event = new DelegateEvent<EventHandler>()
interface ICommand with
[<CLIEvent>]
member this.CanExecuteChanged = event.Publish
member this.CanExecute arg = canExecute(arg)
member this.Execute arg = action(arg
f#
How do I implement an F# high order function in C#?
public ICommand RequestAccount =
new DelegateCommand(FuncConvert.ToFSharpFunc( _ => Debug.WriteLine() ),
FuncConvert.ToFSharpFunc( _ => return true )
);
Error CS0411 The type arguments for method
'FuncConvert.ToFSharpFunc(Action)' cannot be inferred from the
usage. Try specifying the type arguments explicitly.
Based on the error, I'm not aware of how to express the type parameters explicitly. Hence, I don't think C# understands what a unit is that's to be returned on the first lambda.
DelegateCommand
type DelegateCommand (action:(obj -> unit), canExecute:(obj -> bool)) =
let event = new DelegateEvent<EventHandler>()
interface ICommand with
[<CLIEvent>]
member this.CanExecuteChanged = event.Publish
member this.CanExecute arg = canExecute(arg)
member this.Execute arg = action(arg
f#
f#
asked Nov 11 at 13:13
Scott Nimrod
5,42232961
5,42232961
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
If you are in control of both the C# and the F# part of the code, then I would not try to create F# functions explicitly from C# - that will just make your C# code ugly. You can easily add a static method that will take Func
and Action
delegates and provide a C#-friendly interface:
type DelegateCommand (action:(obj -> unit), canExecute:(obj -> bool)) =
let event = new DelegateEvent<EventHandler>()
interface ICommand with
[<CLIEvent>]
member this.CanExecuteChanged = event.Publish
member this.CanExecute arg = canExecute(arg)
member this.Execute arg = action(arg)
static member Create(action:Action<obj>, canExecute:Func<obj, bool>) =
DelegateCommand(action.Invoke, canExecute.Invoke)
Now you can use DelegateCommand.Create
from C# in a nice way:
DelegateCommand.Create(
(o => Console.WriteLine(o)),
(o => true) )
For the record, I also do not quite see the value of defining DelegateCommand
in F# and using that from C# if you are not doing anything else on the F# side - it seems like a simple type that could as well be defined in C# (i.e. you are not gaining much by doing that in F#).
For interop between C# and F#, I agree with Tomas--keep the F#-specific types out of APIs that will be used in C#. Action, Func, and delegates are sufficient for passing code blocks around.
– Curt Nichols
Nov 12 at 23:39
add a comment |
up vote
1
down vote
Try to explicitly specify the arguments type
public ICommand RequestAccount =
new DelegateCommand(FuncConvert.ToFSharpFunc<object>(obj => Debug.WriteLine(obj)),
FuncConvert.ToFSharpFunc<object, bool>(_ => true));
The first lambda works. The second doesn't compile.
– Scott Nimrod
Nov 11 at 14:20
It works for me, what's the error message?
– gileCAD
Nov 11 at 14:25
"Using the generic method 'FuncConvert.ToFSharpFunc<T>(Action<T>)' requires 1 type arguments"
– Scott Nimrod
Nov 11 at 14:29
The second lambda have to be: FuncConvert.ToFSharpFunc<object, bool>(_ => true) to call the overload ToFSharpFunc<T,U>(Converter<T, U>), see: docs.microsoft.com/en-us/previous-versions/visualstudio/…
– gileCAD
Nov 11 at 14:41
The code snippet just doesn't compile on my machine. Not sure how it can compile on yours but not mine...
– Scott Nimrod
Nov 11 at 16:19
|
show 1 more comment
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
If you are in control of both the C# and the F# part of the code, then I would not try to create F# functions explicitly from C# - that will just make your C# code ugly. You can easily add a static method that will take Func
and Action
delegates and provide a C#-friendly interface:
type DelegateCommand (action:(obj -> unit), canExecute:(obj -> bool)) =
let event = new DelegateEvent<EventHandler>()
interface ICommand with
[<CLIEvent>]
member this.CanExecuteChanged = event.Publish
member this.CanExecute arg = canExecute(arg)
member this.Execute arg = action(arg)
static member Create(action:Action<obj>, canExecute:Func<obj, bool>) =
DelegateCommand(action.Invoke, canExecute.Invoke)
Now you can use DelegateCommand.Create
from C# in a nice way:
DelegateCommand.Create(
(o => Console.WriteLine(o)),
(o => true) )
For the record, I also do not quite see the value of defining DelegateCommand
in F# and using that from C# if you are not doing anything else on the F# side - it seems like a simple type that could as well be defined in C# (i.e. you are not gaining much by doing that in F#).
For interop between C# and F#, I agree with Tomas--keep the F#-specific types out of APIs that will be used in C#. Action, Func, and delegates are sufficient for passing code blocks around.
– Curt Nichols
Nov 12 at 23:39
add a comment |
up vote
2
down vote
accepted
If you are in control of both the C# and the F# part of the code, then I would not try to create F# functions explicitly from C# - that will just make your C# code ugly. You can easily add a static method that will take Func
and Action
delegates and provide a C#-friendly interface:
type DelegateCommand (action:(obj -> unit), canExecute:(obj -> bool)) =
let event = new DelegateEvent<EventHandler>()
interface ICommand with
[<CLIEvent>]
member this.CanExecuteChanged = event.Publish
member this.CanExecute arg = canExecute(arg)
member this.Execute arg = action(arg)
static member Create(action:Action<obj>, canExecute:Func<obj, bool>) =
DelegateCommand(action.Invoke, canExecute.Invoke)
Now you can use DelegateCommand.Create
from C# in a nice way:
DelegateCommand.Create(
(o => Console.WriteLine(o)),
(o => true) )
For the record, I also do not quite see the value of defining DelegateCommand
in F# and using that from C# if you are not doing anything else on the F# side - it seems like a simple type that could as well be defined in C# (i.e. you are not gaining much by doing that in F#).
For interop between C# and F#, I agree with Tomas--keep the F#-specific types out of APIs that will be used in C#. Action, Func, and delegates are sufficient for passing code blocks around.
– Curt Nichols
Nov 12 at 23:39
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
If you are in control of both the C# and the F# part of the code, then I would not try to create F# functions explicitly from C# - that will just make your C# code ugly. You can easily add a static method that will take Func
and Action
delegates and provide a C#-friendly interface:
type DelegateCommand (action:(obj -> unit), canExecute:(obj -> bool)) =
let event = new DelegateEvent<EventHandler>()
interface ICommand with
[<CLIEvent>]
member this.CanExecuteChanged = event.Publish
member this.CanExecute arg = canExecute(arg)
member this.Execute arg = action(arg)
static member Create(action:Action<obj>, canExecute:Func<obj, bool>) =
DelegateCommand(action.Invoke, canExecute.Invoke)
Now you can use DelegateCommand.Create
from C# in a nice way:
DelegateCommand.Create(
(o => Console.WriteLine(o)),
(o => true) )
For the record, I also do not quite see the value of defining DelegateCommand
in F# and using that from C# if you are not doing anything else on the F# side - it seems like a simple type that could as well be defined in C# (i.e. you are not gaining much by doing that in F#).
If you are in control of both the C# and the F# part of the code, then I would not try to create F# functions explicitly from C# - that will just make your C# code ugly. You can easily add a static method that will take Func
and Action
delegates and provide a C#-friendly interface:
type DelegateCommand (action:(obj -> unit), canExecute:(obj -> bool)) =
let event = new DelegateEvent<EventHandler>()
interface ICommand with
[<CLIEvent>]
member this.CanExecuteChanged = event.Publish
member this.CanExecute arg = canExecute(arg)
member this.Execute arg = action(arg)
static member Create(action:Action<obj>, canExecute:Func<obj, bool>) =
DelegateCommand(action.Invoke, canExecute.Invoke)
Now you can use DelegateCommand.Create
from C# in a nice way:
DelegateCommand.Create(
(o => Console.WriteLine(o)),
(o => true) )
For the record, I also do not quite see the value of defining DelegateCommand
in F# and using that from C# if you are not doing anything else on the F# side - it seems like a simple type that could as well be defined in C# (i.e. you are not gaining much by doing that in F#).
answered Nov 11 at 21:35
Tomas Petricek
197k13285458
197k13285458
For interop between C# and F#, I agree with Tomas--keep the F#-specific types out of APIs that will be used in C#. Action, Func, and delegates are sufficient for passing code blocks around.
– Curt Nichols
Nov 12 at 23:39
add a comment |
For interop between C# and F#, I agree with Tomas--keep the F#-specific types out of APIs that will be used in C#. Action, Func, and delegates are sufficient for passing code blocks around.
– Curt Nichols
Nov 12 at 23:39
For interop between C# and F#, I agree with Tomas--keep the F#-specific types out of APIs that will be used in C#. Action, Func, and delegates are sufficient for passing code blocks around.
– Curt Nichols
Nov 12 at 23:39
For interop between C# and F#, I agree with Tomas--keep the F#-specific types out of APIs that will be used in C#. Action, Func, and delegates are sufficient for passing code blocks around.
– Curt Nichols
Nov 12 at 23:39
add a comment |
up vote
1
down vote
Try to explicitly specify the arguments type
public ICommand RequestAccount =
new DelegateCommand(FuncConvert.ToFSharpFunc<object>(obj => Debug.WriteLine(obj)),
FuncConvert.ToFSharpFunc<object, bool>(_ => true));
The first lambda works. The second doesn't compile.
– Scott Nimrod
Nov 11 at 14:20
It works for me, what's the error message?
– gileCAD
Nov 11 at 14:25
"Using the generic method 'FuncConvert.ToFSharpFunc<T>(Action<T>)' requires 1 type arguments"
– Scott Nimrod
Nov 11 at 14:29
The second lambda have to be: FuncConvert.ToFSharpFunc<object, bool>(_ => true) to call the overload ToFSharpFunc<T,U>(Converter<T, U>), see: docs.microsoft.com/en-us/previous-versions/visualstudio/…
– gileCAD
Nov 11 at 14:41
The code snippet just doesn't compile on my machine. Not sure how it can compile on yours but not mine...
– Scott Nimrod
Nov 11 at 16:19
|
show 1 more comment
up vote
1
down vote
Try to explicitly specify the arguments type
public ICommand RequestAccount =
new DelegateCommand(FuncConvert.ToFSharpFunc<object>(obj => Debug.WriteLine(obj)),
FuncConvert.ToFSharpFunc<object, bool>(_ => true));
The first lambda works. The second doesn't compile.
– Scott Nimrod
Nov 11 at 14:20
It works for me, what's the error message?
– gileCAD
Nov 11 at 14:25
"Using the generic method 'FuncConvert.ToFSharpFunc<T>(Action<T>)' requires 1 type arguments"
– Scott Nimrod
Nov 11 at 14:29
The second lambda have to be: FuncConvert.ToFSharpFunc<object, bool>(_ => true) to call the overload ToFSharpFunc<T,U>(Converter<T, U>), see: docs.microsoft.com/en-us/previous-versions/visualstudio/…
– gileCAD
Nov 11 at 14:41
The code snippet just doesn't compile on my machine. Not sure how it can compile on yours but not mine...
– Scott Nimrod
Nov 11 at 16:19
|
show 1 more comment
up vote
1
down vote
up vote
1
down vote
Try to explicitly specify the arguments type
public ICommand RequestAccount =
new DelegateCommand(FuncConvert.ToFSharpFunc<object>(obj => Debug.WriteLine(obj)),
FuncConvert.ToFSharpFunc<object, bool>(_ => true));
Try to explicitly specify the arguments type
public ICommand RequestAccount =
new DelegateCommand(FuncConvert.ToFSharpFunc<object>(obj => Debug.WriteLine(obj)),
FuncConvert.ToFSharpFunc<object, bool>(_ => true));
answered Nov 11 at 14:03
gileCAD
1,28656
1,28656
The first lambda works. The second doesn't compile.
– Scott Nimrod
Nov 11 at 14:20
It works for me, what's the error message?
– gileCAD
Nov 11 at 14:25
"Using the generic method 'FuncConvert.ToFSharpFunc<T>(Action<T>)' requires 1 type arguments"
– Scott Nimrod
Nov 11 at 14:29
The second lambda have to be: FuncConvert.ToFSharpFunc<object, bool>(_ => true) to call the overload ToFSharpFunc<T,U>(Converter<T, U>), see: docs.microsoft.com/en-us/previous-versions/visualstudio/…
– gileCAD
Nov 11 at 14:41
The code snippet just doesn't compile on my machine. Not sure how it can compile on yours but not mine...
– Scott Nimrod
Nov 11 at 16:19
|
show 1 more comment
The first lambda works. The second doesn't compile.
– Scott Nimrod
Nov 11 at 14:20
It works for me, what's the error message?
– gileCAD
Nov 11 at 14:25
"Using the generic method 'FuncConvert.ToFSharpFunc<T>(Action<T>)' requires 1 type arguments"
– Scott Nimrod
Nov 11 at 14:29
The second lambda have to be: FuncConvert.ToFSharpFunc<object, bool>(_ => true) to call the overload ToFSharpFunc<T,U>(Converter<T, U>), see: docs.microsoft.com/en-us/previous-versions/visualstudio/…
– gileCAD
Nov 11 at 14:41
The code snippet just doesn't compile on my machine. Not sure how it can compile on yours but not mine...
– Scott Nimrod
Nov 11 at 16:19
The first lambda works. The second doesn't compile.
– Scott Nimrod
Nov 11 at 14:20
The first lambda works. The second doesn't compile.
– Scott Nimrod
Nov 11 at 14:20
It works for me, what's the error message?
– gileCAD
Nov 11 at 14:25
It works for me, what's the error message?
– gileCAD
Nov 11 at 14:25
"Using the generic method 'FuncConvert.ToFSharpFunc<T>(Action<T>)' requires 1 type arguments"
– Scott Nimrod
Nov 11 at 14:29
"Using the generic method 'FuncConvert.ToFSharpFunc<T>(Action<T>)' requires 1 type arguments"
– Scott Nimrod
Nov 11 at 14:29
The second lambda have to be: FuncConvert.ToFSharpFunc<object, bool>(_ => true) to call the overload ToFSharpFunc<T,U>(Converter<T, U>), see: docs.microsoft.com/en-us/previous-versions/visualstudio/…
– gileCAD
Nov 11 at 14:41
The second lambda have to be: FuncConvert.ToFSharpFunc<object, bool>(_ => true) to call the overload ToFSharpFunc<T,U>(Converter<T, U>), see: docs.microsoft.com/en-us/previous-versions/visualstudio/…
– gileCAD
Nov 11 at 14:41
The code snippet just doesn't compile on my machine. Not sure how it can compile on yours but not mine...
– Scott Nimrod
Nov 11 at 16:19
The code snippet just doesn't compile on my machine. Not sure how it can compile on yours but not mine...
– Scott Nimrod
Nov 11 at 16:19
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249083%2fhow-do-i-implement-an-f-high-order-function-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown