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

Igor Bukanov igor at mir2.org
Wed Jun 4 00:58:52 PDT 2014


Thanks, Test::<bool>::new() works indeed.

On 4 June 2014 09:52, Sebastian Gesemann <s.gesemann at gmail.com> wrote:
> On Wed, Jun 4, 2014 at 9:28 AM, Igor Bukanov 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();
>>                     ^~~~
>
> Not sure if it helps because I can't test it right now and I'm still a
> beginner, but here it goes: Try inserting a :: between Test and
> <bool>. AFAIU this is sometimes needed for disambiguation. Otherwise
> the compiler might think < is a less-than operator.
>
> Cheers!
> sg
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev


More information about the Rust-dev mailing list