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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:10:05+00:00 2026-05-26T14:10:05+00:00

For a small AST parser i have a small discriminated union type Numerical =

  • 0

For a small AST parser i have a small discriminated union

type Numerical =
    | Int of int
    | Real of float

for the use in some other constructs like

type Vector = Numerical list
Vector [Int 42; Real 13.5]

When i have a method like this

let eval (e:Numerical) =
    match e with
    | Int n -> ... (* return int *)
    | Real r -> ... (* return float *)

I know that F# infers the int type and produces an error with the Real pattern in the second line, so I want to know, which code-design would be the best one to be able to handle such “generic” types and return their appropriate values with the given types.

EDIT

I have here an either or situation which leads to functions like

let getInt = function
| Int n -> n
| Real _ -> failwith "Given value doesn't represent an int"

let getReal = function
| Real n -> n
| Int _ -> failwith "Given value doesn't represent a real number"

however i would like a method which encapsulates both cases and “automatically chooses the correct one”.

This whole effort should lead to the possibility to be able to really run computations with the “boxed” values like Int 42 and Real 13. with primitive data types but to able to return the appropriate wrapper. If i want to add Real 1. and Real 1.5 I want to extract 1.0 + 1.5 = 2.5 and then go on with Real 2.5, however I do not want to handle everything as floats so that I have the distinction between ints and floats.

  • 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-26T14:10:05+00:00Added an answer on May 26, 2026 at 2:10 pm

    You could cast the results to obj:

    let eval (e:Numerical) =
        match e with
        | Int n -> n :> obj
        | Real r -> r :> obj
    

    But that’s probably not what you want.

    Another option would be implement your own operations on Numerical:

    let (+) a b =
        match (a,b) with
        | (Int an, Int bn) -> Int (an + bn)
        | (Real ar, Real br) -> Real (ar + br)
        | _ -> failwith "Can't add Int and Real"
    

    You can’t have a function that has different compile-time return type based on some run-time value.

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

Sidebar

Related Questions

Small preamble. I was good java developer on 1.4 jdk. After it I have
A small - little question. I have seen many application having buttons like following.
We (small team) currently have our Visual Studio projects on a network drive (no
have small problem, and would very much appreciate help :) I should convert byte
I have a small JS function that does Ajax for me and another like
small questions about Restrictions. i have query like this : Where A in(1,6) and
.model small .stack 100 .data .code mov ah,00h mov al,0e3h mov dx,00h int 14h
I'm writing a compiler for a small language, and my Parser class is currently
I have a set S of small trees S[i] which I need to find
I have a small custom scripting language, and I am trying to update it

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.