Thread overview
How can I check if an element is iterable?
May 03, 2020
Marcone
May 03, 2020
Adam D. Ruppe
May 03, 2020
Marcone
May 03, 2020
Adam D. Ruppe
May 03, 2020
Marcone
May 03, 2020
Jonathan M Davis
May 03, 2020
Marcone
May 04, 2020
Ali Çehreli
May 04, 2020
Marcone
May 03, 2020
How can I check if an element is iterable in Dlang?
May 03, 2020
On Sunday, 3 May 2020 at 20:02:09 UTC, Marcone wrote:
> How can I check if an element is iterable in Dlang?

http://dpldocs.info/experimental-docs/std.traits.isIterable.html
May 03, 2020
On Sunday, 3 May 2020 at 20:11:58 UTC, Adam D. Ruppe wrote:
> On Sunday, 3 May 2020 at 20:02:09 UTC, Marcone wrote:
>> How can I check if an element is iterable in Dlang?
>
> http://dpldocs.info/experimental-docs/std.traits.isIterable.html

Not working. How can I check if a variable is iterable? Becouse my function can receive mult values, and if an value is array or tuple I want iterate over it.
May 03, 2020
On Sunday, 3 May 2020 at 20:11:58 UTC, Adam D. Ruppe wrote:
> On Sunday, 3 May 2020 at 20:02:09 UTC, Marcone wrote:
>> How can I check if an element is iterable in Dlang?
>
> http://dpldocs.info/experimental-docs/std.traits.isIterable.html

I don't want to check if type is iterable, but if variable is iterable.
May 03, 2020
On Sunday, 3 May 2020 at 20:21:24 UTC, Marcone wrote:
> How can I check if a variable is iterable?

Every variable has a type. You can get it with typeof(varaiable)
May 03, 2020
On Sunday, 3 May 2020 at 20:46:30 UTC, Adam D. Ruppe wrote:
> On Sunday, 3 May 2020 at 20:21:24 UTC, Marcone wrote:
>> How can I check if a variable is iterable?
>
> Every variable has a type. You can get it with typeof(varaiable)

I need in runtime.
May 03, 2020
On Sunday, May 3, 2020 2:54:17 PM MDT Marcone via Digitalmars-d-learn wrote:
> On Sunday, 3 May 2020 at 20:46:30 UTC, Adam D. Ruppe wrote:
> > On Sunday, 3 May 2020 at 20:21:24 UTC, Marcone wrote:
> >> How can I check if a variable is iterable?
> >
> > Every variable has a type. You can get it with typeof(varaiable)
>
> I need in runtime.

Then store that information in an enum or variable. I don't know why you'd care about it at runtime though. D is statically typed, so the code that gets compiled would be completely different depending on the type, and you'd have to determine at compile time whether you were going to try to iterate over it or not. At most, you'd have something like a variant where you had to cast it to a particular type, and which branch you took would be decided at runtime. But even then, all of the code related to iteration would have to be dealt with at compile time, so all of the checks for whether a type was iteraterable or not would be done at compile time.

- Jonathan M Davis



May 03, 2020
On 5/3/20 1:44 PM, Marcone wrote:
> On Sunday, 3 May 2020 at 20:11:58 UTC, Adam D. Ruppe wrote:
>> On Sunday, 3 May 2020 at 20:02:09 UTC, Marcone wrote:
>>> How can I check if an element is iterable in Dlang?
>>
>> http://dpldocs.info/experimental-docs/std.traits.isIterable.html
> 
> I don't want to check if type is iterable, but if variable is iterable.

Still, the type of a variable would determine whether whether it's iterable.

As an improvement, the following program can be changed to call use() recursively to visit all members of e.g. structs (which can be determined by 'is (T == struct)').

import std.stdio;
import std.traits;

void use(T)(T var, size_t indent = 0) {
  static if (isIterable!T && !isSomeString!T) {
    foreach (i, e; var) {
      writefln!"%*s: %s"(indent, i, e);
    }

  } else {
    writefln!"%*s"(indent, var);
  }
}

void main() {
  int i = 42;
  string s = "hello";
  double[] arr = [ 1.5, 2.5, 3.5 ];
  use(i);
  use(s);
  use(arr);
}

Ali
May 04, 2020
On Monday, 4 May 2020 at 01:49:28 UTC, Ali Çehreli wrote:
> On 5/3/20 1:44 PM, Marcone wrote:
>> [...]
>
> Still, the type of a variable would determine whether whether it's iterable.
>
> As an improvement, the following program can be changed to call use() recursively to visit all members of e.g. structs (which can be determined by 'is (T == struct)').
>
> import std.stdio;
> import std.traits;
>
> void use(T)(T var, size_t indent = 0) {
>   static if (isIterable!T && !isSomeString!T) {
>     foreach (i, e; var) {
>       writefln!"%*s: %s"(indent, i, e);
>     }
>
>   } else {
>     writefln!"%*s"(indent, var);
>   }
> }
>
> void main() {
>   int i = 42;
>   string s = "hello";
>   double[] arr = [ 1.5, 2.5, 3.5 ];
>   use(i);
>   use(s);
>   use(arr);
> }
>
> Ali

Very good! Thank you!