www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can I check if an element is iterable?

reply Marcone <marcone email.com> writes:
How can I check if an element is iterable in Dlang?
May 03 2020
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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
next sibling parent reply Marcone <marcone email.com> writes:
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
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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
parent reply Marcone <marcone email.com> writes:
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
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
prev sibling parent reply Marcone <marcone email.com> writes:
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
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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 03 2020
parent Marcone <marcone email.com> writes:
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!
May 04 2020