我应该如何修改我的Queue类以允许用户在F#中创建未指定类型的空队列?

时间:2021-09-14 18:55:11

I have created an immutable Queue in F# as follows:

我在F#中创建了一个不可变的队列,如下所示:

type Queue<'a>(f : 'a list, r : 'a list) =    
    let check = function
        | [], r -> Queue(List.rev r, [])
        | f, r -> Queue(f, r)

    member this.hd =
        match f with
        | [] -> failwith "empty"
        | hd :: tl -> hd

    member this.tl =
        match f, r with
        | [], _ -> failwith "empty"
        | hd::f, r -> check(f, r)

    member this.add(x) = check(f, x::r)

    static member empty : Queue<'a> = Queue([], [])

I want to create an instance of an empty Queue, however I get a value-restriction exception:

我想创建一个空Queue的实例,但是我得到一个值限制异常:

> let test = Queue.empty;;

  let test = Queue.empty;;
  ----^^^^

C:\Documents and Settings\juliet\Local Settings\Temp\stdin(5,5): error FS0030: Value restriction. The value 'test' has been inferred to have generic type val test : Queue<'_a> Either define 'test' as a simple data term, make it a function with explicit arguments or, if you do not intend for it to be generic, add a type annotation.

C:\ Documents and Settings \ juliet \ Local Settings \ Temp \ stdin(5,5):错误FS0030:值限制。值'test'被推断为具有泛型类型val测试:Queue <'_ a>将'test'定义为一个简单的数据项,使其成为具有显式参数的函数,或者,如果您不打算将其作为通用参数,添加类型注释。

Basically, I want the same kind of functionality seen in the Set module which allows me to write:

基本上,我想要在Set模块中看到的相同功能,它允许我写:

> let test = Set.empty;;

val test : Set<'a>

How can I modify my Queue class to allow users to create empty queues?

如何修改我的Queue类以允许用户创建空队列?

1 个解决方案

#1


You need to use GeneralizableValueAttribute, a la:

你需要使用GeneralizableValueAttribute,la:

type Queue<'a>(f : 'a list, r : 'a list) =  // '
    let check = function
        | [], r -> Queue(List.rev r, [])
        | f, r -> Queue(f, r)

    member this.hd =
        match f with
        | [] -> failwith "empty"
        | hd :: tl -> hd

    member this.tl =
        match f, r with
        | [], _ -> failwith "empty"
        | hd::f, r -> check(f, r)

    member this.add(x) = check(f, x::r)
module Queue =    
    [<GeneralizableValue>]
    let empty<'T> : Queue<'T> = Queue<'T>([], []) // '

let test = Queue.empty
let x = test.add(1)       // x is Queue<int>
let y = test.add("two")   // y is Queue<string>

You can read a little more about it in the language spec.

您可以在语言规范中阅读更多相关内容。

#1


You need to use GeneralizableValueAttribute, a la:

你需要使用GeneralizableValueAttribute,la:

type Queue<'a>(f : 'a list, r : 'a list) =  // '
    let check = function
        | [], r -> Queue(List.rev r, [])
        | f, r -> Queue(f, r)

    member this.hd =
        match f with
        | [] -> failwith "empty"
        | hd :: tl -> hd

    member this.tl =
        match f, r with
        | [], _ -> failwith "empty"
        | hd::f, r -> check(f, r)

    member this.add(x) = check(f, x::r)
module Queue =    
    [<GeneralizableValue>]
    let empty<'T> : Queue<'T> = Queue<'T>([], []) // '

let test = Queue.empty
let x = test.add(1)       // x is Queue<int>
let y = test.add("two")   // y is Queue<string>

You can read a little more about it in the language spec.

您可以在语言规范中阅读更多相关内容。