Change syntax for impl

Move the name of the bundle to the front, allow type parameters (not
handled yet), and add a 'for' keyword:

    impl utils for int {
        fn str() -> str { int::str(self) }
        fn times(f: block()) { ... }
    }
This commit is contained in:
Marijn Haverbeke 2011-12-16 10:42:28 +01:00
parent 4f826d81f6
commit cff6bdd036
6 changed files with 34 additions and 20 deletions

View File

@ -505,7 +505,7 @@ tag item_ {
node_id /* dtor id */,
[ty_param],
node_id /* ctor id */);
item_impl(@path /* iface */, @ty /* self */, [@method]);
item_impl([ty_param], @ty /* self */, [@method]);
}
type native_item =

View File

@ -235,8 +235,8 @@ fn noop_fold_item_underscore(i: item_, fld: ast_fold) -> item_ {
methods: vec::map(fld.fold_method, o.methods)},
typms, d)
}
item_impl(iface, ty, methods) {
item_impl(fld.fold_path(iface), fld.fold_ty(ty),
item_impl(tps, ty, methods) {
item_impl(tps, fld.fold_ty(ty),
vec::map(fld.fold_method, methods))
}
item_res(dtor, did, typms, cid) {

View File

@ -1859,14 +1859,14 @@ fn parse_item_obj(p: parser, attrs: [ast::attribute]) -> @ast::item {
}
fn parse_item_impl(p: parser, attrs: [ast::attribute]) -> @ast::item {
let lo = p.get_last_lo_pos(), ty = parse_ty(p, false);
expect(p, token::COLON);
let path = parse_path(p), meths = [];
let lo = p.get_last_lo_pos(), ident = parse_ident(p),
tps = parse_ty_params(p);
expect_word(p, "for");
let ty = parse_ty(p, false), meths = [];
expect(p, token::LBRACE);
while !eat(p, token::RBRACE) { meths += [parse_method(p)]; }
ret mk_item(p, lo, p.get_last_hi_pos(),
path.node.idents[vec::len(path.node.idents) - 1u],
ast::item_impl(path, ty, meths), attrs);
ret mk_item(p, lo, p.get_last_hi_pos(), ident,
ast::item_impl(tps, ty, meths), attrs);
}
fn parse_item_res(p: parser, attrs: [ast::attribute]) -> @ast::item {

View File

@ -347,12 +347,6 @@ fn print_native_item(s: ps, item: @ast::native_item) {
end(s); // end the outer ibox
}
ast::native_item_fn(decl, typarams) {
print_fn(s, decl, ast::proto_bare, item.ident, typarams,
decl.constraints);
@ -483,11 +477,14 @@ fn print_item(s: ps, &&item: @ast::item) {
}
bclose(s, item.span);
}
ast::item_impl(path, ty, methods) {
ast::item_impl(tps, ty, methods) {
head(s, "impl");
word(s.s, item.ident);
print_type_params(s, tps);
nbsp(s);
word_nbsp(s, "for");
print_type(s, ty);
word_space(s, ":");
print_path(s, path, false);
space(s.s);
bopen(s);
for meth in methods {
hardbreak_if_not_bol(s);

View File

@ -105,8 +105,7 @@ fn visit_item<E>(i: @item, e: E, v: vt<E>) {
e, v);
}
}
item_impl(path, ty, methods) {
visit_path(path, e, v);
item_impl(_, ty, methods) {
visit_ty(ty, e, v);
for m in methods {
v.visit_fn(m.node.meth, [], m.span, some(m.node.ident), m.node.id,

View File

@ -0,0 +1,18 @@
import a::*;
import b::baz;
mod a {
impl foo for uint { fn plus() -> int { self as int + 20 } }
}
mod b {
impl baz for str { fn plus() -> int { 200 } }
}
fn main() {
impl foo for int { fn plus() -> int { self + 10 } }
assert 10.plus() == 20;
assert 10u.plus() == 30;
assert "hi".plus() == 200;
}