最初我认为你可以做到这一点,因为文档 (http://doc.rust-lang.org/rust.html#implementations) 表明你可以:
trait Bar<T> {
fn ex(&self) -> T;
}
struct Foo {
y:f64
}
impl Bar<int> for Foo {
fn ex(&self) -> int {
return self.y.floor() as int;
}
}
impl Bar<uint> for Foo {
fn ex(&self) -> uint {
if (self.y < 0.0) {
return 0u;
}
return self.y.floor() as uint;
}
}
...但似乎不起作用。我收到类似以下错误的错误消息:
error: multiple applicable methods in scope
error: expected Bar<uint>, but found Bar<int> (expected uint but found int)
error: expected Bar<int>, but found Bar<uint> (expected int but found uint)
所以我想也许Foo必须是通用的才能正常工作,因此每个特定的Foo都有它自己的Bar实现:
trait Bar<T> {
fn ex(&self) -> T;
}
struct Foo<T> {
y:f64
}
impl<T> Foo<T> {
fn new<U>(value:f64) -> Foo<U> {
return Foo { y: value } as Foo<U>;
}
}
impl Bar<int> for Foo<int> {
fn ex(&self) -> int {
return self.y.floor() as int;
}
}
impl Bar<uint> for Foo<uint> {
fn ex(&self) -> uint {
if (self.y < 0.0) {
return 0u;
}
return self.y.floor() as uint;
}
}
fn main() {
let z = Foo::new::<int>(100.5);
let q = Foo::new::<uint>(101.5);
let i:int = z.ex();
let j:uint = q.ex();
}
...但我的构造函数似乎无法工作:
x.rs:11:12: 11:38 error: non-scalar cast: `Foo<<generic #1>>` as `Foo<U>`
x.rs:11 return Foo { y: value } as Foo<U>;
^~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
编辑:我还尝试过:
impl<T> Foo<T> {
fn new<U>(value:f64) -> Foo<U> {
let rtn:Foo<U> = Foo { y: value };
return rtn;
}
}
这解决了类型转换错误,但会导致以下结果:
x.rs:32:11: 32:26 error: cannot determine a type for this expression: unconstrained type
x.rs:32 let z = Foo::new::<int>(100.5);
^~~~~~~~~~~~~~~
我不知道那是什么意思。
你如何做到这一点?
T
,就像let a = 1; { let a = 2; }
定义了内部作用域中的新变量a
一样。Foo<()>
的情况可以工作,因为您只有从new<T>() -> Array<T>
中的类型参数,因为您为Foo
的类型参数指定了一个具体类型(()
, 即 unit): 它不再是通用的。-> Array<U>
不会推断返回类型,因为那是错误的,没有理由U
和T
必须相同... - huonimpl<T> Array<T> { fn convert<U>(&self) -> Array<U> { ... } }
,这个实现将创建一个不同类型的数组。但是只是因为你在代码中提到了Array<...>
并不意味着它必须与impl
中的Array<...>
相匹配。 - huon<()>
这个东西只是将()
类型作为类型参数传递,impl Foo<uint> { ... }
也同样“可行”,但几乎肯定不是你真正想要的。) - huon