I have two types, and they have one named value.
type Type1 =
{
p1: int;
p2: int;
}
type Type2 =
{
p1 : int;
p3 : int;
}
Is it possible to create a function that changes only this named value (p1) and returns a new record?
I tried and got this far:
type IType =
abstract member p1: int;
type Type1 =
{
p1: int;
p2: int;
}
interface IType with
member this.p1 = this.p1
type Type2 =
{
p1 : int;
p3 : int;
}
interface IType with
member this.p1 = this.p1
let changeP1ToTen (value: 'a when 'a :> IType) =
let newValue = {value with p1 = 10}
newValue
let type1 =
{
p1 = 50
p2 = 80
}
let newType1 =
changeP1ToTen(type1)
This does not work, since the compiler assumes that the {value with p1 = 10} is Type2, when it can be.
If there is a better and clever solution, this will also help.
I know this is possible if I use mutable for my types or use a class instead of a simple notation, but I was wondering if there is a better way to deal with it and not with the OO approach.
source
share