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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:41:43+00:00 2026-06-08T02:41:43+00:00

Let’s say I am solving a particular problem and come up with a function

  • 0

Let’s say I am solving a particular problem and come up with a function

let function parameter1 ... = 
     a lot of adding, multiplying & so on with a lot of 
     literals all over the place

Now this function works fine if my parameters are of type int. But somewhere I will need it to go up to 11, I will need that extra push into int64 or even BigInteger. So what do I do?
I copy&paste the function, change the name, and go hunting for all literal appearances that make the compiler think the function should operate on int. And this sucks.

Is there a way to do this:

let greatFunction param1 param2 = (param1+1)/(param2*2)

where param1 and param2 can be any combo of integer types?

Edit:

Expanding a bit on a great tip by kvb below, I came up with the following

module NumericLiteralG 

  let inline FromZero() = LanguagePrimitives.GenericZero
  let inline FromOne() = LanguagePrimitives.GenericOne
  let inline FromInt32 n =
    let rec loop nIn nOut = 
        if nIn>0 then loop (nIn - 1) (nOut + LanguagePrimitives.GenericOne)
        else nOut
    loop n LanguagePrimitives.GenericZero

so it becomes a bit less ugly when used

let inline halfSquare num =
   let res = num / 2G
   res * res

let solve1 = halfSquare 5I 
let solve2 = halfSquare 5.0
let solve3 = halfSquare 5uy

Now the question is should I use this? If not, why not?

  • 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-06-08T02:41:45+00:00Added an answer on June 8, 2026 at 2:41 am

    I think Generic Arithmetic is common problem in .NET languages.
    There are many articles explaining different approaches and very soon I will post another one explaining mine which is similar to the solution you posted.

    Now, if you ask me if should you use it, I would say: as long as you understand what you are doing why not? I’m using it partially in production and have no issues at all, but because I care about run-time performance I use overloading to resolve everything at compile time.
    Then to speed up compile time I redefine basic math operators to operate in the same type, otherwise type signatures get really complicated and may take ages to compile.

    There are more things to consider but for your specific problem here is a sample code:

    open System.Numerics
    
    type FromInt = FromInt with
        static member ($) (FromInt, _:sbyte     ) = fun (x:int) -> sbyte      x
        static member ($) (FromInt, _:int16     ) = fun (x:int) -> int16      x
        static member ($) (FromInt, _:int32     ) = id
        static member ($) (FromInt, _:float     ) = fun (x:int) -> float      x
        static member ($) (FromInt, _:float32   ) = fun (x:int) -> float32    x
        static member ($) (FromInt, _:int64     ) = fun (x:int) -> int64      x
        static member ($) (FromInt, _:nativeint ) = fun (x:int) -> nativeint  x
        static member ($) (FromInt, _:byte      ) = fun (x:int) -> byte       x
        static member ($) (FromInt, _:uint16    ) = fun (x:int) -> uint16     x
        static member ($) (FromInt, _:char      ) = fun (x:int) -> char       x
        static member ($) (FromInt, _:uint32    ) = fun (x:int) -> uint32     x
        static member ($) (FromInt, _:uint64    ) = fun (x:int) -> uint64     x
        static member ($) (FromInt, _:unativeint) = fun (x:int) -> unativeint x
        static member ($) (FromInt, _:bigint    ) = fun (x:int) -> bigint     x
        static member ($) (FromInt, _:decimal   ) = fun (x:int) -> decimal    x
        static member ($) (FromInt, _:Complex   ) = fun (x:int) -> Complex(float x,0.0)  
    
    let inline fromInt (a:int) : ^t = (FromInt  $  Unchecked.defaultof< ^t>) a
    
    module NumericLiteralG =
        let inline FromZero() =LanguagePrimitives.GenericZero
        let inline FromOne() = LanguagePrimitives.GenericOne
        let inline FromInt32 (i:int)     = fromInt i
    
    
    // This will reduce the number of types inferred, will reduce compile time too.
    let inline (+) (a:^t) (b:^t) : ^t = a + b
    let inline (-) (a:^t) (b:^t) : ^t = a - b
    let inline (*) (a:^t) (b:^t) : ^t = a * b
    let inline (/) (a:^t) (b:^t) : ^t = a / b
    let inline (~-) (a:^t) : ^t = -a
    
    
    let inline halfSquare num =
       let res = num / 2G
       res * res
    
    let solve1 = halfSquare 5I 
    let solve2 = halfSquare 5.0
    let solve3 = halfSquare 5uy 
    
    // Define more generic math functions.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say I got a Map<String, String> and I wanna remove all the entries
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a large amount of files, say 20GB worth, all encrypted
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let me try to explain by example. Say website is hosted at example.com (NOT
Let's say I have a table with a Color column. Color can have various

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.