-
Notifications
You must be signed in to change notification settings - Fork 789
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
Anonymous record fields names are sorted #6422
Comments
Ouch, I didn't expect there would be C# libraries requiring a sorted field ordering. But of course there are. Bzzzz. The RFC says sorting is used and it is relevant to the type checking process, see https://github.com/fsharp/fslang-design/blob/master/FSharp-4.6/FS-1030-anonymous-records.md#alternative-do-not-sort-fields-by-name I'm not quite sure what to do. This has to be a feature extension I think. One option would be something like this if we can find a syntax that we want. The other option is to take a breaking change under a language version switch.
|
@dsyme I think it's worth expounding upon this in the RFC:
What is more usable? |
With F# records, order doesn't matter. You just do Regardless of usability, if differently ordered anonymous types are regarded as different incompatible types then that increases the divergence of the feature from F# records. |
what about considering a fix to dapper?
Am Mi., 3. Apr. 2019 um 18:07 Uhr schrieb Don Syme <notifications@github.com
…:
What is more usable?
With F# records, order doesn't matter. You just do { Y=3; X=3 } and the
compiler evaluates in order then permutes to create the object, and at
another creation site you can just do { X=4; Y=4 }. Placing the
assignments in order is, I think, less usable (as it would be for named
arguments).
Regardless of usability, if differently ordered anonymous types are
regarded as different incompatible types then that increases the divergence
of the feature from F# records.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#6422 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AADgNF5o33Q8T5y7XMD5EzWQ4gukgFekks5vdNHPgaJpZM4caE-p>
.
|
I think we could file an issue and see what they think |
Column order is important in SQL, I get why they do this. I really do suspect there is a use case for order-sensitive anonymous records. It's just one of those things that plays both ways |
Most .net and sql stuff is generally reliably on resolving column indices / names in case resultset doesn't come with same order, it really looks like a Dapper shortcoming to me. As the feature work as specified in the RFC, and I believe this is impacting type unification with current implementation details, we should postpone changes for current release and keep what's working now. The anonymous records are really convenient for singleton values and replacing tuples already. In future, maybe this can be relaxed in non breaking way, I like the The first point: var foos = new[]
{
new {a = 1, b = 'c'},
new {b = 'd', a = 2},
};
Do you want F# to behave same as C# on this regard? I actually like that the order doesn't matter, otherwise at construction it is inconsistant with F# record type and would (could?) produce different records.
I'm not sure there is a definitive or non subjective answer, F# anonymous records are more usable than C# anonymous objects, they can get exposed without reflection outside local scope and work with type inference the F# way (+ they allow slight typo in order of fields :D ) |
My bad, it's a new feature and i missed in the RFC part about ordering. It was just strange, the following code seems valid and i didnt understood the why about the F#/C# difference. let res = db.QuerySingle< {| Tid:int; Name:string; X:string; Y:int |} >("select Tid=20, Name='fdsf', X='hfds', Y=15") Now make sense and i prefer the F# behaviour. Yes is dapper who should have checked the parameters names too instead of just signature maybe (i'll open an issue and send a PR). I expected to be something who may show in other libs (or create friction), that's why i opened the issue. Having an unsorted way may be nice to work with column oriented structures without names, but is another RFC, not this one I'll close, make sense by design |
For anybody else coming across this in the context of Dapper: Dapper.FSharp happens to work around this issue of ordering by using reflection on the result type when constructing projection clauses. The resulting ordering of columns in projection clauses then matches the ordering which Dapper uses for decoding results (also based on reflection on the result type). |
workaroundfix query ( put in alphabetical order ) select t.aaa, t.bbb, t.ccc from foo t anonymous record label's order is free (。・ω・。)ノ con.Query<{|ccc:string;bbb:string;aaa:string|}>(sql=sql) |
When i create an anonymous record, the fields get sorted by name.
This cause issues when libraries expect the same order of declaration.
An example in dapper
the error is cryptic too, but explain it doesnt find the constructor with right shape
Repro steps
Use the snippet above.
We can also compare C# and F#
while C#
Expected behavior
The generated IL respect order of declaration
Actual behavior
The generated IL sort the field names
Known workarounds
None
Related information
The text was updated successfully, but these errors were encountered: