Stream: helpdesk (published)

Topic: ✔ Understanding type signatures


view this post on Zulip Michael Fiano (Jun 29 2022 at 01:02):

Can you please tell me why this doesn't match the 2nd candidate, and what I must do to get it to work?

(Practice) julia> x
3-element StaticArraysCore.MArray{Tuple{3}, Float64, 1, 3} with indices SOneTo(3):
 1.0
 2.0
 3.0

(Practice) julia> ones(x)
MethodError: no method matching ones(::StaticArraysCore.MArray{Tuple{3}, Float64, 1, 3})
Closest candidates are:
  ones(::Tuple{Vararg{Union{Integer, AbstractUnitRange}}}) at ~/.data/asdf-vm/installs/julia/1.7.3/share/julia/base/array.jl:522
  ones(::Type{StaticArraysCore.MArray{Tuple{N}, T, 1, N} where T}) where N at ~/.data/julia/packages/StaticArrays/5bAMi/src/MVector.jl:4
  ones(::Type{StaticArraysCore.SArray{Tuple{N}, T, 1, N} where T}) where N at ~/.data/julia/packages/StaticArrays/5bAMi/src/SVector.jl:4
  ...

view this post on Zulip Brian Chen (Jun 29 2022 at 01:18):

Unfortunately this particular method list and how it highlights on Zulip (and probably elsewhere too) makes the answer harder to find. The key is that candidates 2 and 3 match on Type{M/SArray} instead of M/SArray. Thus they'll match ones(typeof(x)) but not ones(x).

view this post on Zulip Michael Fiano (Jun 29 2022 at 01:19):

That seems rather bizarre from a user standpoint.

view this post on Zulip Michael Fiano (Jun 29 2022 at 01:19):

That means runtime type checking is necessary, correct?

view this post on Zulip Michael Fiano (Jun 29 2022 at 01:19):

as is implied with typeof

view this post on Zulip Brian Chen (Jun 29 2022 at 01:20):

Semantically it always happens, but more often than not it is elided completely by the compiler.

view this post on Zulip Brian Chen (Jun 29 2022 at 01:21):

e.g. if you define a function indirection(::T) where T = ones(T).

view this post on Zulip Michael Fiano (Jun 29 2022 at 01:24):

Hmm, ok. Though I get a "Missing reference: T" with that.

view this post on Zulip Michael Fiano (Jun 29 2022 at 01:27):

Ah nevermind. I see what I did. Thanks for the help.

view this post on Zulip Brian Chen (Jun 29 2022 at 01:29):

I do think there's a UX argument for Types printing out differently in method signatures than other parametric types, but I'm not sure how reasonable/feasible that is.

view this post on Zulip Michael Fiano (Jun 29 2022 at 01:33):

Relatedly, I want to define a method analogous to isone for 1xN matrices, that checks if all their elements are ones. This seems to break expectations to add a method on an MVector, as I'm not checking the main diagonal of a matrix. Thoughts?

view this post on Zulip Michael Fiano (Jun 29 2022 at 01:34):

or rather analagous to iszero

view this post on Zulip Michael Fiano (Jun 29 2022 at 01:34):

but defining for isone seems wrong.

view this post on Zulip Sukera (Jun 29 2022 at 13:00):

isone is typically only used for scalars - is there anything wrong with all(isone, x)?

view this post on Zulip Sukera (Jun 29 2022 at 13:01):

and since neitherisone nor SArray are owned by you, defining a new isone method for SArray is definitely type piracy

view this post on Zulip Michael Fiano (Jun 29 2022 at 13:02):

I did isones(v::MathVector{T}) where {T} = v == ones(T). Notice the custom plural function name.

view this post on Zulip Sukera (Jun 29 2022 at 13:02):

gotcha!

view this post on Zulip Sukera (Jun 29 2022 at 13:03):

that definition seems a little wasteful, since it'll allocate the ones(T) vector too, for comparison

view this post on Zulip Michael Fiano (Jun 29 2022 at 13:03):

Yeah I realize that. I'm iteratively trying to make everything I write better as I read the Base/stdlib module docs, but if you have any suggestions, I'm all ears.

view this post on Zulip Sebastian Pfitzner (Jun 29 2022 at 13:04):

all(isone, x)

view this post on Zulip Michael Fiano (Jun 29 2022 at 13:04):

Ah I missed that. Thanks!

view this post on Zulip Notification Bot (Jun 30 2022 at 23:29):

Michael Fiano has marked this topic as resolved.


Last updated: Nov 06 2024 at 04:40 UTC