Skip to content

Expanding implicit casting #4879

@alexcrichton

Description

@alexcrichton

I've noticed that things like ~, @, &~ and such can all be casted to an &-pointer automatically when dealing with the type of self in a function call. These types of casts don't happen for things like function parameters, however.

Is there a reason for these implicit casts to not work in other use cases and only work in the method-call self use case? An example is:

struct Foo { f: int }

impl Foo {
  fn bar(&self) {}
}

fn foo(_: &Foo) {
}

fn main() {
  let s : &~Foo = &~Foo{ f: 2 };
  s.bar(); // ok, cast to '&Foo' happens
  foo(s);  // error: mismatched types: expected `&Foo` but found `&~Foo`
}

Personally, I think that this would make code more readable in that you wouldn't need as many explicit * operators all over the place. This mainly comes up when there's an owned pointer in an enum variant, and I'm just iterating over the enum to do some processing. I have a function which takes &Enum, and then each variant captures via ref var, creating &~ pointers which then must be recursed on with *var.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions