JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.02k stars 5.43k forks source link

[FR] user-facing type inference API #54207

Open nsajko opened 2 months ago

nsajko commented 2 months ago

Many packages, such as Dictionaries.jl or StaticArrays.jl, seemingly need access to Julia's type inference functionality to be able to offer their users convenient construction of their custom data structures from generators. This is how comprehensions that create Arrays work, after all, so it only seems fair to enable user packages to use type inference, too.

Exposing type inference to users is, however, dangerous; because the type inference works on a best-effort basis, it's not reliably exact, so users must not rely on the exactness of the type inference for correctness of their code and APIs. A peculiar fact is that type inference may give different results for the same queries at different points during the run of Julia. These issues are subtle, and users are overconfident.

Functionality like Core.Compiler.return_type, Base.return_types or Base.@default_eltype is widely used in the ecosystem, even though these are Julia-internal implementation details not subject to any compatibility guarantee.

Could we have a user-facing type inference API, provided it came with enough documentation and warnings to enable it to be used correctly?

Realistically, the type inference functionality is already widely (mis)used, so IMO it's clear that such documentation is necessary. And, once the docs exist, I guess that providing an interface that's actually permitted for use would be a no-brainer?

Tortar commented 2 months ago

Probably then I was overconfident in https://github.com/JuliaLang/julia/pull/54208 :D

vtjnash commented 2 months ago

I think you are overlooking Base.promote_op, which is the documented way to do this, while many of those others are private or interactive-only APIs

vtjnash commented 2 months ago

This is how comprehensions that create Arrays work, after all

It may be worth noting that this is only for the empty case. Normally comprehensions do not involve inference to create Arrays.

nsajko commented 2 months ago

I think you are overlooking Base.promote_op, which is the documented way to do this

True. However, Base.promote_op is not public, and it's doc string is not in the Manual. Should that be changed? It's mentioned in a section in the Methods page in the Manual, however that section seems to contradict the doc string of Base.promote_op somewhat regarding the correct way of using Base.promote_op.

It may be worth noting that this is only for the empty case. Normally comprehensions do not involve inference to create Arrays.

TBH then I have quite big misunderstandings here. I guess I'll read the code in Base to see how to do this in the nonempty case.