user11466165
user11466165

Reputation:

Adding element to a couchbase database with f# code

Hi I am trying to add a simple document to a couchbase database from F# however I am having some issues creating the document

I have simply tried a very barebones approach by trying to translate c# to f# its worked fine until the document definition

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

The compiler will complain the the "dynamic" keyword does not exist? Seems very odd as it works fine in c#.

Upvotes: 1

Views: 78

Answers (1)

Fyodor Soikin
Fyodor Soikin

Reputation: 80754

dynamic is not a real type. It's just a special syntax in C# that tells the compiler to treat variables of this type in a special way.

When you declare something dynamic in C#, the actual type it gets is Object, which in F# is called obj. So the equivalent would be to cast to Document<obj>.

But I don't think even what would work: you cannot cast your type to Document, because your type is not a subtype of Document (i.e. doesn't inherit from it). The compiler will complain.

You need to either make your type a subtype of Document, or, perhaps, you meant to cast to IDocument instead, which is what IBucket.Insert expects? At this point I can't help you anymore, because it's not clear what you meant.

Upvotes: 3

Related Questions