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

proposal: Go 2: spec: allow unary + on string #27657

Closed
deanveloper opened this issue Sep 13, 2018 · 4 comments
Closed

proposal: Go 2: spec: allow unary + on string #27657

deanveloper opened this issue Sep 13, 2018 · 4 comments
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@deanveloper
Copy link

deanveloper commented Sep 13, 2018

Problem

Especially with generics coming up where operators become important, the following contracts are different, even though they may look like they are the same:

contract UnaryAdd(a A) {
    +a
}
contract BinaryAdd(a A) {
    a+a
}

Note that the following contracts are currently equivalent:

contract UnarySub(a A) {
    -a
}
contract BinarySub(a A) {
    a-a
}

If someone wants an obvious "I want to allow numeric addition and not concatenation", they can much more explicitly say it like so:

contract NumericAdd(a A) {
    0 + a
}

Solution

This is what the following is said by the spec:

For integer operands, the unary operators +, -, and ^ are defined as follows:

+x                          is 0 + x
-x    negation              is 0 - x
^x    bitwise complement    is m ^ x  with m = "all bits set to 1" for unsigned x
                                      and  m = -1 for signed x

I propose it be changed to something similar to the following:

For all operands, the unary operators +, -, and ^ are defined as follows:

+x                          is z + x
-x    negation              is z - x
^x    bitwise complement    is m ^ x

where z is the zero value for the type of x
and m is "all bits set to 1" for unsigned x and -1 for signed x

Impact

This would have an extremely small impact on programming in Go, but in contract-writing this could hide away one of those "weird cases". It prevents a library writer from seeing this case and using +a to mean "numeric addition only" in a contract, which may not be immediately obvious to a reader.

This also means that in the future of Go, if more types (ie matrices) are added, if binary operators defined on them, the unary operators will also be defined on them. Even though this is unlikely, I think it's a good thing to add.

This is also a very small change, and should be Go 1 compatible. This needs to have a decision either with or before Go 2 though, because of contract equivalence.

@gopherbot gopherbot added this to the Proposal milestone Sep 13, 2018
@mvdan mvdan changed the title proposal: allow unary + on string proposal: spec: allow unary + on string Sep 13, 2018
@mvdan mvdan added LanguageChange v2 A language change or incompatible library change labels Sep 13, 2018
@mvdan
Copy link
Member

mvdan commented Sep 13, 2018

/cc @griesemer @ianlancetaylor

@mvdan
Copy link
Member

mvdan commented Sep 13, 2018

I realise that you've said this should happen along with or before contracts, but we default to placing all language changes into Go2. Remember that generics is also part of the broader Go2, even if it ends up being just Go 1.19 or whichever incremental version.

@griesemer
Copy link
Contributor

This seems like a fine idea to finesse this specific problem with contracts. But we should wait and see what the final contracts design is going to be and whether we actually need this.

@ianlancetaylor ianlancetaylor changed the title proposal: spec: allow unary + on string proposal: Go 2: spec: allow unary + on string Sep 13, 2018
@ianlancetaylor
Copy link
Contributor

Thanks for the suggestion. Let's close this specific issue in favor of the general discussion about generics and contracts.

@golang golang locked and limited conversation to collaborators Oct 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

5 participants