digitalmars.D.learn - Passing ranges around
- Bahman Movaqar (18/18) Jul 11 2016 What should be signature of `foo` in the following piece of code?
- Mike Parker (2/10) Jul 11 2016 auto foo(R)(R r) { ... }
- Bahman Movaqar (6/7) Jul 12 2016 That did it. Thanks.
- Mike Parker (4/10) Jul 12 2016 It's just a function template. It's what you use whenever you
- H. S. Teoh via Digitalmars-d-learn (9/23) Jul 12 2016 Better yet:
What should be signature of `foo` in the following piece of code? auto foo(range r) { // do something with the `r` } void main() { foo([1,2,3].map!(x => x*x)); } Right now I use `.array` to convert the range before passing: auto foo(int[] r) { // do something with the `r` } void main() { foo([1,2,3].map!(x => x*x).array); } But it doesn't feel right. Thanks, -- Bahman
Jul 11 2016
On Tuesday, 12 July 2016 at 03:57:09 UTC, Bahman Movaqar wrote:What should be signature of `foo` in the following piece of code? auto foo(range r) { // do something with the `r` } void main() { foo([1,2,3].map!(x => x*x)); }auto foo(R)(R r) { ... }
Jul 11 2016
On 07/12/2016 11:07 AM, Mike Parker wrote:auto foo(R)(R r) { ... }That did it. Thanks. Out of curiosity, does the same pattern apply to functions which take `tuple`s as input arguments? -- Bahman
Jul 12 2016
On Tuesday, 12 July 2016 at 07:50:34 UTC, Bahman Movaqar wrote:On 07/12/2016 11:07 AM, Mike Parker wrote:It's just a function template. It's what you use whenever you need to deal with generic types. https://dlang.org/spec/template.htmlauto foo(R)(R r) { ... }That did it. Thanks. Out of curiosity, does the same pattern apply to functions which take `tuple`s as input arguments?
Jul 12 2016
On Tue, Jul 12, 2016 at 06:37:35AM +0000, Mike Parker via Digitalmars-d-learn wrote:On Tuesday, 12 July 2016 at 03:57:09 UTC, Bahman Movaqar wrote:Better yet: import std.range.primitives; auto foo(R)(R r) if (isInputRange!R) { ... } This is to ensure R is actually a range, so that, for example, foo(123) will be rejected at compile-time. T -- "A man's wife has more power over him than the state has." -- Ralph EmersonWhat should be signature of `foo` in the following piece of code? auto foo(range r) { // do something with the `r` } void main() { foo([1,2,3].map!(x => x*x)); }auto foo(R)(R r) { ... }
Jul 12 2016