Init-only & required properties support #695
smoothdeveloper
started this conversation in
Language and core library RFC discussions
Replies: 1 comment 4 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.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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