[rust-dev] syntax for explicit generics when calling static method

Tommi rusty.gates at icloud.com
Wed Jun 4 00:52:36 PDT 2014


Apparently this works as well:

let t = Test::<bool>::new().test();
println!("t={}", t);

On 2014-06-04, at 10:50, Tommi <rusty.gates at icloud.com> wrote:

> I don't know if there's a better way, but this at least works:
> 
> let tmp: Test<bool> = Test::new();
> let t = tmp.test();
> println!("t={}", t);
> 
> On 2014-06-04, at 10:28, Igor Bukanov <igor at mir2.org> wrote:
> 
>> What is the syntax for calling a static method of a generic struct
>> while selecting the the generic parameters explicitly? Apparently
>> Struct<Type>::static_method does not work. For example, consider the
>> following program:
>> 
>> #[deriving(Show)]
>> struct Test<T> { i: int }
>> 
>> impl<T> Test<T> {
>>    fn new() -> Test<T> { Test {i: 1} }
>>    fn test(&self) -> int { self.i }
>> }
>> 
>> fn main() {
>>    let t = Test<bool>::new().test();
>>    println!("t={}", t);
>> }
>> 
>> The latest nightly compiler generates:
>> 
>> s.rs:10:13: 10:17 error: `Test` is a structure name, but this
>> expression uses it like a function name
>> s.rs:10     let t = Test<bool>::new().test();
>>                    ^~~~
>> 
>> Note that in this case type inference does not work as removing <bool> gives:
>> 
>> s.rs:10:13: 10:31 error: cannot determine a type for this expression:
>> unconstrained type
>> s.rs:10     let t = Test::new().test();
>>                    ^~~~~~~~~~~~~~~~~~
>> _______________________________________________
>> Rust-dev mailing list
>> Rust-dev at mozilla.org
>> https://mail.mozilla.org/listinfo/rust-dev
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140604/6d62782e/attachment.html>


More information about the Rust-dev mailing list