www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Function, signatures and tuples

reply Russel Winder <russel russel.org.uk> writes:
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Is there an easy explanation of why I cannot do:

        real partialSum ( immutable Tuple ! ( long , long , real )
        data )

but instead have to do:

        real partialSum ( T ... ) ( T data )

to be frank I really don't get this latter which is what compiles -- I
have no idea if it works yet there are other problems with my code.
Specifically, how do I create an array of tuples?  TDPL has little on
tuples (p.163 basically) and the online manual has little as well.  I
tried:

        auto inputData =3D Tuple ! ( long , long , real )
        [ numberOfThreads ] ;

but this errors out with:

        pi_d2_parallelMap.d(26): Error: template instance template 'Tuple' =
is not defined
        pi_d2_parallelMap.d(26): Error: Tuple!(long,long,real) must be an a=
rray or pointer type, not void

what is worse trying to set values in the array:

        foreach ( i ; 0 .. numberOfThreads ) { inputData[i] =3D tuple ( 1 +=
 i * sliceSize , ( i + 1 ) * sliceSize , delta ) ; }

I get the error:

        pi_d2_parallelMap.d(27): Error: undefined identifier tuple

Please tell me I am being stupid and that I should read some specific
reference of TFM :-)

Thanks.

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel russel.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
Nov 12 2010
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 11/12/10 11:13 AM, Russel Winder wrote:
 Is there an easy explanation of why I cannot do:

          real partialSum ( immutable Tuple ! ( long , long , real )
          data )

 but instead have to do:

          real partialSum ( T ... ) ( T data )

 to be frank I really don't get this latter which is what compiles -- I
 have no idea if it works yet there are other problems with my code.
 Specifically, how do I create an array of tuples?

Doesn't this help? auto tuples = new Tuple!(long, long, real)[128]; Tuple is a type like any other. Andrei
Nov 12 2010
parent reply Iain Buclaw <ibuclaw ubuntu.com> writes:
== Quote from Russel Winder (russel russel.org.uk)'s article
 On Fri, 2010-11-12 at 11:15 -0800, Andrei Alexandrescu wrote:
 On 11/12/10 11:13 AM, Russel Winder wrote:
 Is there an easy explanation of why I cannot do:

          real partialSum ( immutable Tuple ! ( long , long , real ) data )

 but instead have to do:

          real partialSum ( T ... ) ( T data )

 to be frank I really don't get this latter which is what compiles -- I
 have no idea if it works yet there are other problems with my code.
 Specifically, how do I create an array of tuples?

Doesn't this help? auto tuples = new Tuple!(long, long, real)[128]; Tuple is a type like any other.

auto inputData = new Tuple ! ( long , long , real ) [ numberOfThreads ] ; causes the error message: pi_d2_parallelMap.d(30): Error: template instance template 'Tuple' = is not defined pi_d2_parallelMap.d(30): Error: Tuple!(long,long,real) is used as a type Clearly code you think is right and that is expected to work on D 2.050 isn't working for me :-( So either I guess there is a bug in D or, much more likely, I am doing something wrong. -- Russel.

import std.typecons; ?
Nov 13 2010
parent =?UTF-8?B?UGVsbGUgTcOlbnNzb24=?= <pelle.mansson gmail.com> writes:
On 11/13/2010 11:43 AM, Russel Winder wrote:
 On Sat, 2010-11-13 at 08:18 +0000, Iain Buclaw wrote:
 [ . . . ]
 import std.typecons; ?

Hummm... I thought I had put that in but clearly I had not :-(( OK so that explains the bulk of the problems on this code, I knew it was something stupid on my part, thanks for spotting it. However, now we may be getting to something more serious. The line: foreach ( i ; 0 .. numberOfTasks ) { inputData[i] = tuple ( 1 + i * sliceSize , ( i + 1 ) * sliceSize , delta ) ; } now results in the error: /home/users/russel/lib.Linux.x86_64/DMD2/bin/../../src/phobos/ td/typecons.d(662): Error: can only initialize const member _field_field_2 inside constructor /home/users/russel/lib.Linux.x86_64/DMD2/bin/../../src/phobos std/typecons.d(26): Error: template instance std.typecons.tuple!(long,long,immutable(double)) error instantiating Which at first sight seems to indicate an error in the typecons package of Phobos. On the other hand, it is probably more reasonable to assume I still have something stupid wrong in my code.

It's not your code, you can work around it with cast(double)delta, or using tuple!(long,long,double) explicitly, I think. Tuple doesn't handle immutable or const really well, yet.
Nov 16 2010
prev sibling next sibling parent Russel Winder <russel russel.org.uk> writes:
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Fri, 2010-11-12 at 11:15 -0800, Andrei Alexandrescu wrote:
 On 11/12/10 11:13 AM, Russel Winder wrote:
 Is there an easy explanation of why I cannot do:

          real partialSum ( immutable Tuple ! ( long , long , real )
          data )

 but instead have to do:

          real partialSum ( T ... ) ( T data )

 to be frank I really don't get this latter which is what compiles -- I
 have no idea if it works yet there are other problems with my code.
 Specifically, how do I create an array of tuples?

Doesn't this help? =20 auto tuples =3D new Tuple!(long, long, real)[128]; =20 Tuple is a type like any other.

I noted in a separate email that: auto inputData =3D new Tuple ! ( long , long , real ) [ numberOfThreads ] ; causes the error message: pi_d2_parallelMap.d(30): Error: template instance template 'Tuple' = is not defined pi_d2_parallelMap.d(30): Error: Tuple!(long,long,real) is used as a= type =20 Clearly code you think is right and that is expected to work on D 2.050 isn't working for me :-( So either I guess there is a bug in D or, much more likely, I am doing something wrong. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel russel.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Nov 13 2010
prev sibling parent Russel Winder <russel russel.org.uk> writes:
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Sat, 2010-11-13 at 08:18 +0000, Iain Buclaw wrote:
[ . . . ]
 import std.typecons; ?

Hummm... I thought I had put that in but clearly I had not :-(( OK so that explains the bulk of the problems on this code, I knew it was something stupid on my part, thanks for spotting it. However, now we may be getting to something more serious. The line: foreach ( i ; 0 .. numberOfTasks ) { inputData[i] =3D tuple ( 1 + = i * sliceSize , ( i + 1 ) * sliceSize , delta ) ; } now results in the error: /home/users/russel/lib.Linux.x86_64/DMD2/bin/../../src/phobos/std/t= ypecons.d(662): Error: can only initialize const member _field_field_2 insi= de constructor /home/users/russel/lib.Linux.x86_64/DMD2/bin/../../src/phobos/std/t= ypecons.d(26): Error: template instance std.typecons.tuple!(long,long,immut= able(double)) error instantiating Which at first sight seems to indicate an error in the typecons package of Phobos. On the other hand, it is probably more reasonable to assume I still have something stupid wrong in my code. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel russel.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Nov 13 2010