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

  • Home
  • SEARCH
  • 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 6580061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:58:41+00:00 2026-05-25T15:58:41+00:00

Does anyone know why sub throws an exception when add does not? And is

  • 0

Does anyone know why sub throws an exception when add does not? And is this a bug?

open Microsoft.FSharp.Linq.QuotationEvaluation

let inline add x = x + x
let inline sub x = x - x

let answer  = <@ add 1 @>.Eval() // 2, as expected
let answer2 = <@ sub 1 @>.Eval() // NotSupportedException

Note, without the inline keyword the exception is not thrown (but the code is not generic)
Also, the exception is only thrown when using quotations. Normal evaluation works fine.

Thanks

Edit: simplified code example

  • 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-25T15:58:42+00:00Added an answer on May 25, 2026 at 3:58 pm

    Thanks for this question – it is a really nice bug report with a simple repro and I couldn’t believe this, but you’re completely right. Plus works, but minus doesn’t.

    The problem is that sub and add are compiled as generic methods and the LINQ version invokes these generic methods. The inlining is performed after quotations are stored, so the quoted code contains call to the sub method. This isn’t a problem in normal F# code, because the functions are inlined and the operators are resolved to + or – over some numeric types.

    However, the generic version uses a dynamic lookup. If you look into prim-types.fs:3530, you’ll see:

    let inline (+) (x: ^T) (y: ^U) : ^V = 
      AdditionDynamic<(^T),(^U),(^V)>  x y 
      when ^T : int32       and ^U : int32      = (# "add" x y : int32 #)
      when ^T : float       and ^U : float      = (# "add" x y : float #)
      // ... lots of other cases
    

    The AdditionDynamic is what gets called from the generic method. It does the dynamic lookup, which will be slower, but it will work. Interestingly, for the minus operator, the F# library doesn’t include dynamic implementation:

    [<NoDynamicInvocation>]
    let inline (-) (x: ^T) (y: ^U) : ^V = 
      ((^T or ^U): (static member (-) : ^T * ^U -> ^V) (x,y))
      when ^T : int32      and ^U : int32      = (# "sub" x y : int32 #)
      when ^T : float      and ^U : float      = (# "sub" x y : float #)
      // ... lots of other cases
    

    I have no idea why this is the case – I don’t think there is any technical reason, but it explains why you get the behavior you reported. If you look at the compiled code using ILSpy, you’ll see that the add method does something and the sub method just throws (so this is where the exception comes from).

    As for a workaround, you need to write the code in a way in which it doesn’t use the generic minus operator. Probably the best option is to avoid inline functions (either by using sub_int or sub_float) or by writing your own dynamic implementation of sub (which can be done probably quite efficiently using DLR (see this post).

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

Sidebar

Related Questions

Does anyone know of a good method for editing PDFs in PHP? Preferably open-source/zero-license
Does anyone know of a faster way to enumerate through a directory and sub-folders
Does anyone know how replace all \ with \\ in python? Ive tried: re.sub('\','\\',string)
Does anyone know how to use Java to create sub-directories based on the alphabets
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does anyone know how to transform a enum value to a human readable value?
Does anyone know of a good Command Prompt replacement? I've tried bash/Cygwin, but that
Does anyone know of a FOSS Python lib for generating Identicons ? I've looked,
Does anyone know of any existing packages or libraries that can be used to
Does anyone know a good Java lib that will hook into SVN so I

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.