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 7417921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:47:27+00:00 2026-05-29T07:47:27+00:00

I have a variant type like this: type score = InInteger of int |

  • 0

I have a variant type like this:

type score = 
    InInteger of int
    | InFloat of float ;;

Now, given two scores (InInteger(5) and InFloat(5.5)), I want to add, subtract them etc..

How can this be done?

PS – I’m a newbie to OCaml.

Edit::

More specifically:

How does this work?

let n = InInt(2);;
let m = InFloat(3.2);;

let var = InFloat(float n +.  m);;
  • 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-29T07:47:28+00:00Added an answer on May 29, 2026 at 7:47 am

    First, discriminated unions require their identifiers starting with upper cases:

    type score = 
        InInteger of int
        | InFloat of float
    

    Second, you can define an add function on this datatype by pattern matching all possible cases and returning appropriate values:

    let add s1 s2 = 
        match s1, s2 with
        | InInteger i1, InInteger i2 -> InInteger (i1 + i2)
        | InInteger i1, InFloat f2   -> InFloat (float i1 +. f2)
        | InFloat f1, InInteger i2   -> InFloat (f1 +. float i2)
        | InFloat f1, InFloat f2     -> InFloat (f1 +. f2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a boost variant of looking like this: typedef boost::variant<int, float, double, long,
Let's say that I have a boost::variant<std::string, int> myVariant; In this object I keep
I have a procedure that expects a parameter of type TObject, something like this:
I have a typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant which I
Given a variant, does VBScript have an equivalent of C#'s DateTime.TryParse method?
I have a 2D array of type Variant . The size and values that
I have a template class that looks something like this: template<class T> class C
I have tables like this: Table1 Table2 name1 | link_id name2 | link_id text
I am using SimpleJdbcTemplate and for example I have something like this: @Override public
I have a function in a module that looks something like this: module MyLibrary

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.