How to do explicit overloaded conversion in F# like in C#? [duplicate]
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
)
f# type-conversion c#-to-f#
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.
add a comment |
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
)
f# type-conversion c#-to-f#
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.
add a comment |
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
)
f# type-conversion c#-to-f#
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#
f# type-conversion c#-to-f#
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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"
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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"
add a comment |
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"
add a comment |
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"
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"
answered Nov 16 '18 at 11:23
dumetrulodumetrulo
1,343310
1,343310
add a comment |
add a comment |