generics

What's different between “def apply[T](c:T)” and “type T;def apply(c:T)”

本秂侑毒 提交于 2020-11-29 21:11:30
问题 I have this program: object B{ def apply[T](c:T)={} } object C{ type T def apply(c:T)={} } object A extends App{ val d=B{println(1);2} val e=C{println(1);2} } the line val e = C{println(1);2} told me error:Type mismatch,expected C.T,actual:2 so why can't I write type T def apply(c:T) it seems the same as apply[T](c:T) and what's the type of T when I write val d=B{println(1);2} I can write many lines here! because T means generic,so it can be Int,String,user defined class Apple,Orange... and

Enforcing that dependent return type must implement typeclass

吃可爱长大的小学妹 提交于 2020-11-29 10:15:13
问题 I am trying to enforce a rule that the (dependent) return type of a typeclass, must itself implement a typeclass. So when the user implements the IsVec typeclass below, they must also ensure that the return value of the getElem method implements another typeclass ( IsVecElem ). My attempts to make this work look something like this: // A typeclass for an vector element abstract class IsVecElem[A, T: Numeric] { def dataOnly(self: A): T } // A typeclass for a vector abstract class IsVec[A, T:

Enforcing that dependent return type must implement typeclass

巧了我就是萌 提交于 2020-11-29 10:13:43
问题 I am trying to enforce a rule that the (dependent) return type of a typeclass, must itself implement a typeclass. So when the user implements the IsVec typeclass below, they must also ensure that the return value of the getElem method implements another typeclass ( IsVecElem ). My attempts to make this work look something like this: // A typeclass for an vector element abstract class IsVecElem[A, T: Numeric] { def dataOnly(self: A): T } // A typeclass for a vector abstract class IsVec[A, T:

How to overload functions in C?

与世无争的帅哥 提交于 2020-11-29 09:50:27
问题 There are lots of posts on this topic, but all of them are not for a complete beginners in C programming. The thing is that I have three functions: struct model* createModel_empty(void); struct model* createModel_single_value(double value); struct model* createModel_single_value(const double* arr_value); And I want to just use createModel function in my code which will be substituted with the right implementation. From my research over the Internet and specifically stack overflow I understood

How to overload functions in C?

一世执手 提交于 2020-11-29 09:49:45
问题 There are lots of posts on this topic, but all of them are not for a complete beginners in C programming. The thing is that I have three functions: struct model* createModel_empty(void); struct model* createModel_single_value(double value); struct model* createModel_single_value(const double* arr_value); And I want to just use createModel function in my code which will be substituted with the right implementation. From my research over the Internet and specifically stack overflow I understood

How to write a generic function taking any iterator of `u32` or `&u32`?

我怕爱的太早我们不能终老 提交于 2020-11-29 08:22:46
问题 I'm trying to write a function that processes a sequence of integers. fn process_one(n: u32) {} fn process<II>(ii: II) where II: IntoIterator<Item = u32>, { for n in ii { process_one(n); } } I want the client to be able to pass a Vec<u32> without consuming it ( process(&v) ). This function can't be used because <&Vec<u32> as IntoIterator>::Item is &u32 ; I'd have to pass v.iter().cloned() instead, which is annoying. Alternatively, I could make the bound Item = &u32 and use process_one(*n) ,

How to write a generic function taking any iterator of `u32` or `&u32`?

女生的网名这么多〃 提交于 2020-11-29 08:21:11
问题 I'm trying to write a function that processes a sequence of integers. fn process_one(n: u32) {} fn process<II>(ii: II) where II: IntoIterator<Item = u32>, { for n in ii { process_one(n); } } I want the client to be able to pass a Vec<u32> without consuming it ( process(&v) ). This function can't be used because <&Vec<u32> as IntoIterator>::Item is &u32 ; I'd have to pass v.iter().cloned() instead, which is annoying. Alternatively, I could make the bound Item = &u32 and use process_one(*n) ,

How to write a generic function taking any iterator of `u32` or `&u32`?

Deadly 提交于 2020-11-29 08:20:42
问题 I'm trying to write a function that processes a sequence of integers. fn process_one(n: u32) {} fn process<II>(ii: II) where II: IntoIterator<Item = u32>, { for n in ii { process_one(n); } } I want the client to be able to pass a Vec<u32> without consuming it ( process(&v) ). This function can't be used because <&Vec<u32> as IntoIterator>::Item is &u32 ; I'd have to pass v.iter().cloned() instead, which is annoying. Alternatively, I could make the bound Item = &u32 and use process_one(*n) ,

How to write a generic function taking any iterator of `u32` or `&u32`?

泄露秘密 提交于 2020-11-29 08:20:26
问题 I'm trying to write a function that processes a sequence of integers. fn process_one(n: u32) {} fn process<II>(ii: II) where II: IntoIterator<Item = u32>, { for n in ii { process_one(n); } } I want the client to be able to pass a Vec<u32> without consuming it ( process(&v) ). This function can't be used because <&Vec<u32> as IntoIterator>::Item is &u32 ; I'd have to pass v.iter().cloned() instead, which is annoying. Alternatively, I could make the bound Item = &u32 and use process_one(*n) ,

What is the proper way to create a new generic struct?

纵饮孤独 提交于 2020-11-28 07:55:49
问题 I'm trying to make a generic struct that can be initialized to something of type T . It looks like this: pub struct MyStruct<T> { test_field: Option<T>, name: String, age: i32, } impl MyStruct<T> { fn new(new_age: i32, new_name: String) -> MyStruct<T> { MyStruct<T> { test_field: None, age: new_age, name: new_name, } } } This doesn't seem to work. Among other errors, I get: error: chained comparison operators require parentheses --> src/lib.rs:9:17 | 9 | MyStruct<T> { | ^^^^^ | 回答1: I highly