digitalmars.D.learn - How can I check if an element is iterable?
- Marcone (1/1) May 03 2020 How can I check if an element is iterable in Dlang?
- Adam D. Ruppe (2/3) May 03 2020 http://dpldocs.info/experimental-docs/std.traits.isIterable.html
- Marcone (4/7) May 03 2020 Not working. How can I check if a variable is iterable? Becouse
- Adam D. Ruppe (2/3) May 03 2020 Every variable has a type. You can get it with typeof(varaiable)
- Marcone (2/5) May 03 2020 I need in runtime.
- Jonathan M Davis (11/17) May 03 2020 Then store that information in an enum or variable. I don't know why you...
- Marcone (3/6) May 03 2020 I don't want to check if type is iterable, but if variable is
- =?UTF-8?Q?Ali_=c3=87ehreli?= (25/32) May 03 2020 Still, the type of a variable would determine whether whether it's itera...
- Marcone (2/29) May 04 2020 Very good! Thank you!
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: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.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: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:I need in runtime.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, 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: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 DavisOn Sunday, 3 May 2020 at 20:21:24 UTC, Marcone wrote:I need in runtime.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:11:58 UTC, Adam D. Ruppe wrote:On Sunday, 3 May 2020 at 20:02:09 UTC, Marcone wrote:I don't want to check if type is iterable, but if variable is iterable.How can I check if an element is iterable in Dlang?http://dpldocs.info/experimental-docs/std.traits.isIterable.html
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: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); } AliOn Sunday, 3 May 2020 at 20:02:09 UTC, Marcone wrote:I don't want to check if type is iterable, but if variable is iterable.How can I check if an element is iterable in Dlang?http://dpldocs.info/experimental-docs/std.traits.isIterable.html
May 03 2020
On Monday, 4 May 2020 at 01:49:28 UTC, Ali Çehreli wrote:On 5/3/20 1:44 PM, Marcone wrote:Very good! Thank you![...]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