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









share|improve this question

























    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









    share|improve this question























      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









      share|improve this question













      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#






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 13:13









      Scott Nimrod

      5,42232961




      5,42232961






















          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#).






          share|improve this answer




















          • 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

















          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));





          share|improve this answer




















          • 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











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          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

























          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#).






          share|improve this answer




















          • 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














          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#).






          share|improve this answer




















          • 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












          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#).






          share|improve this answer












          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#).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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












          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));





          share|improve this answer




















          • 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















          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));





          share|improve this answer




















          • 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













          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));





          share|improve this answer












          Try to explicitly specify the arguments type



          public ICommand RequestAccount =
          new DelegateCommand(FuncConvert.ToFSharpFunc<object>(obj => Debug.WriteLine(obj)),
          FuncConvert.ToFSharpFunc<object, bool>(_ => true));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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

















          • 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


















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3