# How to assist the LSP when it comes to function outputs, and Julia Language "Good Code"

**URL:** https://discourse.julialang.org/t/how-to-assist-the-lsp-when-it-comes-to-function-outputs-and-julia-language-good-code/111770
**Category:** VS Code
**Tags:** type, vscode, languageserver, type-stability, code-organization
**Created:** [March 18, 2024, 1:10pm UTC](https://discourse.julialang.org/t/how-to-assist-the-lsp-when-it-comes-to-function-outputs-and-julia-language-good-code/111770 "2024-03-18T13:10:31Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Qfl3x](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qfl3x/32/16227_2.png) [@Qfl3x](https://discourse.julialang.org/u/Qfl3x)
#### Post date: [March 18, 2024, 1:10pm UTC](https://discourse.julialang.org/t/how-to-assist-the-lsp-when-it-comes-to-function-outputs-and-julia-language-good-code/111770/1 "2024-03-18T13:10:31Z")

</div>

One of the key features of an LSP is auto-completion based on the type.

Suppose this code:

```julia
struct Foo
    x
    y
end
Foo_constructor(i) = Foo(i,i)
a = Foo_constructor(3)

```

Now, the LSP does not recognize `a` as an object of type `Foo`, if you type `a.` you don’t get auto completion, now a simple analysis of the code shows that no matter the `i` given, you’ll always get an object of type `Foo` so it would’ve been nice if that was automatic.

Now suppose this new example here:

```julia
Foo(i) = Foo(i,i)
b = Foo(3)

```

Now, the LSP does recognize `b` as of being of type `Foo`, and you get auto-completion; as `b.` gives the option for both `x` and `y`. This may seem “good”, so “constructors” should preferably be named after their output type.

However, now consider this example: (which I admit is terrible ambiguous code)

```julia
Foo(i::Float64) = 10.
c = Foo(2.3)

```

Now, the LSP thinks that `c` is of type `Foo`! And when prompted for auto-completion it will show `x` and `y` which is erroneous, a fact more apparent if `Foo(...)` gives an object of type `Bar` with different attributes.

When dealing with projects which feature loads of `struct`s (I’m now working on a Compiler after finishing the Interpreter, check out: [GitHub - lucifer1004/MonkeyLang.jl: "Writing an Interpreter in GO" and "Writing a Compiler in GO" in Julia.](https://github.com/lucifer1004/MonkeyLang.jl) for a similar implementation) auto-completion of variables assigned as the output of a function is mighty useful.

Now, how to go forward with this? Not sure, though in my mind a more elaborate static code analyzer might do it.

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [March 18, 2024, 2:57pm UTC](https://discourse.julialang.org/t/how-to-assist-the-lsp-when-it-comes-to-function-outputs-and-julia-language-good-code/111770/2 "2024-03-18T14:57:43Z")

</div>

Yeah, this is a bit tricky. Generally, the closest we can possibly come to a correct answer is to ask the Julia compiler, but even that will fail for sufficiently complex cases.

The fact that constructors are not guaranteed to return their type widely considered to be a mistake though [citation needed].
