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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:35:46+00:00 2026-05-31T04:35:46+00:00

I would like to define a type such as: type blah = AThing |

  • 0

I would like to define a type such as:

type blah =
     AThing
   | AnotherThing
   with 
     static member ofString : string -> blah
     override x.ToString () : string

I would like to make sure that the two methods are always guaranteed to be consistent. A good way of doing this would be to construct two maps from the same list of pairs, and then turn them into the obvious implementations of the two methods. Roughly:

let pairs = [Athing, "AThing"; AnotherThing, "AnotherThing"]
let tostr = Map.ofList pairs
let toblah = pairs |> List.map swap |> Map.ofList

I think this code can only be defined in a static member function. The static bit is implied by it needing to be accessible from ofString, which is static. It cannot be defined before the type, since the list relies on it. It cannot be defined afterwards because F# does not allow methods to be just declared and implemented later (in the same way that e.g. C++ would). So that leaves the choice between a static member and a static let. The compiler says that static lets are not allowed in an augmentation. (?)

The code works fine when put into a static member function, however it makes the maps every time they are needed. Needless to say, this is even less efficient than linear search. How do I do it correctly please? Many thanks.

  • 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-31T04:35:48+00:00Added an answer on May 31, 2026 at 4:35 am

    Here’s the code that works as required:

    type blahFactory() =
        static let pairs = printf "array initialized; "; [AThing, "AThing"; AnotherThing, "AnotherThing"]
        static member tostr = Map.ofList pairs
        static member toblah = pairs |> List.map swap |> Map.ofList
    and blah =
        | AThing
        | AnotherThing
        with
        static member ofString (string) = blahFactory.toblah.[string]
        override x.ToString () = blahFactory.tostr.[x]
    

    I’ve placed printf instruction to demonstrate the array is initialized just once.

    Several thoughts you may consider useful.
    First, using DU is overhead for sample provided. If your data is that simple, you may consider enum types.
    If you really mean DU, that may be sign of possible problems in the future. Consider this:

    type blah =
        | AThing of string
        | AnotherThing of int * int
    

    Both construction and comparison will be impossible in this case.

    Do you have any reasons not to use standard XML serialization?

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

Sidebar

Related Questions

I would like to be able to define and use a custom type in
I would like to define data types hierarchically, such as: data Cat = BigCat
I would like to define a constant (like the admin-email-adress) depending on the environment.
I would like to define and initialize some variables in web.xml and the access
I would like to define some kind of safe division (and modulo) function, one
I would like to define the z order of the views of a RelativeLayout
I'm using Phil Haack's URL routing for WebForms and I would like to define
I have a WPF application with several windows. I would like to define GLOBAL
So , I've been reading this article: http://msdn.microsoft.com/en-us/library/aa290051%28VS.71%29.aspx And I would like to define
I would like to know how to define a returntype in a function in

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.