www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to expand tuple?

reply "Suliman" <evermind live.ru> writes:
Few questions.

1. In examples tuples are created with keyword auto. Can I create 
them with another keyword. Or auto mean structure of data, that 
have not standard type like (int or string)?

2. How ti expend tuple? I tried to do:

    auto imglist = tuple("aaa");
    imglist.expand["sss"];
    writeln(imglist);

but got very strange error:
app.d(30): Error: cannot implicitly convert expression ("sss") of 
type string to  uint
Nov 01 2014
next sibling parent Philippe Sigaud via Digitalmars-d-learn writes:
On Sat, Nov 1, 2014 at 9:34 PM, Suliman via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:
 Few questions.

 1. In examples tuples are created with keyword auto. Can I create them with
 another keyword. Or auto mean structure of data, that have not standard type
 like (int or string)?
`tuple' is a function defined in the std.typecons module. It's a helper function to create Tuple!(T...), a templated struct acting as a tuple. Don't forget the type will depend on the types of the arguments, so: auto tup = tuple(1.0, "abc", [2,3]); contains a float, a string and an array of int's, so it's type is Tuple!(float, string, int[]): Tuple!(float, string, int[]) tup = tuple(1.0, "abc", [2,3]); It's a bit long to type, so most people prefer using `auto' for such declarations.
 2. How ti expend tuple? I tried to do:

    auto imglist = tuple("aaa");
    imglist.expand["sss"];
    writeln(imglist);

 but got very strange error:
 app.d(30): Error: cannot implicitly convert expression ("sss") of type
 string to  uint
Expand, you mean, as in appending to a tuple? It can be done, but it will create a new type. First, tup.expand gives you access to the 'raw' tuple ( a template parameter list, to be precise) underneath Tuple!(T...). The returned value can be indexed, sliced and its length is known at compile time. Continuing with my example: auto first = tup.expand[0]; // "1.0" auto slice = tup.expand[1..$]; // ("abc", [2,3]) auto len = tup.expand.length; // 3, since tup has three elements. That explains the error you get: imglist.expand is a bit like a vector: it can be indexed, but with uint, not with a string. I thought you were expanding it, but to the compiler you were trying to index using a string. If you really want to append to a tuple, you must create a new variable, since the augmented tuple will have a different type: auto tup2 = tuple(tup.expand, 'd'); // tup2 == (1.0, "abc", [2,3], 'd') The type of tup2 is then Tuple!(float, string, int[], char) Note that, in your case, as you're using only strings, an array of strings would probably be easier to use.
Nov 01 2014
prev sibling next sibling parent Philippe Sigaud via Digitalmars-d-learn writes:
 I thought you were expanding it
Drat. *You* thought you were expanding it.
Nov 01 2014
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/01/2014 01:34 PM, Suliman wrote:

 Or auto mean structure of data, that have not standard type
 like (int or string)?
D is a strongly typed language, so every variable has its specific type. D also has type inference, meaning that we don't need to specify types explicitly. In theory, even the following could work: t = tuple(42, 1.5); and the type of t would be Tuple!(int, double). However, the syntax rules require a keyword before t. If t could be either one of the following, we could simply write: const t = ... immutable t = ... shared t = ... static t = ... The problem is when t is not either one of those. In that case auto comes to the rescue: auto t = ... Note that auto (meaning "automatic storage duration") is actually redundant there but works well because it kind of means "infer the type automatically." Hm. It is redundant even there because "infer" implies "automatic" :p. Ali
Nov 01 2014
parent reply "Suliman" <evermind live.ru> writes:
Thanks! In which cases tuples can be helpful?
Nov 02 2014
parent Philippe Sigaud via Digitalmars-d-learn writes:
Any time you want to return or store a group of values of different types.

In a way, tuples are just structs without a name (anonymous structs,
if you will).

Say you have function `foo' and want it to return an int and a string.
In D, you cannot do:

(int, string) foo() { ...  return (3, "abc");}

But you can do:

import std.typecons: tuple;
auto foo() { return tuple(3, "abc");}

Which is more or less equivalent to:

struct FooReturn { int i; string s;}
FooReturn foo() { return FooReturn(3, "abc");}


`Tuple' and its associated factory function `tuple' is just the
standard way to group values together in Phobos. You'll see some
functions returning Tuples, for example. Some D constructs also
recognize std.typecons.Tuple and deal with it elegantly. By defining
you own struct (like `FooReturn' in the previous example), you gain
some control on your code (you can define how people can interact with
FooReturn, you can test for it, etc), but you also lose the
possibility of easy interaction with some parts of Phobos.
Nov 02 2014