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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:09:14+00:00 2026-05-18T04:09:14+00:00

F# is giving me some trouble with its type inference rules. I’m writing a

  • 0

F# is giving me some trouble with its type inference rules. I’m writing a simple computation builder but can’t get my generic type variable constraints right.


The code that I would want looks as follows in C#:

class FinallyBuilder<TZ>
{
    readonly Action<TZ> finallyAction;

    public FinallyBuilder(Action<TZ> finallyAction)
    {
        this.finallyAction = finallyAction;
    }

    public TB Bind<TA, TB>(TA x, Func<TA, TB> cont)  where TA : TZ
    {                                      //        ^^^^^^^^^^^^^
        try                                // this is what gives me a headache
        {                                  //      in the F# version
            return cont(x);
        }
        finally
        {
            finallyAction(x);
        }
    }
}

The best (but non-compiling code) I’ve come up with for the F# version so far is:

type FinallyBuilder<′z> (finallyAction : ′z -> unit) =

    member this.Bind (x : ′a) (cont : ′a -> ′b) =
        try     cont x
        finally finallyAction (x :> ′z) // cast illegal due to missing constraint

// Note: ' changed to ′ to avoid bad syntax highlighting here on SO.

Unfortunately, I have no clue how I would translate the where TA : TZ type constraint on the Bind method. I thought it should be something like ′a when ′a :> ′z, but the F# compiler doesn’t like this anywhere and I always end up with some generic type variable constrained to another.

Could someone please show me the correct F# code?


Background: My goal is to be able to write an F# custom workflow like this:

let cleanup = new FinallyBuilder (fun x -> ...)

cleanup {
    let! x = ...   // x and y will be passed to the above lambda function at
    let! y = ...   // the end of this block; x and y can have different types! 
}
  • 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-18T04:09:15+00:00Added an answer on May 18, 2026 at 4:09 am

    I don’t think it is possible to write constraint like this in F# (although I’m not exactly sure why). Anyway, syntactically, you’d want to write something like this (as Brian suggests):

    type FinallyBuilder<'T> (finallyAction : 'T -> unit) = 
      member this.Bind<'A, 'B when 'A :> 'T>(x : 'A) (cont : 'A -> 'B) =  //' 
        try cont x 
        finally finallyAction (x :> 'T) 
    

    Unfortunately, this gives the following error:

    error FS0698: Invalid constraint: the type used for the constraint is sealed, which means the constraint could only be satisfied by at most one solution

    This seems to be the same case as the one discussed in this mailing list. Where Don Syme says the following:

    This is a restriction imposed to make F# type inference tractable. In particular, the type on the right of a subtype constraint must be nominal. Note constraints of the form ‘A :> ‘B are always eagerly solved to ‘A = ‘B, as specified in section 14.5.2 (Solving Subtype Constraints) of the F# specification.

    You can always solve this by using obj in the function passed to your builder.
    EDIT: Even when you use obj, the values bound using let! will have more specific types (when calling finallyAction, F# will automatically cast the value of some type parameter to obj):

    type FinallyBuilder(finallyAction : obj -> unit) =  
      member x.Bind(v, f) =  
        try f v 
        finally finallyAction v 
      member x.Return(v) = v
    
    let cleanup = FinallyBuilder(printfn "%A")
    
    let res = 
      cleanup { let! a = new System.Random()
                let! b = "hello"
                return 3 }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to start learning java but Eclipse is giving me some trouble. First
I´ve got this CRac class and it´s giving me some problems with its members.
I am using some NHibernate 1.2 code with NHibernate 2.0 and its giving me
I'm working on an application that it's been giving me some trouble, partly because
I am trying to learn Ruby, and arrays are giving me some trouble. I
I am having some trouble with Python giving me a result I do not
I think I understand ViewState pretty well, but the following is giving me some
As you can tell by the title, I am having some trouble with AS3
I have a linq union statement that has been giving me some trouble and
New ASP.NET Web API HttpClient has been giving me some strange results. Here is

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.