-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type-directed constructor resolution #1349
Comments
Likely covered by #969 |
This won't make it more readable for sure, code will quickly be polluted with
Definitely doesn't look great. |
As with many other features, it's useful specifically in some scenarios. This feature is useful in scenarios where you operate a bunch of DTO classes that don't have anything but empty constructor and bunch of properties, so there is no difference in readability between proposed syntax and regular records creation: compare let a = new ( X = 1, Y = 2 )
let b = { X = 1; Y = 2 } |
If objects have non-null properties by default (they're initialized at construction time) code from example can be translated to following C#. It doesn't override properties, instead it updates existing values. Requires DTOs to be mutable var x = new MyObj
{
To =
{
FiId =
{
FinInstnId = { BicFi = "smth" }
}
}
} Mentioned #969 discovers how to do it in F#. In my opinion C#'s target-typed-new was a mistake. Language wouldn't need them if language had better type inference (at least holes in constructors are supported |
@jl0pd Thanks! didn't know that you can even skip UPDATE: I've figured out that without new() C# version won't work, it will throw NRE |
It doesn't throw if property is initialized. Test1 will configure existing |
The correct issue that this issue duplicates is not #969 but just one away: |
Duplicate of #970 |
True, thanks. I closed it as dupe, however example in the OP might be solved it the one I linked, I think. |
I propose we add type-directed resolutions for calling constructors for known types.
Real world example:
would become
MyObj type is used for XML serialization so inner type names are not important, only properties are used
This is especially useful when using C# apis from autogenerated classes with long names where property name often corresponds it's type name. This also corresponds to C# feature and several other F# suggestions for type-directed collections, tuples and dictionaries.
The existing way of approaching this problem in F# is just to use full type names
Pros and Cons
The advantages of making this adjustment to F# are : enabling convenient using of C#-centrific API, also knowing that such APIs will be convenient to use from F#, improving conciseness, parity with other type-directed features
The disadvantages of making this adjustment to F# are : more type-inference work for compiler, less explicit code
Extra information
Estimated cost (XS, S, M, L, XL, XXL): not sure, probably M
Related suggestions: #1086, #49, #988
Affidavit (please submit!)
Please tick these items by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: