F # quotes, arrays and self-identifiers in constructors

I think the known limitation is F #, but I did not find any good workarounds ...

So, here is the code (I tried to make it as simple as possible, so it probably looks like it makes no sense):

[<ReflectedDefinition>]
type Human (makeAName: unit -> string) as self =
    let mutable cats : Cat array = [| |]
    do
        // get a cat
        cats <- Array.append cats [| new Cat (self, makeAName ()) |]
    member this.Cats = cats
and
 [<ReflectedDefinition>]
 Cat (owner : Human, name : string) = class end

The compiler says:

error FS0452: quotes cannot contain embedded assembly code or pattern matching on arrays

This is actually a combination as selfand getter getter properties that breaks everything.

Points here:

  • I really want to use arrays because I want WebSharper to translate my collections into JavaSript arrays.
  • I really need a self-identifier in the constructors.
  • I really need classes (i.e. functional style will not work).
  • (member this.Foo) .

, , . as self. .

?


Update:

:

[<ReflectedDefinition>]
type User (uid: int) as self =
    let ROOT_UID = 0
    member this.isRoot = (uid = ROOT_UID)

as self . , , : ?


+5
1

, . , , WebSharper. IntelliFactory , - self.

F # . , self:

[<ReflectedDefinition>]
type Human private (cats: ref<Cat []>) =
    member this.Cats = !cats

    static member Create(makeAName: unit -> string) =
        let cats = ref [| |]
        let h = Human(cats)
        let cat = Cat(h, makeAName())
        cats := [| cat |]
        h

and [<ReflectedDefinition>] Cat (owner: Human, name: string) =
    class
    end

, , ref.

-, FS0452 ReflectedDefinition , . (Array.iter, Array.map).

:

[<ReflectedDefinition>]
module Users =

    [<Literal>]     
    let ROOT_UID = 0

    type User(uid: int) =
        member this.isRoot = (uid = ROOT_UID)

[<Literal>] , , .

:

  • -
  • - ,
  • (.. ) -
  • (member this.Foo) -
+8

All Articles