F# Higher-order property accessors
Posted
by Nathan Sanders
on Stack Overflow
See other posts from Stack Overflow
or by Nathan Sanders
Published on 2010-06-07T20:40:40Z
Indexed on
2010/06/07
20:42 UTC
Read the original article
Hit count: 244
I just upgraded my prototyping tuple to a record. Someday it may become a real class. In the meantime, I want to translate code like this:
type Example = int * int
let examples = [(1,2); (3,4); (5,6)]
let field1s = Seq.map (fst >> printfn "%d") examples
to this:
type Example = {
Field1 : int
Field2 : int
Description : string
}
let examples = [{Field1 = 1; Field2 = 2; Description = "foo"}
{Field1 = 3; Field2 = 4; Description = "bar"}
{Field1 = 5; Field2 = 6; Description = "baz"}]
let field1s = Seq.map Description examples
The problem is that I expected to get a function Description : Example -> string
when I declared the Example record, but I don't. I've poked around a little and tried properties on classes, but that doesn't work either. Am I just missing something in the documentation or will I have to write higher-order accessors manually? (That's the workaround I'm using now.)
© Stack Overflow or respective owner