Skip to content
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

+ DateOnly and TimeOnly as Monoid #550

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/FSharpPlus/Control/Monoid.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
static member ``+`` (x: array<_> , y , [<Optional>]_mthd: Plus ) = Array.append x y
static member ``+`` (() , () , [<Optional>]_mthd: Plus ) = ()
static member ``+`` (x: bool , y: bool , [<Optional>]_mthd: Plus ) = x <> y

#if NET6_0_OR_GREATER
static member ``+`` (x: DateOnly , y: DateOnly , [<Optional>]_mthd: Plus ) = DateOnly.FromDayNumber (x.DayNumber + y.DayNumber)
static member ``+`` (x: TimeOnly , y: TimeOnly , [<Optional>]_mthd: Plus ) = (x.Ticks + y.Ticks) % 864000000000L |> TimeOnly
#endif

static member ``+`` (x: Set<_> , y , [<Optional>]_mthd: Plus ) = Set.union x y

#if !FABLE_COMPILER
Expand All @@ -40,7 +46,7 @@
static member ``+`` (_: Id0 , _: Id0 , [<Optional>]_mthd: Plus ) = Id0 ""
static member ``+`` (x: exn , y: exn , [<Optional>]_mthd: Plus ) =
let f (e: exn) = match e with :? AggregateException as a -> a.Data0 :> seq<_> | _ -> Seq.singleton e
AggregateException (seq {yield! f x; yield! f y}) :> exn

Check warning on line 49 in src/FSharpPlus/Control/Monoid.fs

View workflow job for this annotation

GitHub Actions / testFable3SubsetOnCore

This upcast is unnecessary - the types are identical
#endif

static member inline Invoke (x: 'Plus) (y: 'Plus) : 'Plus =
Expand Down
10 changes: 9 additions & 1 deletion src/FSharpPlus/Control/Numeric.fs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ type FromInt32 =
static member FromInt32 (_: uint64 , _: FromInt32) = fun (x: int32) -> uint64 x
static member FromInt32 (_: float32 , _: FromInt32) = fun (x: int32) -> float32 x
static member FromInt32 (_: decimal , _: FromInt32) = fun (x: int32) -> decimal x

#if NET6_0_OR_GREATER
static member FromInt32 (_: System.DateOnly , _: FromInt32) = fun (x: int32) -> System.DateOnly.FromDayNumber x
#endif

static member inline Invoke (x: int32) : 'Num =
let inline call_2 (a: ^a, b: ^b) = ((^a or ^b) : (static member FromInt32 : _*_ -> _) b, a)
Expand Down Expand Up @@ -133,6 +137,10 @@ type Zero =
inherit Default1

static member Zero (_: System.TimeSpan , _: Zero ) = System.TimeSpan ()
#if NET6_0_OR_GREATER
static member Zero (_: System.DateOnly , _: Zero ) = System.DateOnly.MinValue
static member Zero (_: System.TimeOnly , _: Zero ) = System.TimeOnly.MinValue
#endif
static member Zero (_: DmStruct , _: Zero ) = Unchecked.defaultof<DmStruct>
static member Zero (_: list<'a> , _: Zero ) = [] : list<'a>
static member Zero (_: option<'a> , _: Zero ) = None : option<'a>
Expand Down Expand Up @@ -635,4 +643,4 @@ type MaxValue =
static member inline MaxValue (_: 'a*'b*'c*'d*'e*'f , _: MaxValue) = (MaxValue.Invoke (), MaxValue.Invoke (), MaxValue.Invoke (), MaxValue.Invoke (), MaxValue.Invoke (), MaxValue.Invoke ())
static member inline MaxValue (_: 'a*'b*'c*'d*'e*'f*'g, _: MaxValue) = (MaxValue.Invoke (), MaxValue.Invoke (), MaxValue.Invoke (), MaxValue.Invoke (), MaxValue.Invoke (), MaxValue.Invoke (), MaxValue.Invoke ())

#endif
#endif
2 changes: 1 addition & 1 deletion src/FSharpPlus/FSharpPlus.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<DefineConstants Condition=" '$(Configuration)' == 'Fable'">$(DefineConstants);FABLE_COMPILER</DefineConstants>
<DefineConstants Condition=" '$(Configuration)' == 'Fable3'">$(DefineConstants);FABLE_COMPILER;FABLE_COMPILER_3</DefineConstants>
<DefineConstants Condition=" '$(Configuration)' == 'Fable4'">$(DefineConstants);FABLE_COMPILER;FABLE_COMPILER_4</DefineConstants>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<!--<OutputPath>..\..\bin</OutputPath>-->
</PropertyGroup>
<ItemGroup>
Expand Down
14 changes: 13 additions & 1 deletion tests/FSharpPlus.Tests/Monoid.fs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,16 @@ module Monoid =
let! y = struct ("Ten", 10)
return y - x }
Assert.AreEqual (str, "FourTen")
Assert.AreEqual (num, 6)
Assert.AreEqual (num, 6)

#if NET6_0_OR_GREATER
[<Test>]
let testDateAndTimes =
let d1 = DateOnly(2020, 1, 1)
let d2 = d1 ++ zero ++ one
Assert.AreEqual (DateOnly(2020, 1, 2), d2)

let t1 = TimeOnly (0, 0, 0)
let t2 = zero
Assert.AreEqual (t1, t2)
#endif
Loading