# How to check if a variable is scalar in julia

I would like to check if a variable is scalar in julia, such as Integer, String, Number, but not AstractArray, Tuple, type, struct, etc. Is there a simple method to do this (i.e. `isscalar(x)`)

• How about `isa(x, Number) || isa(x, String)` – Tasos Papastylianou Dec 11 '17 at 23:31
• (Presumably you could also add `Char` to that list) – Tasos Papastylianou Dec 11 '17 at 23:32
• Thanks, so probably listing all scalar types is the simplest solution. I'll go through the julia type page. – Phuoc Dec 11 '17 at 23:43
• I'm not sure if I miss any scalar but here it goes: `isscalar(x) = isa(x, Union{Number,AbstractString,Char,Bool})`. This misses out the Nullable however! – Phuoc Dec 11 '17 at 23:56
• I suppose whether you want to consider `null` a scalar or not is up for debate (e.g. note that a 'null array' is not the same as an 'array of nulls') ... but if you did want to consider it, I suppose adding a `|| isnull(x)` to your definition is possible. The choice of union is interesting; I have no idea if this is more efficient (I would assume that it is not, but I may be wrong). – Tasos Papastylianou Dec 12 '17 at 0:13

The notion of what is, or is not a scalar is under-defined without more context. Mathematically, a scalar is defined; (Wikipedia)

A scalar is an element of a field which is used to define a vector space.

That is to say, you need to define a vector space, based on a field, before you can determine if something is, or is not a scalar (relative to that vector space.). For the right vector space, tuples could be a scalar.

Of-course we are not looking for a mathematically rigorous definition. Just a pragmatic one.

### Base it off what Broadcasting considers to be scalar

I suggest that the only meaningful way in which a scalar can be defined in julia, is of the behavior of broadcast. As of Julia 1:

``````using Base.Broadcast

isscalar(x::T) where T = isscalar(T)
``````

In julia 0.7, `Scalar` is the default. So it is basically anything that doesn't have specific broadcasting behavior, i.e. it knocks out things like array and tuples etc.: using Base.Broadcast

``````isscalar(x::T) where T = isscalar(T)
``````

In julia 0.6 this is a bit more messy, but similar:

``````isscalar(x::T) where T = isscalar(T)
``````

The advantage of using the methods for `Broadcast` to determine if something is scalar, over using your own methods, is that anyone making a new type that is going to act in a scalar way must make sure it works with those methods correctly (or actually nonscalar since scalar is the default.)

### Structs are not not scalar

That is to say: sometimes structs are scalar and sometimes they are not and it depends on the struct.

Note however that these methods do not consider `struct` to be non-scalar. I think you are mistaken in your desire to.

Julia structs are not (necessarily or usually) a collection type. Consider that: `BigInteger`, `BigFloat`, `Complex128` etc etc are all defined using structs

I was tempted to say that having a `start` method makes a type nonscalar, but that would be incorrect as `start(::Number)` is defined. (This has been debated a few times)

• You are correct, structs or composite objects should be scalars to be consistent with existing "struct" numbers. Thanks a lot for your definition of scalar using Broadcast.Scalar. I wish broadcast can be applied to subarray (i.e. broadcast to slices of array). But it seems break the concept of broadcast. – Phuoc Dec 13 '17 at 0:11
• @Phuoc ask that as a seperate question. I'ld be interested to see the answers. – Lyndon White Dec 13 '17 at 1:19
• The link to Broadcast.Scalar doesn't show any definition for Scalar. – Amin Sep 22 '19 at 8:49
• That is a link to `Broadcast`. The next sentence is starts with the word Scalar – Lyndon White Sep 22 '19 at 9:00
• Also I think it may have changed Abit since Julia 0.7-prerelease. so the terms might be different. I will look into and update the answer. – Lyndon White Sep 22 '19 at 9:01

I found myself needing to capture the notion of if something was scalar or not recently in MultiResolutionIterators.jl.

I found the boardcasting based rules from the other answer, did not meet my needs.

In particular I wanted to consider strings as nonscalar.

I defined a trait, bases on `method_exists(start, (T,))`, with some exceptions as mentioned e.g. for `Number`.

``````abstract type Scalarness end
struct Scalar <: Scalarness end
struct NotScalar <: Scalarness end

isscalar(::Type{Any}) = NotScalar() # if we don't know the type we can't really know if scalar or not
isscalar(::Type{<:AbstractString}) = NotScalar() # We consider strings to be nonscalar
isscalar(::Type{<:Number}) = Scalar() # We consider Numbers to be scalar
isscalar(::Type{Char}) = Scalar() # We consider Sharacter to be scalar
isscalar(::Type{T}) where T = method_exists(start, (T,)) ? NotScalar() : Scalar()
``````

Something similar is also done by AbstractTrees.jl

``````isscalar(x) == applicable(start, x) && !isa(x, Integer) && !isa(x, Char) && !isa(x, Task)
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.89298505,"math_prob":0.79841715,"size":5053,"snap":"2021-31-2021-39","text_gpt3_token_len":1301,"char_repetition_ratio":0.13487819,"word_repetition_ratio":0.11633372,"special_character_ratio":0.2560855,"punctuation_ratio":0.15829384,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9730698,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-30T12:01:57Z\",\"WARC-Record-ID\":\"<urn:uuid:7a73d1c6-deb4-49d7-9783-b49d5761fe64>\",\"Content-Length\":\"190539\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:85abe47d-6ebc-4bd8-8945-1e2a2fecd073>\",\"WARC-Concurrent-To\":\"<urn:uuid:54b2a6ee-77b4-4421-88bc-c0ff9a45681a>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/47762777/how-to-check-if-a-variable-is-scalar-in-julia\",\"WARC-Payload-Digest\":\"sha1:MCWPERH6QMZ7F63XY2DFX75HI5JQH7PT\",\"WARC-Block-Digest\":\"sha1:MFZVOMMELGCEQI3NF7TNGVHJEUC5MCG4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046153966.52_warc_CC-MAIN-20210730091645-20210730121645-00058.warc.gz\"}"}