使用f#代码

时间:2019-06-14 09:33:17

标签: .net f# couchbase

嗨,我正在尝试从F#向Couchbase数据库添加一个简单的文档,但是在创建该文档时遇到了一些问题

我只是尝试了一种非常准系统的方法,即尝试将c#转换为f#,直到文档定义正常为止

open System
open Couchbase


type TestDoc(id : string, content : string) = 
     interface IDocument with
        member this.Cas
            with get (): uint64 = 
                failwith "Not Implemented"
            and set (v: uint64): unit = 
                failwith "Not Implemented"
        member this.Expiry
            with get (): uint32 = 
                failwith "Not Implemented"
            and set (v: uint32): unit = 
                failwith "Not Implemented"
       member this.Id
            with get (): string = 
               id
            and set (v: string): unit = 
               failwith "Not Implemented"
       member this.Token: Core.Buckets.MutationToken = 
            failwith "Not Implemented"



 let t() = 
    let cluster = new Cluster()
    let bucket = cluster.OpenBucket("beer-sample")
    let document = TestDoc ("hei", null) :> Document<dynamic>
    let res = bucket.Insert(document)
    res



[<EntryPoint>]
let main argv =
     printfn "Hello World from F#!"

      0 // return an integer exit code

编译器会抱怨“ dynamic”关键字不存在?似乎很奇怪,因为它在c#中可以正常工作。

1 个答案:

答案 0 :(得分:3)

dynamic不是实型。这只是C#中的一种特殊语法,它告诉编译器以特殊方式处理此类型的变量。

在C#中声明dynamic时,它得到的实际类型为Object,在F#中称为obj。因此,等效项将转换为Document<obj>

但是我什至不认为会行得通:您不能将类型强制转换为Document,因为您的类型不是Document的子类型(即不继承自它)。编译器会抱怨。

您需要将您的类型设为Document的子类型,或者也许是要转换为IDocument,这正是IBucket.Insert的期望?目前,我无法为您提供帮助,因为您的意思不清楚。