f# - How do I overload operators for WPF containers? -
type addertype() = /// appends container. static member (+) (cont:dockpanel,child:#uielement) = cont.children.add child |> ignore child
when make class above , try this.
let dock = dockpanel() let win = window(title = "check window style", content = dock) let menu = dock + menu()
i error none of types 'dockpanel,menu' support operator '+'.
inspired make above phil trelford's binding example goes this:
type dependencypropertyvaluepair(dp:dependencyproperty,value:obj) = member this.property = dp member this.value = value static member (+) (target:#uielement,pair:dependencypropertyvaluepair) = target.setvalue(pair.property,pair.value) target
the above reason works. have no idea why. possible overload +
or other operator elegantly add controls containers?
operators defined inside class work if 1 of arguments instance of class, can define operator global operator:
let (++) (cont:dockpanel) (child:#uielement) = cont.children.add child |> ignore child
the following should work:
let dock = dockpanel() let win = window(title = "check window style", content = dock) let menu = dock ++ menu()
but honest, don't think kind of problem place using custom operators. using +
here confusing, because not adding 2 things in sense. operator not commutative, e.g. (a ++ b) <> (b ++ a)
.
i think more idiomatic code define named function , use |>
:
let appendto (cont:dockpanel) (child:#uielement) = cont.children.add child |> ignore child let dock = dockpanel() let win = window(title = "check window style", content = dock) let menu = menu() |> appendto dock
Comments
Post a Comment