diff --git a/src/core/inequality.scala b/src/core/inequality.scala index f636e0e..a8377b8 100644 --- a/src/core/inequality.scala +++ b/src/core/inequality.scala @@ -25,15 +25,19 @@ import language.experimental.captureChecking object NumericallyComparable: inline given numeric: Inequality[Boolean, Int | Double | Char | Byte | Short | Float | Long] with inline def compare - (inline left: Boolean, inline right: Int | Double | Char | Byte | Short | Float | Long, - inline strict: Boolean, inline greaterThan: Boolean) - : Boolean = + ( inline left: Boolean, + inline right: Int | Double | Char | Byte | Short | Float | Long, + inline strict: Boolean, + inline greaterThan: Boolean ) + : Boolean = + ${Hypotenuse2.inequality('left, 'right, 'strict, 'greaterThan)} given inequality: Inequality[ByteSize, ByteSize] with inline def compare (inline left: ByteSize, inline right: ByteSize, inline strict: Boolean, inline greaterThan: Boolean) - : Boolean = + : Boolean = + !strict && left.long == right.long || (left.long < right.long) ^ greaterThan trait NumericallyComparable @@ -52,13 +56,15 @@ trait CompareGreaterEqual[-LeftType, -RightType, +ResultType] extends Numericall trait Inequality[-LeftType, -RightType] -extends CompareGreaterEqual[LeftType, RightType, Boolean], CompareLess[LeftType, RightType, Boolean], - CompareLessEqual[LeftType, RightType, Boolean], CompareGreater[LeftType, RightType, Boolean]: +extends + CompareGreaterEqual[LeftType, RightType, Boolean], + CompareLess[LeftType, RightType, Boolean], + CompareLessEqual[LeftType, RightType, Boolean], + CompareGreater[LeftType, RightType, Boolean]: inline def compare - (inline left: LeftType, inline right: RightType, inline strict: Boolean, - inline greaterThan: Boolean) - : Boolean + (inline left: LeftType, inline right: RightType, inline strict: Boolean, inline greaterThan: Boolean) + : Boolean inline def lessThan(inline left: LeftType, inline right: RightType): Boolean = compare(left, right, true, false) @@ -76,24 +82,28 @@ extension [LeftType](inline left: LeftType) @targetName("lt") inline infix def < [RightType, ResultType](inline right: RightType) (using inline compareLess: CompareLess[LeftType, RightType, ResultType]) - : ResultType = + : ResultType = + compareLess.lessThan(left, right) @targetName("lte") inline infix def <= [RightType, ResultType](inline right: RightType) (using inline compareLessEqual: CompareLessEqual[LeftType, RightType, ResultType]) - : ResultType = + : ResultType = + compareLessEqual.lessThanOrEqual(left, right) @targetName("gt") inline infix def > [RightType, ResultType](inline right: RightType) (using inline compareGreater: CompareGreater[LeftType, RightType, ResultType]) - : ResultType = + : ResultType = + compareGreater.greaterThan(left, right) @targetName("gte") inline infix def >= [RightType, ResultType](inline right: RightType) (using inline compareGreaterEqual: CompareGreaterEqual[LeftType, RightType, ResultType]) - : ResultType = + : ResultType = + compareGreaterEqual.greaterThanOrEqual(left, right) diff --git a/src/core/macros.scala b/src/core/macros.scala index 39dc74f..e5cb683 100644 --- a/src/core/macros.scala +++ b/src/core/macros.scala @@ -84,10 +84,12 @@ object Hypotenuse2: Expr(int.toByte) def inequality - (expr: Expr[Boolean], bound: Expr[Int | Double | Char | Byte | Short | Long | Float], - strict: Expr[Boolean], greaterThan: Expr[Boolean]) + ( expr: Expr[Boolean], + bound: Expr[Int | Double | Char | Byte | Short | Long | Float], + strict: Expr[Boolean], + greaterThan: Expr[Boolean] ) (using Quotes) - : Expr[Boolean] = + : Expr[Boolean] = val errorMessage = msg"this cannot be written as a range expression" @@ -108,6 +110,7 @@ object Hypotenuse2: case '{($bound: Float) > ($value: Float)} => value case '{($bound: Float) >= ($value: Float)} => value case _ => fail(errorMessage) + else expr match case '{($bound: Int) < ($value: Int)} => value case '{($bound: Int) <= ($value: Int)} => value