dotnet-websharper / core

WebSharper - Full-stack, functional, reactive web apps and microservices in F# and C#
https://websharper.com
Apache License 2.0
598 stars 50 forks source link

`Seq.sum` returns incorrect result for types that define the `get_Zero` operator #1284

Closed cata closed 1 year ago

cata commented 2 years ago

Please see this Try WebSharper snippet for the repro.

Jand42 commented 2 years ago

@cata Thanks, indeed Seq.sum and some others probably just have a single implementation for the default + operator. I have fixed the related issue #1283 first, as it's needed to process resolving generic operators, looking into this next.

cata commented 2 years ago

@Jand42 - awesome, thank you!

Jand42 commented 2 years ago

These are the Seq/List/Array functions that I would upgrade for better generic handling: sum, sumBy, average, averageBy.

Functions using comparisons (sorts) are already working well enough as the actual comparison is handled by a helper that checks for all cases. Similar idea could be implemented for required operations here. Possibly with fallback to a flat simple implementation with direct operator uses, for runtime performance.

Jand42 commented 1 year ago

@cata This is in GH build 6.0.5.244 right now.

Primitive numeral types still default to a function call that uses plain JS operators inside, but any other type compile to an inline that uses the type's defined get_Zero and op_Addition members. (And also DivideByInt for average)

Breaking change: you should now use sum, sumBy, average, averageBy functions with resolved generics, or annotate with [<Inline>]

cata commented 1 year ago

@Jand42 - super, you guys are awesome!