f# - Records satisfying implicit interface w/o re-implementing -
seeing record has public getter/(setter) fields, possible specify record satisfies matching interface without re-implementing it?
for example:
type itext = abstract text : string get,set type textrec = { mutable text : string }
now seeing record implements interface implicitly, i'd put "inherit itext" or "interface itext" (with no body) on record, appears can't that. is, believe have re-implement interface adding record:
interface itext member this.text get() = this.text , set(v) = this.text <- v
thanks
f# not support implicit interface implementations (not classes), 1 of frequently requested features, might happen in future. see why useful.
i don't think there workaround - best option write additional piece of code needed implement interface.
if wanted adventurous, try writing "wrapping" function creates interface implementation value provides required members. using static member constraints, can require members there (without implementing interface):
type itext = abstract text : string get, set let inline wrap (a:^t) = { new itext member x.text get() = (^t : (member text : string) (a)) , set(v) = (^t : (member set_text : string -> unit) (a, v)) }
static member constraints (used in implementation of wrap
) useful generic numerical computations, bit of stretch (and advanced f# feature), trick:
type rect = { mutable text : string } let = wrap { text = "hi" } i.text <- i.text + " there!"
Comments
Post a Comment