Skip to content

Conform TimeAmount to AdditiveArithmetic #1691

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

Merged
merged 11 commits into from
Nov 17, 2020
Merged

Conform TimeAmount to AdditiveArithmetic #1691

merged 11 commits into from
Nov 17, 2020

Conversation

joshrutkowski
Copy link
Contributor

Conforms TimeAmount to AdditiveArithmetic, resolves #1690

Motivation:
TimeAmount does not support -=, +=. Sometimes it is useful to manipulate time amounts when building up a delay and if that iteration fails, we would want to delay += .milliseconds(5) to add 5 milliseconds to our delay and try again.

Modifications:
Conformed TimeAmount to AdditiveArithmetic: added a static zero property and required operators.

Result:
TimeAmount conforms to AdditiveArithmetic.

Conforms TimeAmount to AdditiveArithmetic.

Motivation:
TimeAmount does not support -=, +=. Sometimes it is useful to manipulate time amounts when building up a delay and if that iteration fails, we would want to delay += .milliseconds(5) to add 5 milliseconds to our delay and try again.

Modifications:
Conformed TimeAmount to AdditiveArithmetic: added a static zero property and required operators.

Result:
TimeAmount conforms to AdditiveArithmetic.
@swift-server-bot
Copy link

Can one of the admins verify this patch?

10 similar comments
@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

Generated hooks to run tests on Linux
Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! You’ve added a pair of functions that don’t exist on additive arithmetic and that behave a bit unlike the usual behaviour of += and -=. I think it’d probably be best to remove those two.

Josh added 2 commits November 16, 2020 12:30
Removed duplication of -= and += functions already present in AdditiveArithmetic. Moved declaring zero property to extension.
@joshrutkowski
Copy link
Contributor Author

Thanks for this! You’ve added a pair of functions that don’t exist on additive arithmetic and that behave a bit unlike the usual behaviour of += and -=. I think it’d probably be best to remove those two.

Removed the functions as suggested to preserve usual behavior of += and -=.

@@ -353,10 +359,18 @@ extension TimeAmount {
public static func + (lhs: TimeAmount, rhs: TimeAmount) -> TimeAmount {
return TimeAmount(lhs.nanoseconds + rhs.nanoseconds)
}

public static func += (lhs: inout TimeAmount, rhs: TimeAmount) -> TimeAmount {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this is my fault, I wasn't clear enough. The signature is: public static func += (lhs: inout TimeAmount, rhs: TimeAmount). The function is supposed to assign the result into the lhs argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this function be required, given that we are conforming to AdditiveArithmetic? Removed in later commit, and passes with test:

func testTimeAmountDoesAddTime() {
        var lhs = TimeAmount.milliseconds(0)
        lhs += .milliseconds(5)
        XCTAssertEqual(lhs, .milliseconds(5))
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct that this function isn’t required at all.

Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, this patch looks reasonable to me!

@Lukasa
Copy link
Contributor

Lukasa commented Nov 17, 2020

@swift-nio-bot test this please

@Lukasa
Copy link
Contributor

Lukasa commented Nov 17, 2020

Sadly it looks like the two functions do need to be implemented in older Swift versions to get the conformance. We need static func +=(lhs: inout TimeAmount, rhs: TimeAmount) and static func -=(lhs: inout TimeAmount, rhs: TimeAmount).

Josh added 2 commits November 17, 2020 05:23
Added += and -= functions to support 5.0 and 5.1. Added hooks for tests on Linux.
@Lukasa
Copy link
Contributor

Lukasa commented Nov 17, 2020

@swift-nio-bot test this please

Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice patch!

@Lukasa
Copy link
Contributor

Lukasa commented Nov 17, 2020

@swift-nio-bot test this please

@Lukasa Lukasa added the 🆕 semver/minor Adds new public API. label Nov 17, 2020
@Lukasa Lukasa merged commit 2f9ea47 into apple:main Nov 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆕 semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TimeAmount does not support +=, -=, etc...
3 participants