How to do explicit overloaded conversion in F# like in C#? [duplicate]












1
















This question already has an answer here:




  • How to use overloaded explicit conversion operators?

    1 answer




Let's say that I have a class in C# with overloaded implicit and explicit operators:



public static implicit operator CSClass(int a) => ...;
public static explicit operator int(CSClass a) => ...;


I compile this project as class library.



In F# now I can add my operator for implicit conversions and use it:



#r @"C:pathto.dll"
open Some.Namespace.ToMyClass
let inline (!>) (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> ^b) x)
let a : CSClass = !> 5


But how can I do an explicit overloaded conversion in F#? (CSClass to int)










share|improve this question















marked as duplicate by Community Nov 17 '18 at 21:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    1
















    This question already has an answer here:




    • How to use overloaded explicit conversion operators?

      1 answer




    Let's say that I have a class in C# with overloaded implicit and explicit operators:



    public static implicit operator CSClass(int a) => ...;
    public static explicit operator int(CSClass a) => ...;


    I compile this project as class library.



    In F# now I can add my operator for implicit conversions and use it:



    #r @"C:pathto.dll"
    open Some.Namespace.ToMyClass
    let inline (!>) (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> ^b) x)
    let a : CSClass = !> 5


    But how can I do an explicit overloaded conversion in F#? (CSClass to int)










    share|improve this question















    marked as duplicate by Community Nov 17 '18 at 21:41


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      1












      1








      1









      This question already has an answer here:




      • How to use overloaded explicit conversion operators?

        1 answer




      Let's say that I have a class in C# with overloaded implicit and explicit operators:



      public static implicit operator CSClass(int a) => ...;
      public static explicit operator int(CSClass a) => ...;


      I compile this project as class library.



      In F# now I can add my operator for implicit conversions and use it:



      #r @"C:pathto.dll"
      open Some.Namespace.ToMyClass
      let inline (!>) (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> ^b) x)
      let a : CSClass = !> 5


      But how can I do an explicit overloaded conversion in F#? (CSClass to int)










      share|improve this question

















      This question already has an answer here:




      • How to use overloaded explicit conversion operators?

        1 answer




      Let's say that I have a class in C# with overloaded implicit and explicit operators:



      public static implicit operator CSClass(int a) => ...;
      public static explicit operator int(CSClass a) => ...;


      I compile this project as class library.



      In F# now I can add my operator for implicit conversions and use it:



      #r @"C:pathto.dll"
      open Some.Namespace.ToMyClass
      let inline (!>) (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> ^b) x)
      let a : CSClass = !> 5


      But how can I do an explicit overloaded conversion in F#? (CSClass to int)





      This question already has an answer here:




      • How to use overloaded explicit conversion operators?

        1 answer








      f# type-conversion c#-to-f#






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 1:42







      Endorphinex

















      asked Nov 16 '18 at 1:34









      EndorphinexEndorphinex

      1611




      1611




      marked as duplicate by Community Nov 17 '18 at 21:41


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Community Nov 17 '18 at 21:41


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          2














          It is my understanding that F# does not usually do explicit conversions. Instead, you would just use a function. For example, if you have a char and want to convert that explicitly into an int, in C# you write:



          char theChar = 'A';
          int convertedChar = (int)theChar;


          In F#, the int operator (function) is used for the same purpose:



          let theChar = 'A'
          let convertedChar = int theChar;


          Therefore the idiomatic way to do the conversion would be something like this:



          module Some.Namespace.MyClass
          let toInt (x : MyClass) = [...]


          You would use it like so:



          let convertedMyClass = MyClass.toInt myClass


          It can also be piped:



          funcReturningMyClass x y
          |> MyClass.toInt
          |> printfn "%d"





          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            It is my understanding that F# does not usually do explicit conversions. Instead, you would just use a function. For example, if you have a char and want to convert that explicitly into an int, in C# you write:



            char theChar = 'A';
            int convertedChar = (int)theChar;


            In F#, the int operator (function) is used for the same purpose:



            let theChar = 'A'
            let convertedChar = int theChar;


            Therefore the idiomatic way to do the conversion would be something like this:



            module Some.Namespace.MyClass
            let toInt (x : MyClass) = [...]


            You would use it like so:



            let convertedMyClass = MyClass.toInt myClass


            It can also be piped:



            funcReturningMyClass x y
            |> MyClass.toInt
            |> printfn "%d"





            share|improve this answer




























              2














              It is my understanding that F# does not usually do explicit conversions. Instead, you would just use a function. For example, if you have a char and want to convert that explicitly into an int, in C# you write:



              char theChar = 'A';
              int convertedChar = (int)theChar;


              In F#, the int operator (function) is used for the same purpose:



              let theChar = 'A'
              let convertedChar = int theChar;


              Therefore the idiomatic way to do the conversion would be something like this:



              module Some.Namespace.MyClass
              let toInt (x : MyClass) = [...]


              You would use it like so:



              let convertedMyClass = MyClass.toInt myClass


              It can also be piped:



              funcReturningMyClass x y
              |> MyClass.toInt
              |> printfn "%d"





              share|improve this answer


























                2












                2








                2







                It is my understanding that F# does not usually do explicit conversions. Instead, you would just use a function. For example, if you have a char and want to convert that explicitly into an int, in C# you write:



                char theChar = 'A';
                int convertedChar = (int)theChar;


                In F#, the int operator (function) is used for the same purpose:



                let theChar = 'A'
                let convertedChar = int theChar;


                Therefore the idiomatic way to do the conversion would be something like this:



                module Some.Namespace.MyClass
                let toInt (x : MyClass) = [...]


                You would use it like so:



                let convertedMyClass = MyClass.toInt myClass


                It can also be piped:



                funcReturningMyClass x y
                |> MyClass.toInt
                |> printfn "%d"





                share|improve this answer













                It is my understanding that F# does not usually do explicit conversions. Instead, you would just use a function. For example, if you have a char and want to convert that explicitly into an int, in C# you write:



                char theChar = 'A';
                int convertedChar = (int)theChar;


                In F#, the int operator (function) is used for the same purpose:



                let theChar = 'A'
                let convertedChar = int theChar;


                Therefore the idiomatic way to do the conversion would be something like this:



                module Some.Namespace.MyClass
                let toInt (x : MyClass) = [...]


                You would use it like so:



                let convertedMyClass = MyClass.toInt myClass


                It can also be piped:



                funcReturningMyClass x y
                |> MyClass.toInt
                |> printfn "%d"






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 11:23









                dumetrulodumetrulo

                1,343310




                1,343310

















                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues