Use the method name 'unary-' for overloading negation

It's less likely to clash with something than 'neg'.

Issue #1520
This commit is contained in:
Marijn Haverbeke 2012-01-27 10:06:53 +01:00
parent 7b67e2d274
commit b80f0a3c9f
3 changed files with 7 additions and 3 deletions

View File

@ -1903,7 +1903,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
oper_t = structurally_resolved_type(fcx, oper.span, oper_t);
if !(ty::type_is_integral(tcx, oper_t) ||
ty::type_is_fp(tcx, oper_t)) {
oper_t = check_user_unop(fcx, "-", "neg", expr, oper_t);
oper_t = check_user_unop(fcx, "-", "unary-", expr, oper_t);
}
}
}

View File

@ -1829,7 +1829,11 @@ fn parse_method_name(p: parser) -> ast::ident {
token::BINOP(op) { p.bump(); token::binop_to_str(op) }
token::NOT { p.bump(); "!" }
token::LBRACKET { p.bump(); expect(p, token::RBRACKET); "[]" }
_ { parse_value_ident(p) }
_ {
let id = parse_value_ident(p);
if id == "unary" && eat(p, token::BINOP(token::MINUS)) { "unary-" }
else { id }
}
}
}

View File

@ -7,7 +7,7 @@ impl point_ops for point {
fn -(other: point) -> point {
{x: self.x - other.x, y: self.y - other.y}
}
fn neg() -> point {
fn unary-() -> point {
{x: -self.x, y: -self.y}
}
fn [](x: bool) -> int {