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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:31:04+00:00 2026-05-14T20:31:04+00:00

I’ve created an F# class to represent an array that allocates one element for

  • 0

I’ve created an F# class to represent an array that allocates one element for each value of a specific enum. I’m using an explicit constructor that creates a dictionary from enum values to array indices, and an Item property so that you can write expressions like:

let my_array = new EnumArray<EnumType, int>
my_array.[EnumType.enum_value] <- 5

However, I’m getting the following obscure compilation error at the line marked with ‘// FS0670’ below.

error FS0670: This code is not sufficiently generic.
The type variable  ^e when  ^e : enum<int> and  ^e : equality
and  ^e : (static member op_Explicit :  ^e -> int)
could not be generalized because it would escape its scope.

I’m at a loss – can anyone explain this error?

type EnumArray< 'e, 'v when 'e : enum<int>  //'
                       and 'e : equality
                       and 'e : (static member op_Explicit :  'e -> int) > =
    val enum_to_int : Dictionary<'e, int>  //'
    val a : 'v array  //'

    new() as this =
        { 
            enum_to_int = new Dictionary<'e, int>()  //'
            a = Array.zeroCreate (Enum.GetValues(typeof<'e>).Length)  //'
        }
        then
            for (e : obj) in Enum.GetValues(typeof<'e>) do  //'
                this.enum_to_int.Add(e :?> 'e, int(e :?> 'e))

    member this.Item
        with get (idx : 'e) : 'v = this.a.[this.enum_to_int.[idx]]  // FS0670
        and set (idx : 'e) (c : 'v) = this.a.[this.enum_to_int.[idx]] <- c
  • 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-14T20:31:05+00:00Added an answer on May 14, 2026 at 8:31 pm

    Here you go:

    open System
    open System.Collections.Generic 
    
    type EnumArray<'e, 'v when 'e : enum<int> and 'e : equality>() = 
        let dict = new Dictionary<'e, int>()    //'
        let values = Enum.GetValues(typeof<'e>) //'
        let a = Array.zeroCreate values.Length
        do
            for (o : obj) in values do 
                let e = o :?> 'e   //'
                dict.Add(e, LanguagePrimitives.EnumToValue(e)) 
        member this.Item 
            with get idx  = a.[dict.[idx]]
            and set idx c = a.[dict.[idx]] <- c
    
    let d = new EnumArray<StringSplitOptions, string>()
    d.[StringSplitOptions.None] <- "foo"
    d.[StringSplitOptions.RemoveEmptyEntries] <- "bar"
    

    A key aspect is LanguagePrimitives.EnumToValue, which removes the need for the static member constraints. (Use of static member constraints is bad enough, but when things fail with them, the compiler diagnostics are even worse.)

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

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Stupid mistake on my part. The "parent" is an abstract… May 14, 2026 at 10:39 pm
  • Editorial Team
    Editorial Team added an answer You can either load the existing Group object, say group,… May 14, 2026 at 10:39 pm
  • Editorial Team
    Editorial Team added an answer You can use the document() XPath function to load an… May 14, 2026 at 10:39 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.