Использование обобщений в F # для создания типа EnumArray - PullRequest
2 голосов
/ 13 мая 2010

Я создал класс F # для представления массива, который выделяет один элемент для каждого значения определенного перечисления. Я использую явный конструктор, который создает словарь из значений перечисления для индексов массива, и свойство Item, чтобы вы могли писать выражения вроде:

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

Однако я получаю следующую неясную ошибку компиляции в строке, помеченной '// FS0670' ниже.

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.

Я в растерянности - кто-нибудь может объяснить эту ошибку?

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 Ответ

5 голосов
/ 13 мая 2010

Вот, пожалуйста:

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"

Ключевым аспектом является LanguagePrimitives.EnumToValue , который устраняет необходимость в статических членских ограничениях. (Использование статических ограничений членов достаточно плохо, но когда с ними что-то не так, диагностика компилятора еще хуже.)

...