# Detect argument is an iterable or a "scalar"

**URL:** <https://discourse.julialang.org/t/detect-argument-is-an-iterable-or-a-scalar/101436>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 10, 2023, 1:53pm UTC](https://discourse.julialang.org/t/detect-argument-is-an-iterable-or-a-scalar/101436 "2023-07-10T13:53:03Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 11, 2023, 4:04pm UTC](https://discourse.julialang.org/t/detect-argument-is-an-iterable-or-a-scalar/101436/21 "2023-07-11T16:04:32Z")

</div>

Just `Tuple` would be a `UnionAll` (if tuple was not more primitive than other parametric types), and these are never `isbits` I think.

```julia
help?> UnionAll
search: UnionAll

  UnionAll

  A union of types over all values of a type parameter. UnionAll is used to describe
  parametric types where the values of some parameters are not known.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> typeof(Vector)
  UnionAll
  
  julia> typeof(Vector{Int})
  DataType

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [July 11, 2023, 4:27pm UTC](https://discourse.julialang.org/t/detect-argument-is-an-iterable-or-a-scalar/101436/22 "2023-07-11T16:27:30Z")

</div>

the concretetype and bitstype flags appear to be independent. I wonder if there is currently some type in the system that is bitstype but not concretetype?

```julia

@edit isbitstype(Tuple{Int,Int})
# isbitstype(@nospecialize t) = (@_total_meta; isa(t, DataType) && (t.flags & 0x8) == 0x8)

@edit isconcretetype(Vector{Int})

# isconcretetype(@nospecialize(t)) = (@_total_meta; isa(t, DataType) && (t.flags & 0x2) == 0x2)

```

EDIT: I answer myself the definition requires that the type be of type DataType

julia\> isa(Vector, DataType)  
false

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [July 11, 2023, 7:43pm UTC](https://discourse.julialang.org/t/detect-argument-is-an-iterable-or-a-scalar/101436/23 "2023-07-11T19:43:40Z")

</div>

Still using the legth() function to distinguish cases, if the purpose is to define the names of the files to be generated, you could use a scheme like this

```julia
julia> function func(xs, args=nothing )
         # . . . here comes a lot of preparation to plot figures . . .
            s=length(xs)==1 ? "" : f"-p\%3.3d(i)"
               pre="myfig"
               suf=".png"
            for (i, x) in pairs(xs)
               # p = plot_a_figure_with_x(x, args )
               # savefig(p, "$pre$s$suf") # -> myfig-p001.png, myfig-p002.png, . . .
               println("$pre$s$suf", " x = $x")
            end
       end
func (generic function with 2 methods)

julia> func(1:2:6) # iterable
myfig-p001.png x = 1
myfig-p002.png x = 3
myfig-p003.png x = 5

julia> func(3) # scalar
myfig.png x = 3

```

or using a flag

```julia
julia> function func(xs;scalar=false )
         # . . . here comes a lot of preparation to plot figures . . .
            s=scalar ? "" : f"-p\%3.3d(i)"
               pre="myfig"
               suf=".png"
            for (i, x) in pairs(xs)
               # p = plot_a_figure_with_x(x, args )
               # savefig(p, "$pre$s$suf") # -> myfig-p001.png, myfig-p002.png, . . .
               println("$pre$s$suf", " x = $x")
            end
       end
func (generic function with 2 methods)

julia> func(1:2:6) # iterable
myfig-p001.png x = 1
myfig-p002.png x = 3
myfig-p003.png x = 5

julia> func(3, scalar=true) # scalar
myfig.png x = 3

```

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [July 12, 2023, 10:15am UTC](https://discourse.julialang.org/t/detect-argument-is-an-iterable-or-a-scalar/101436/24 "2023-07-12T10:15:55Z")

</div>

Contrary to what you say, `hasmethod` will work perfectly since the goal is to have no error in the plotting program. It will not give an error if a “scalar” goes through the “iterable” part of the code. Also, I think in recent/future versions of julia the cost of `hasmethod` will become very small.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 12, 2023, 10:40am UTC](https://discourse.julialang.org/t/detect-argument-is-an-iterable-or-a-scalar/101436/25 "2023-07-12T10:40:02Z")

</div>

If you look at it from this perspective, then why bother to have a ‘scalar’ part of the code anyway or even check if it is iterable with `hasmethod`? If you pass a single number (a scalar), or a collection of numbers, both can be treated as ‘iterable’. The only reason for having these two parts is that you _want_ the single number `1` to be treated differently from `[1]`, and you will not be able to signal this with just `Number` types and `hasmethod(iterate, ...)`.

Basically, my point is, `hasmethod(iterate, ...)` does not solve the semantic problem for types that implement `iterate` but are often seen as a scalar and you want to treat them as a scalar. The classic example is `String`, you often will want to treat it as a single scalar, but as it implements iterate it will be treated by the plotting method as a sequence of characters.

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [July 12, 2023, 10:50am UTC](https://discourse.julialang.org/t/detect-argument-is-an-iterable-or-a-scalar/101436/26 "2023-07-12T10:50:37Z")

</div>

You will get an error if you pass every object to the “iterable” part of the code. For example, a `Symbol` is not iterable. The goal is to have something working for every object, be it a collection or not.

[Previous page](https://discourse.julialang.org/t/detect-argument-is-an-iterable-or-a-scalar/101436.md?page=1)
