Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4084640
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:27:57+00:00 2026-05-20T18:27:57+00:00

I have a function, which can returns different types, and I use discriminated union

  • 0

I have a function, which can returns different types, and I use discriminated union for this. What I need, is to have conversion from one type in discriminated union to another type.
Also some of the types can be convertable to all other types (String), but some of the types can be converted only to String (MyCustomType)

For this I’ve added member method ConvertTo to the ResultType:

type MyTypes = 
   | Boolean       = 1
   | Integer       = 2
   | Decimal       = 3
   | Double        = 4
   | String        = 5
   | MyCustomType  = 6

type ResultType = 
   | Boolean of bool
   | Integer of int
   | Decimal of decimal
   | Double of double
   | String of string
   | MyCustomType of MyCustomType

   with 
     member this.ConvertTo(newType: MyTypes) = 
       match this with 
       | ResultType.Boolean(value) -> 
           match newType with 
           | MyTypes.Boolean -> 
              this
           | MyTypes.Integer -> 
              ResultType.Integer(if value then 1 else 0)
          ...
       | ResultType.MyCustomType(value) -> 
           match newType with 
           | MyTypes.MyCustomType -> 
              this
           | MyTypes.String -> 
              ResultType.String(value.ToString()) 
           | _ -> 
              failwithf "Conversion from MyCustomType to %s is not supported" (newType.ToString())

I don’t like such construction, because if I add more types, this requires me to do many changes: MyTypes, ResultType and also in several places in the ConvertTo member function.

Can anybody suggest better solution for such types conversion?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:27:57+00:00Added an answer on May 20, 2026 at 6:27 pm

    With a slightly different design, it is possible to exploit System.Convert.ChangeType and the fact that the constructors of discriminated unions are actually functions:

    // statically typed wrapper for System.Convert.ChangeType
    let conv a : 'T = System.Convert.ChangeType(a, typeof<'T>) :?> 'T
    
    type MyCustomType() = class end
    
    type ResultType = 
      | Boolean of bool
      | Integer of int
      | Decimal of decimal
      | Double of double
      | String of string
      | MyCustomType of MyCustomType
      with
        member this.ConvertTo (newType:'T->ResultType) =
          match this with
          | Boolean b -> newType( conv b )
          | Integer i -> newType( conv i )
          | Decimal d -> newType( conv d )
          | Double d -> newType( conv d )
          | String s -> newType( conv s )
          | MyCustomType m ->
             if typeof<'T> <> typeof<string> then
                raise (new System.InvalidCastException("MyCustomType can only be converted to String"))
             else
                String (m.ToString())
    
    let i = Integer 42
    
    let b = i.ConvertTo Boolean
    printfn "%A" b
    
    let d = i.ConvertTo Decimal
    printfn "%A" d
    
    let d2 = i.ConvertTo Double
    printfn "%A" d2
    
    let s = i.ConvertTo String
    printfn "%A" s
    
    //let mi = i.ConvertTo MyCustomType  // throws InvalidCastException
    
    let m = MyCustomType (new MyCustomType())
    let sm = m.ConvertTo String
    printfn "%A" sm
    
    //let im = m.ConvertTo Integer // throws InvalidCastException
    

    EDIT: Once you add more custom types, this will not help much.

    Maybe you should make your custom types implement IConvertible. Then you can remove the special case code from ConvertTo and completely rely on System.Convert.ChangeType.

    You would still have to extend every custom type’s ToObject implementation whenever you add a new custom type. Whether that really is better than a central ConvertTofunction is debatable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

if i have a function A,which can apply a certain rule on a given
I have a function which parses one string into two strings. In C# I
I have a function which searches an STL container then returns the iterator when
I have a function which gets a key from the user and generates a
i have a function which retrieves values from a webservice , then loops through
Imagine I have an function which goes through one million/billion strings and checks smth
This is a Windows Forms application. I have a function which captures some mouse
i have a c function which returns a long double . i'd like to
We have a database library in C# that we can use like this: DatabaseConnection
I have an function which decodes the encoded base64 data in binary data but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.