Julia language server, return type declarations, type stability

:wave: I’m totally new to Julia, but have some Rust, Zig and Python background.

I am using the Julia Language Server with VSCode. Even for type-stable functions, vscode does not show the return type of functions. And variables which are the results of functional calls, those types are not inferred either, even for type stable functions. (even for simple functions like from Example.jl)

hello(who::String) = "Hello, $who"

I have read in the manual this passage:

Return type declarations are rarely used in Julia: in general, you should instead write “type-stable” functions in which Julia’s compiler can automatically infer the return type. Functions · The Julia Language

So if writing type-stable functions are the best practice, shouldn’t the Language Server provide some information about variable types, and function return types? It’s quite surprising, having used the Language Server implementations for other languages, including Python.

5 Likes

That would be nice, but limited to functions with only one possible input (or output) type (like that of the example). And those are not very common. In general type stability and return types are properties of the funcion and of the input types together.

For example, what should this show?

f(x) = 2x

Or is there a broader picture I’m missing?

1 Like

@lmiq to build on your example:

def f(x):
    return 2 * x

var1 = f(42)
print(var1)

var2 = f(3.14)
print(var2)

var3 = f("greetings")
print(var3)
$ python example.py 
84
6.28
greetingsgreetings

Here are the code help popups which the Python language server displays in VSCode:

# hover on f()
(function) f: (x) -> Any
# hover on var1
(variable) var1: int
# hover on var2:
(variable) var2: float
# hover on var3:
(variable) var3: str
2 Likes

A slightly different example with Python again, but using type hinting

def foo(b: bool) -> Optional[str]:
    if not b:
        return None
    return 'ok'

# hover over foo: 
# (function) foo: (b: bool) -> str | None

var4 = foo(True)
# hover over var4
# (variable) var4: str | None

No doubt there are lots of differences between Python type “hinting” system and Julia’s real type system. I am just trying to wrap my head around how this works :slight_smile:

1 Like

That could definitely work, it would be just the reading of the output of @code_warntype.

The Any of f() is misleading there, if the function is type stable. That should be different (not sure how, though).

1 Like