Allow type of an explicitly specified record property to differ from the corresponding primary constructor parameter #5999
Unanswered
tmat
asked this question in
Language Ideas
Replies: 1 comment 6 replies
-
I think we should enumerate the set of synthesized record members and rationalize what happens when the primary constructor type and the related property type are different in each case. Perhaps we would treat the primary constructor parameter as just being specific to construction, and use the property type in all other cases. |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Currently, an optional parameter of a primary constructor of type
T
can only have default valuedefault(T)
, but that's sometimes not the default value one might want to use. In a non-record type one could write code like so:This allows the call site of the constructor of
S
to omit argument forp
and the reader ofS.P
then gets the customized default value.With records though the equivalent code is considered invalid:
The above example could however compile into following:
When matching primary constructor parameters to explicitly specified properties/fields in records the compiler would match by name only, ignoring the type of the property/field/parameter.
In general, the type of the parameter
T1
might be entirely unrelated to the typeT2
of the corresponding property/field. A valid conversion between these types must be performed in the explicitly specified initializer of the property/field.Beta Was this translation helpful? Give feedback.
All reactions