Init-only & required properties support #695
Replies: 2 comments 6 replies
-
Assuming C# required properties works with the "initializer" syntax (I gather that is the point of the feature); there will be need for F# to distinguish the property setter that are accepted into any method call, versus those that are for a constructor call: public class WithRequired {
public required int Foo {get; set;}
public required int Bar {get; init;}
}
var ok = new WithRequired {Foo=42, Bar =42} type WithRequiredFactory =
static member MakeDefault() = WithRequired(Foo=42, Bar=42)
static member MakeWithFoo(foo) = WithRequired(Foo=foo, Bar=42)
let notOk = WithRequiredFactory.MakeDefault(Bar=42) // can't set because `Bar` is init-only
let notOk2 = WithRequiredFactory.MakeWithFoo(1, Bar=42) // can't set because `Bar` is init-only
let ok = WithRequiredFactory.MakeDefault(Foo=43) // an extra call to Foo setter is appended before returning the expression
let ok2 = WithRequired(Foo=42, Bar=42) This may have weird interaction with Initially, I always seen initializer syntax in C# as "syntax sugar" only, but now it seems to evolve into "checks on the caller/initializer". |
Beta Was this translation helpful? Give feedback.
-
Hey, is it possible to annotate an F#'s property value with this required modifier? Similar to that C# snippet mentioned at this discussion:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is initialising only, the required discussion about F# compiler to honour C# types that bear
init
andrequired
modifiers on properties.Beta Was this translation helpful? Give feedback.
All reactions