How can I check if an element is iterable?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun May 3 23:45:54 UTC 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





More information about the Digitalmars-d-learn mailing list