The following code does not compile:
[<Struct>]
type Point(x:int, y:int) =
member __.X = x
member __.Y = y
member __.Edges = ArrayList<Edge>()
[<Struct>]
and Edge(target:Point, cost:int) =
member __.Target = target
member __.Cost = cost
The problem resides on the [<Struct>]
attributes, that seem to collide with the "and" construct.
How should I go about doing this? I know I could alternatively accomplish the task with
type Point(x:int, y:int) =
struct
member __.X = x
member __.Y = y
member __.Edges = new ArrayList<Edge>()
end
and Edge(target:Point, cost:int) =
struct
member __.Target = target
member __.Cost = cost
end
but I like the [<Struct>]
succinctness.
Thanks
Move the attribute definition after the and
token
and [<Struct>] Edge(target:Point, cost:int) =
To elaborate Jared's answer:
per F# spec (8. Type Definitions) Custom attributes may be placed immediately before a type definition group, in which case they apply to the first type definition, or immediately before the name of the type definition
meaning that you can also use this style:
type
[<Struct>]
A(x : int) =
member this.X = x
and
[<Struct>]
B(y : int) =
member this.Y = y
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With