Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation about abstract type declarations on assignments #56809

Open
ArnoStrouwen opened this issue Dec 12, 2024 · 2 comments
Open

Documentation about abstract type declarations on assignments #56809

ArnoStrouwen opened this issue Dec 12, 2024 · 2 comments
Labels
docs This change adds or pertains to documentation types and dispatch Types, subtyping and method dispatch

Comments

@ArnoStrouwen
Copy link
Contributor

https://docs.julialang.org/en/v1/manual/types/#Type-Declarations

When appended to a variable on the left-hand side of an assignment, or as part of a local declaration, the :: operator means something a bit different: it declares the variable to always have the specified type

But this does not seem to be true for abstract types:

b::Real = 1
b = 1.0
b = '1'
b = "1"

b does not have type Real, instead it can have any concrete subtype of Real.

@barucden
Copy link
Contributor

My understanding of the documentation is that b::T = x ensures that b isa T will always hold.

In your example:

julia> b::Real = 1
1

julia> b = 1.0
1.0

julia> b isa Real
true

julia> b = '1'
'1': ASCII/Unicode U+0031 (category Nd: Number, decimal digit)

julia> b isa Real # even though  '1' isa Real == false
true

IIRC, any assignment to a variable declared as b::T translates to

b = convert(T, x)::T

@ArnoStrouwen
Copy link
Contributor Author

My understanding of the documentation is that b::T = x ensures that b isa T will always hold.

IIRC, any assignment to a variable declared as b::T translates to
b = convert(T, x)::T

If it works like this, I think the docs would benefit from explicitly mentioning this.
The current phrasing is confusing to me.

@nsajko nsajko added docs This change adds or pertains to documentation types and dispatch Types, subtyping and method dispatch labels Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

3 participants