May 25, 2016
On Wednesday, 25 May 2016 at 14:32:11 UTC, ag0aep6g wrote:
> On 05/25/2016 03:27 PM, Chris wrote:
>> Why can the tuple be iterated with foreach, as in my quick fix, and
>> indexed with tuple[0..], but is not accepted as a range? What are the
>> differences?
>
> popFront doesn't make sense with a tuple (aka expression list). When you remove the first element of a tuple, you get another tuple of a different type, but popFront can't change the type of the range.
>
> In code:
>
> ----
> void main()
> {
>     import std.meta: AliasSeq;
>     AliasSeq!(int, int, int) tuple = AliasSeq!(1, 2, 3);
>     tuple.popFront(); /* How would this be implemented? Would have to change tuple's type to AliasSeq!(int, int). */
> }
> ----
>
>> Is there a way to rangify a tuple?
>
> std.range.only:
>
> ----
> void main()
> {
>     import std.meta: AliasSeq;
>     import std.range: only;
>     AliasSeq!(int, int, int) tuple = AliasSeq!(1, 2, 3);
>     auto range = only(tuple);
>     range.popFront(); /* ok */
> }
> ----

I see. Maybe it would be worth adding a wrapper to typecons.Tuple or std.range that helps to rangify tuples. I cannot think of any use case right now. I never needed this and in the example that started this thread, it would keep the function from converting mixed tuples (cf. my example above).
May 25, 2016
On 05/25/2016 04:39 PM, Chris wrote:
> I see. Maybe it would be worth adding a wrapper to typecons.Tuple or
> std.range that helps to rangify tuples.

std.range.only is that wrapper.

> I cannot think of any use case
> right now. I never needed this and in the example that started this
> thread, it would keep the function from converting mixed tuples (cf. my
> example above).

I'm not sure what you're saying here. Should the wrapper support tuples with different element types? Can't be a range then as a range has just one element type.
May 25, 2016
On Wednesday, 25 May 2016 at 14:48:14 UTC, ag0aep6g wrote:
> On 05/25/2016 04:39 PM, Chris wrote:
>> I see. Maybe it would be worth adding a wrapper to typecons.Tuple or
>> std.range that helps to rangify tuples.
>
> std.range.only is that wrapper.

Duh! Of course! :-)

>> I cannot think of any use case
>> right now. I never needed this and in the example that started this
>> thread, it would keep the function from converting mixed tuples (cf. my
>> example above).
>
> I'm not sure what you're saying here. Should the wrapper support tuples with different element types? Can't be a range then as a range has just one element type.

I'm saying that for the above example something like std.range.only doesn't make sense, because the user might want to turn anything into string as in

test(1, 2, "v", 4, 'c');

Mixed type tuples cannot be rangified, of course (and std.range.only takes care of that).
May 25, 2016
On Wednesday, 25 May 2016 at 13:27:28 UTC, Chris wrote:
> Why can the tuple be iterated with foreach, as in my quick fix, and indexed with tuple[0..], but is not accepted as a range? What are the differences? Is there a way to rangify a tuple?

 The tuple is identified/used at compile-time, as such it's a compiler primitive and not a range. Foreach in this case will unroll the loop regardless how it looks. So...

  test(Args...)(Args args) {
  ...
  foreach (const ref i; items)
    itemstrings ~= i.to!string;

  Will become: (const and ref are pointless in this example, unless the args are referenced)

  test(int arg1, int arg2, int arg3, int arg4) {
  ...
    itemstrings ~= arg1.to!string;
    itemstrings ~= arg2.to!string;
    itemstrings ~= arg3.to!string;
    itemstrings ~= arg4.to!string;


 Trying to use map on it was literally expanding the entire input to map.

  itemstrings => map!(to!string)(items);

  will become (all as individual items, not an array or range):

  itemstrings => map!(to!string)(arg1, arg2, arg3, arg4);

  You could probably use Array() inside which might work, assuming they are all the same type (or does it support a multi-type? I don't think so, unless you use Variant).

  There's a good section on Homogeneous vs Variadic behavior. Starts on page TDPL pg. 159-164

 If you had the function declared as: test(int[] args...) then numbers passed with or without an array would be converted as an array. so your calling function would have an array which can be used with map, however it loses it's flexible template and variable types...
May 26, 2016
On Wednesday, 25 May 2016 at 19:07:32 UTC, Era Scarecrow wrote:
> On Wednesday, 25 May 2016 at 13:27:28 UTC, Chris wrote:
>> Why can the tuple be iterated with foreach, as in my quick fix, and indexed with tuple[0..], but is not accepted as a range? What are the differences? Is there a way to rangify a tuple?
>
>  The tuple is identified/used at compile-time, as such it's a compiler primitive and not a range. Foreach in this case will unroll the loop regardless how it looks. So...
>
>   test(Args...)(Args args) {
>   ...
>   foreach (const ref i; items)
>     itemstrings ~= i.to!string;
>
>   Will become: (const and ref are pointless in this example, unless the args are referenced)
>
>   test(int arg1, int arg2, int arg3, int arg4) {
>   ...
>     itemstrings ~= arg1.to!string;
>     itemstrings ~= arg2.to!string;
>     itemstrings ~= arg3.to!string;
>     itemstrings ~= arg4.to!string;
>
>
>  Trying to use map on it was literally expanding the entire input to map.

Ah, I didn't know that it was just unrolled. That makes sense, of course.

[snip]
1 2
Next ›   Last »