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

Unary minus operator overloading #780

Closed
davidhesselbom opened this Issue Jul 29, 2014 · 1 comment

Comments

Projects
None yet
2 participants
@davidhesselbom
Contributor

davidhesselbom commented Jul 29, 2014

I'm having trouble with unary minus operator overloads, especially when overloading both the binary and the unary minus operator. I should be able to build PointD, but for some reason I can't.

import math

// Builds, but does not use the binary minus operator
PointA: cover {
    x, y: Float
    init: func@ (=x, =y)
    // difference: func (other: This) -> This { this - other }
    operator + (other: This) -> This { This new(this x + other x, this y + other y) }
    operator - (other: This) -> This { This new(this x - other x, this y - other y) }
    operator - -> This { This new(-this x, -this y) }
}

// Builds, but implements only the binary minus operator, not the unary one
PointB: cover {
    x, y: Float
    init: func@ (=x, =y)
    difference: func (other: This) -> This { this - other }
    operator + (other: This) -> This { This new(this x + other x, this y + other y) }
    operator - (other: This) -> This { This new(this x - other x, this y - other y) }
    // operator - -> This { This new(-this x, -this y) }
}

// Builds, but does not implement binary minus operator, only the unary one
PointC: cover {
    x, y: Float
    init: func@ (=x, =y)
    difference: func (other: This) -> This { this + (-other) }
    operator + (other: This) -> This { This new(this x + other x, this y + other y) }
    // operator - (other: This) -> This { This new(this x - other x, this y - other y) }
    operator - -> This { This new(-this x, -this y) }
}

// Does NOT build, "error Argl, you need 2 arguments to override the '-' operator, not 1"
PointD: cover {
    x, y: Float
    init: func@ (=x, =y)
    difference: func (other: This) -> This { this - other }
    operator + (other: This) -> This { This new(this x + other x, this y + other y) }
    operator - (other: This) -> This { This new(this x - other x, this y - other y) }
    operator - -> This { This new(-this x, -this y) }
}

// Does NOT build, even though we're actually just subtracting a Float from a Float
PointE: cover {
    x, y: Float
    init: func@ (=x, =y)
    difference: func (other: This) -> This { this - other }
    operator + (other: This) -> This { This new(this x + other x, this y + other y) }
    operator - (other: This) -> This { This new(this x - (other x), this y - (other y)) }
    operator - -> This { This new(-this x, -this y) }
}

// Builds, but requires use of the unary minus operator, cannot use the binary one
PointF: cover {
    x, y: Float
    init: func@ (=x, =y)
    difference: func (other: This) -> This { this + (- other) }
    operator + (other: This) -> This { This new(this x + other x, this y + other y) }
    operator - (other: This) -> This { This new(this x - other x, this y - other y) }
    operator - -> This { This new(-this x, -this y) }
}

// Builds, but requires the binary minus operator to use the unary one
PointG: cover {
    x, y: Float
    init: func@ (=x, =y)
    difference: func (other: This) -> This { this + (- other) }
    operator + (other: This) -> This { This new(this x + other x, this y + other y) }
    operator - (other: This) -> This { This new(this x + (- other x), this y + (- other y)) }
    operator - -> This { This new(-this x, -this y) }
}

// Builds, but requires explicit casting to Float
PointH: cover {
    x, y: Float
    init: func@ (=x, =y)
    difference: func (other: This) -> This { this + (- other) }
    operator + (other: This) -> This { This new(this x + other x, this y + other y) }
    operator - (other: This) -> This { This new(this x - other x as Float, this y - other y as Float) }
    operator - -> This { This new(-this x, -this y) }
}
@fasterthanlime

This comment has been minimized.

Collaborator

fasterthanlime commented Aug 15, 2014

Full testcase:

// Builds, but requires use of the unary minus operator, cannot use the binary one
Point: cover {
    x, y: Float
    init: func@ (=x, =y)
    difference: func (other: This) -> This { this - other }
    operator + (other: This) -> This { This new(this x + other x, this y + other y) }
    operator - (other: This) -> This { This new(this x - other x, this y - other y) }
    operator - -> This { This new(-this x, -this y) }
    _: String { get { toString() } }
    toString: func -> String { "(#{x}, #{y})" }
}

main: func {
    p1 := Point new(1, 2)
    p2 := Point new(2, 4)
    "p1 = #{p1 _}" println()
    "p2 = #{p2 _}" println()
    "-p1 = #{(-p1) _}" println()
    "p1 + p2 = #{(p1 + p2) _}" println()
    "p1 - p2 = #{(p1 - p2) _}" println()
    "p1 difference(p2) = #{(p1 difference(p2)) _}" println()
}

Correct output:

p1 = (1.000000, 2.000000)
p2 = (2.000000, 4.000000)
-p1 = (-1.000000, -2.000000)
p1 + p2 = (3.000000, 6.000000)
p1 - p2 = (-1.000000, -2.000000)
p1 difference(p2) = (-1.000000, -2.000000)

fasterthanlime added a commit that referenced this issue Aug 15, 2014

@fasterthanlime fasterthanlime added this to the 0.9.9 milestone Aug 15, 2014

@fasterthanlime fasterthanlime self-assigned this Aug 15, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment