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

Immediate, Rust-level type hints #3597

Open
MilesCranmer opened this issue Apr 25, 2024 · 2 comments
Open

Immediate, Rust-level type hints #3597

MilesCranmer opened this issue Apr 25, 2024 · 2 comments

Comments

@MilesCranmer
Copy link

MilesCranmer commented Apr 25, 2024

@Zentrik @pfitzseb I am thinking about the best way to get continual feedback with more detailed type hints in VSCode. Similar to how it is done with rust-analyzer.

Since Julia is very dynamic, this is obviously a bit trickier. Right now the mechanism for this is to connect a REPL, and run Cthulhu descend (thanks to @Zentrik’s PR and @timholy’s Cthulhu).

This is awesome but I am wondering if we can do even better. I want type information to automatically show up while I am typing out the function, so I can immediately spot type indtability, rather than needing to manually descend to find it.

Here’s one idea: could we have a special comment that tells Julia LSP to run Cthulhu for the function with a given input?

For example:

function foo(x)
    out = []
    for xi in x
        push!(out, xi)
    end
    push!(out, "bar")
    out
end
# descend: ([1, 2, 3],)

This "descend" would tell the Julia language server to continually run Cthulhu.descend(foo, ([1, 2, 3],)) whenever the code is changed. (Or just @code_warntype, but I feel like multiple levels could be useful)

In the future you could also specify multiple descend. In VSCode I believe there is the ability to cycle between code warnings for a given function (?) which would let you quickly switch between seeing the warnings for different example inputs.

The descend could also be more complex to allow different input types. For example,

# descend: begin
#    using MyPkg
#    (MyPkg.MyStruct(), "foobar")
# end

I think the comments are the right place to put this because this is where you control linters and formatters.


Edit: I'm going to cross-post this on the discourse as I think wider input might be useful too!

@MilesCranmer MilesCranmer changed the title Continual, Rust-level type hints Immediate, Rust-level type hints Apr 25, 2024
@Zentrik
Copy link
Contributor

Zentrik commented Apr 25, 2024

This is along the lines of what I wanted to do initially, but one reason I switched was that there was a desire to not load/ execute user code in the LSP.

This seems like a good idea if we can deal with that.
Alternatively, the extension could just take the descend call you wanted to run, e.g. Cthulhu.descend(foo, ([1, 2, 3],)) and run it in the REPL in the background. I think this should work and achieve a similar result.

The other reason I switched to using the REPL was so we could use variables and redefined function from the REPL but with the alternative approach maybe we could continue to support that. Or at worst we don't but you can just run 'descend like we do currently.

Another concern would be if Cthulhu would be fast enough, iirc the main reasons why it's slow is pretty printing types and compilation. Presumably if we're rerunning Cthulhu a lot we could ensure it's caching things appropriately so that it is able to be fast.

Also, I'm not sure a comment in a file is the best place for this to be in the long term but we can of course change it at some point. Whilst Cthulhu only supports descending in a single file for now, if it did support multiple files it could get tricky to know which descend to run if there are multiple or to know where which file the descend comment is in.

@MilesCranmer
Copy link
Author

Good points!

I moved this post to discourse btw: https://discourse.julialang.org/t/how-can-we-get-rust-level-feedback-on-type-stability/113497, maybe copy your comment there so others can see it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants