How is Number(1) allowed when Number is an abstract type?

abstract type cannot be instantiated (no constructor). How are such expressions as Number(1), Number(1+im) possible then?

Try

@edit Number(1)

which takes you to the relevant method

(::Type{T})(x::T) where {T<:Number} = x

ie it is simply identity.

Ah, so it is simply an identity function with the same name. Thank you!

@Tamas_Papp, I have additional question:

I believe (::Type{T})(x::T) where {T<:Number} = x could have been written as T(x::T) where T <: Number = x, which looks simpler and in fact It works as I experimented. I wonder if there is any benefit to using the original syntax?

I think your experiments were misleading, the second option just defines a generic function T (with no methods).

How the latter should work as the former. T as method name and T as parameter are different things. It should not produce a constructor for Number. The former adds to an object callability(function like behaviour) but not necessarily a constructor, the latter is not(it produces a method named T).

1 Like

I got your point. And you are right. Now I fully understood: As @Tamas_Papp said, it is misleading. The two statement are different things although they look like producing same effect. Thank you!

I think more accurately the latter produces a generic function T not method named T. :slight_smile:

The two forms have very different effects.

I assume that you tried it in the REPL, then tested for Number(1), which kept working because the T function would not affect that at all. But this does not mean that nothing changed.

1 Like

I could have missed that important point. Iā€™ve learned a lot. Thank you :slight_smile: