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

Direct cast to a polymorphic trait type is not supported directly #3635

Closed
ahmadsalim opened this issue Oct 1, 2012 · 4 comments
Closed

Comments

@ahmadsalim
Copy link

It seems that one is not able to cast to a polymorphic type directly e.g.

extern mod std;
fn main () {
  let table : std::map::HashMap<int, int> = std::map::HashMap::<int, int>();
  (table as std::map::Map<int,int>).insert(123, 1233);
  io::println(table.get(123).to_str());
}

does not work, but

extern mod std;
fn main () {
  let table : std::map::HashMap<int, int> = std::map::HashMap::<int, int>();
  (table as @std::map::Map<int,int>).insert(123, 1233);
  io::println(table.get(123).to_str());
}

does.

It might be a documentation bug if one is only allowed to cast to a trait pointer type (as it is shown in the tutorial that one can cast to Drawable trait directly), or it might be an implementation bug regarding traits with type parameters.

@brson
Copy link
Contributor

brson commented Oct 2, 2012

This is more-or-less working as intended, although there are further changes coming to trait types. Previously, traits as types were implicitly boxed, but not written with @. The new, mostly unimplemented design, is that you can only cast between pointer types, so @table as @Map, ~table as ~Map, &table as &Map.

The current functionality is still very limited, and not often used.

@brson brson closed this as completed Oct 2, 2012
@brson
Copy link
Contributor

brson commented Oct 2, 2012

Oh, it isn't possible to directly cast to a value-type trait because they don't have a fixed size (similar to how it isn't possible to use a plain str type without a pointer).

@ahmadsalim
Copy link
Author

Hmm, I still believe that section "15.6 Casting to an interface type" on the tutorial should be revised to reflect this (in the future, when it is working anyways).

@brson
Copy link
Contributor

brson commented Oct 2, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants