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

Type aliases are not printed as aliases if the aliased type is imported from a different module #40448

Open
mobius-eng opened this issue Apr 12, 2021 · 2 comments
Labels
domain:display and printing Aesthetics and correctness of printed representations of objects.

Comments

@mobius-eng
Copy link

The following works as expected:

struct Foo{U,T}
    x::U
    y::T
end

const Foo1{T} = Foo{T, T}

Then in REPL:

julia> Foo1
Foo1{T} where T (alias for Foo{T, T} where T)
julia> Foo(10, 20)
Foo1{Int64}(10, 20)

However, if the aliased type is defined somewhere else, the alias is not shown as alias:

const Tensor3{T} = Array{T, 3}
julia> Tensor3
Array{T, 3} where T
julia> Array{Int64}(undef, 2, 3, 3)
2×3×3 Array{Int64, 3}:
[:, :, 1] =<...>

I would have expected it to show

Tensor3{T} where T (alias for Array{T, 3} where T)

and use Tensor3{Int64} prefix when showing the array.

Tensor3 does act as an alias otherwise:

julia> x = Array{Int64}(undef, 2, 3, 3);
julia> isa(x, Tensor3)
true

julia> bar(m::Tensor3) = m[1,1,1]
bar (generic function with 1 method)

julia> bar(x)
454402320

Julia version:

Julia Version 1.6.0-rc3
Commit 23267f0d46 (2021-03-16 17:04 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, broadwell)
@JeffBezanson
Copy link
Sponsor Member

This is intentional; we only consider aliases defined in the same module as the type itself, so that any package can freely define whatever alias it wants without "pirating" somebody else's printing.

A possible variant might be to consider the :module IO property, so that names imported into the REPL also count.

@JeffBezanson JeffBezanson added the domain:display and printing Aesthetics and correctness of printed representations of objects. label Apr 12, 2021
@mobius-eng
Copy link
Author

This is intentional; we only consider aliases defined in the same module as the type itself, so that any package can freely define whatever alias it wants without "pirating" somebody else's printing.

Ok, I was wondering if it was intentional.

Here is my argument to change the behavior: I want to have a generic interface to "strong" types. Strong types is something one would use to ensure right parameters of type Float64 are passed to a model among 20+ values. In Julia one can also use keyword arguments, but those imply the existence of default values which might not be the case. So, after experimenting a bit I settled for strong types. A generic type may look like (adapted from C++):

struct NamedType{NameType, DataType}
    value::DataType
    NamedType(::Type{NameType}, x::DataType) where {NameType, DataType} =
        new{NameType, DataType}(x)
end

So, then the user can do:

abstract type Length_tag end
const Length{T} = NamedType{Length_tag, T}

in which case I do want the printing of my type NamedType to be hijacked and values of Length{Float64} == NamedType{Length_tag, Float64} be printed as, e. g. Length{Floa64}(1.0). But this doesn't work if NamedType is defined in one module (library) but Length --- in another (user code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
Development

No branches or pull requests

2 participants