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

Lock type via := declaration #44259

Open
jonas-schulze opened this issue Feb 19, 2022 · 3 comments
Open

Lock type via := declaration #44259

jonas-schulze opened this issue Feb 19, 2022 · 3 comments
Labels
kind:speculative Whether the change will be implemented is speculative

Comments

@jonas-schulze
Copy link
Contributor

I would like to suggest

a := 42

as a shortcut for

a::typeof(42) = 42

The same idea but with different syntax had already been proposed in #43671 (comment).

@jonas-schulze jonas-schulze added the kind:speculative Whether the change will be implemented is speculative label Feb 19, 2022
@jonas-schulze
Copy link
Contributor Author

Just if people search for it: implicit type annotation.

@quinnj
Copy link
Member

quinnj commented Feb 19, 2022

That seems pretty clever/useful. One thing to think about is that currently you're not allowed to have multiple type declarations, regardless of the scope of the variable in a function, i.e.:

function foo()
       x::Int = 0
       for _ = 1:10
           x::Float64 = _ / 10
       end
       return x
  end
ERROR: syntax: multiple type declarations for "x"
Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

I mention that because I could see that being a confusing error if I had the same variable name in different scopes within a function, even if the desired type declarations are the same.

@jonas-schulze
Copy link
Contributor Author

Good point. I am in favor of keeping that restriction, though, even if the annotation is for the same type. I personally like it a lot if the compiler gives me hints, and this is a very clear error message. To me, more than one type annotation hints at a potential code smell, even if it's the same type. The syntax x := 0 is used in mathematics as "define x to be equal to 0", and every variable should only be declared once. The syntax x = 0 feels more like "give zero the name x".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:speculative Whether the change will be implemented is speculative
Projects
None yet
Development

No branches or pull requests

2 participants