F# type constraint for record type with specific property

前端 未结 3 1424
失恋的感觉
失恋的感觉 2021-01-17 22:01

I\'m trying to create a generic function which requires of its type argument that it is a record type, and that it has a specific property. Here\'s a sample that generates t

相关标签:
3条回答
  • 2021-01-17 22:47

    I'm not sure what your goal is.

    If what you want is to read properties of a generic record see TheInnerLight's working example.

    If, instead, you want to write a function that clones many types of records, then you should change your design. You can follow the approach suggested by Tomas.

    In addition to all that, here's another alternative: use a nested generic record.

    type Test<'a> = {Bar : string; Rest : 'a}
    
    type A = {PropA : string}
    type B = {PropB : int}
    
    let a = {Bar = "bar"; Rest  = {PropA = "propA" }}
    
    
    let foo a = {a with Bar = "foo " + a.Bar}
    
    let foobar = foo {Bar = "bar"; Rest = {PropA = "propA"}} 
    // val foobar : Test<A> = {Bar = "foo bar"; Rest = {PropA = "propA";};}
    
    let foobar' = foo {Bar = "bar"; Rest = {PropB = 0}}
    // val foobar' : Test<B> = {Bar = "foo bar"; Rest = {PropB = 0;};}
    

    As a final note, with this design, you can move to Lenses as FyodorSoikin initially suggested.

    0 讨论(0)
  • 2021-01-17 22:50

    I would suggest reading Tomas' answer first. Using statically resolved type constraints should generally be avoided when possible. They're a feature of the F# compiler rather than .NET so they do, to some extent, restrict the reusability of your code. That said, they are very powerful and do allow you to impose useful constraints at compile time.

    The syntax for using them is also not terribly pleasant but if you remain undeterred, you could do something like this:

    type Test = {Bar : string}
    
    let inline foo (a : ^a) =
        "foo " + ((^a) : (member Bar : string) (a))
    
    let foobar = foo {Bar = "bar"} // prints "foo bar"
    

    Note however that you can't actually restrict the type to being a record, simply something that has a member Bar of type string. So this would also resolve:

    type Test2(str : string) = member this.Bar = str
    
    let foobar2 = foo (Test2("bar")) // prints "foo bar"
    
    0 讨论(0)
  • 2021-01-17 23:04

    I don't think there is a way to specify this using static member constraints - static member constraints are fairly restricted and they are mainly an abstraction mechanism that is available in addition to other more usual techniques.

    If I was trying to solve problem like this, I would probably consider using interfaces (this is not always the best way to do this, but without knowing more about your specific situation, it is probably a reasonable default approach):

    type ISetA<'T> = 
      abstract WithA : string -> 'T
    
    type MyRecord = 
      { A : string }
      interface ISetA<MyRecord> with
        member x.WithA(a) = { x with A = a }
    

    When implementing the record, you need to add an interface implementation (so you need to do a bit more work than if you could do this using just static member constraints). But then, you are also explicitly saying that this is an intended use for the type...

    The usage is also simpler than when using static constraints:

    let setA (setA:ISetA<_>) = 
      setA.WithA "Hello"
    
    setA { A = "Test" }
    
    0 讨论(0)
提交回复
热议问题