www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is there a common type?

reply =?ISO-8859-1?Q?Christian_K=F6stlin?= <christian.koestlin gmail.com> writes:
for [1, 2, 3] and iota(2, 10)?

thanks in advance

christian
May 15 2012
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, May 15, 2012 at 07:29:38PM +0200, Christian Köstlin wrote:
 for [1, 2, 3] and iota(2, 10)?

What are you trying to accomplish? T -- We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true. -- Robert Wilensk
May 15 2012
parent =?ISO-8859-1?Q?Christian_K=F6stlin?= <christian.koestlin gmail.com> writes:
On 5/15/12 19:44 , H. S. Teoh wrote:
 On Tue, May 15, 2012 at 07:29:38PM +0200, Christian Köstlin wrote:
 for [1, 2, 3] and iota(2, 10)?

What are you trying to accomplish? T

like this: int main(string[] args) { auto h = [1, 2, 3]; if (args.length > 1) { h = iota(1, args[1].to!(int)); } do_something_with(h); return 0; }
May 17 2012
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/15/2012 10:29 AM, Christian Köstlin wrote:
 for [1, 2, 3] and iota(2, 10)?

 thanks in advance

 christian

When it comes to compile-time polymorphism or duck typing, they are both RandomAccessRanges. (Pedantically, [1, 2, 3] is not a range (I think :P), but a container. Although, any slice of it is a RandomAccessRange.) import std.range; import std.stdio; void foo(R)(R r) if (isRandomAccessRange!R) { if (!r.empty) { writeln(r[0]); } } void main() { foo([1, 2, 3]); foo(iota(2, 10)); } When it comes to runtime polymorphism, they can be both RandomAccessFinite!int: import std.range; import std.stdio; void foo(RandomAccessFinite!int r) { if (!r.empty) { writeln(r[0]); } } void main() { RandomAccessFinite!int r; r = inputRangeObject([1, 2, 3]); foo(r); r = inputRangeObject(iota(2, 10)); foo(r); } Ali -- D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
May 15 2012
parent =?UTF-8?B?Q2hyaXN0aWFuIEvDtnN0bGlu?= <christian.koestlin gmail.com> writes:
On 5/15/12 20:14 , Ali Çehreli wrote:
 On 05/15/2012 10:29 AM, Christian Köstlin wrote:
 for [1, 2, 3] and iota(2, 10)?

 thanks in advance

 christian

When it comes to compile-time polymorphism or duck typing, they are both RandomAccessRanges. (Pedantically, [1, 2, 3] is not a range (I think :P), but a container. Although, any slice of it is a RandomAccessRange.) import std.range; import std.stdio; void foo(R)(R r) if (isRandomAccessRange!R) { if (!r.empty) { writeln(r[0]); } } void main() { foo([1, 2, 3]); foo(iota(2, 10)); } When it comes to runtime polymorphism, they can be both RandomAccessFinite!int: import std.range; import std.stdio; void foo(RandomAccessFinite!int r) { if (!r.empty) { writeln(r[0]); } } void main() { RandomAccessFinite!int r; r = inputRangeObject([1, 2, 3]); foo(r); r = inputRangeObject(iota(2, 10)); foo(r); } Ali

regards christian
May 17 2012
prev sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Thu, 17 May 2012 16:28:56 +0200, Christian K=C3=B6stlin  =

<christian.koestlin gmail.com> wrote:

 On 5/15/12 19:44 , H. S. Teoh wrote:
 On Tue, May 15, 2012 at 07:29:38PM +0200, Christian K=C3=B6stlin wrot=


 for [1, 2, 3] and iota(2, 10)?

What are you trying to accomplish? T


 like this:
 int main(string[] args) {
    auto h =3D [1, 2, 3];
    if (args.length > 1) {
      h =3D iota(1, args[1].to!(int));
    }
    do_something_with(h);
    return 0;
 }

In that case, you should probably replace h =3D iota(1, args[1].to!(int)); with h =3D iota(1, args[1].to!(int)).array(); (assuming 2.059) or h =3D array(iota(1, args[1].to!(int)));
May 17 2012