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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:02:02+00:00 2026-06-12T20:02:02+00:00

I have a discriminated union, such as type Dish = | Eggs | Spam

  • 0

I have a discriminated union, such as

type Dish = 
| Eggs
| Spam of Dish

This is basically a linked list, without any content, e.g. Spam(Spam(Spam(Eggs))). I want to strictly perform a computation on this structure, such as counting the length, and memorize the result. In a normal type, I’d use class-local let bindings, but those aren’t available in discriminated unions.

One way to do this would be,

type Count = int
type Dish = 
| Eggs
| Spam of Dish * Count

But this is really messy, when the data I need is easily computable, but I still hope there is a better way (without using external mutable constructs).

  • 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-12T20:02:03+00:00Added an answer on June 12, 2026 at 8:02 pm

    After reviewing the answers, I’ve decided to go with a model that seems the least obtrusive to me. I’ve used a modified object to demonstrate how it would work in a slightly more complex scenario.

    type StackDef<'a>(v : 'a, s : Stack<'a>) = 
        member val Length = s.Length + 1
        member val Inner = v, s 
    and Stack<'a> = 
        | Empty
        | Stack of StackDef<'a>
        member this.Length = 
            match this with
            | Empty -> 0
            | Stack(def) -> def.Length
    
    let Stack (v, s) = Stack(StackDef(v, s)) 
    let (|Stack|Empty|) = function | Empty -> Empty | Stack(sd) -> Stack(sd.Inner)
    //...
    let example = Stack(1, Stack(2, Stack(3, Empty))).Length
    
    1. It doesn’t contain any external mutable state.
    2. The discriminated union Dish (or in the example, Stack) continues to exist.
    3. The field length doesn’t appear in the union definition at all, nor is it provided by any constructor, just as it should be.
    4. The memoized data is associated with the instance, as it should be.

    However, having thought about it, by using a static weaver such as Afterthought it might be possible to replace any method such as:

    Stack<'a> = 
        | Empty
        | Stack of 'a * Stack<'a>
    
        [<Lazy>] //custom attribute that would work with a static weaver
        member this.Length = 
            match this with
            | Empty -> 0
            | Stack(_, s) -> s.Length + 1
    

    With a private readonly Lazy<int> __length initialized in the constructor with a delegate that executes the above code, and change the actual content of the method to simply invoking __length.Value.
    While F# doesn’t allow union types to contain fields, possibly for very valid reasons, I highly doubt the IL would have such restrictions.
    In fact, it would be possible to do a lot of things using some IL manipulation. Maybe it’s something to think about.

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

Sidebar

Related Questions

I have the following discriminated union: type ActCard = Cellar of card list |
For a small AST parser i have a small discriminated union type Numerical =
Say I have the following single case discriminated union: type OrderId = OrderId of
Let's say I have a discriminated union: type foo = Bar | Baz |
Let's say I have the following discriminated union and value: type DisUnion = |
I have 2 nested discriminated unions: type ServiceTypes = | Contexts | Context of
I have a function, which can returns different types, and I use discriminated union
this is what i have so far: type u = {str : string} //some
I have a Discriminated Union, and I hope to use built in operators like
have written this little class, which generates a UUID every time an object of

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.