Please explain syntax

This is a cool feature, but it does not help to organize code. Besides it only works when Char(::Number) is already in context. Not all development is done in REPL. Try writing a web server and you will soon realize virtues of a fully functioning debugger, etc.

1 Like

Is it really true?

julia> import LinearAlgebra

julia> Base.show(a::LinearAlgebra.SingularException) = "A"

julia> show(LinearAlgebra.SingularException(1))
"A"

And you still could pollute global namespaces with using if you like! :slight_smile:

julia> using LinearAlgebra

julia> show(SingularException(1))
"A"

It works for code loading, via packages, which contain modules. Did you read the documentation I linked above? Specifically, in Julia, a package is an

an independent, reusable collection of Julia code, wrapped in a module

Yes. The reason is namespace management.

Sure. Use submodules. Eg see code in Base for a real-world use case.

Actually yes, there is a concept of sub-projects, which have their own sets of dependencies, and can contain their own top-level modules. We are using this for our test code – it’s its own project, with its own dependencies, including a dependency on the main package, and contains its own top-level module. By making it a sub-project, we can achieve this while keeping it all in the same git repository, and without including the test-specific dependencies in the main package. Although this technique works decently well, it feels a bit under-developed at the moment. More details in this post.

So if I understand you correctly, you want to organize your own code as follows:

src/
  |-- MainModule.jl
  |-- Submodule1.jl
  |-- Submodule2.jl

Then import submodules inside MainModule.jl as:

using Submodul1
# instead of 
# include("Submodule1.jl")
# using Submodule1

And later use then from another package as:

using MainModule.Submodule1

If so, then you can already do it with the only exception of include/using pair which can be combined into a macro, e.g.:

@using_module Submodule1

If it’s not what you want, please describe exactly the workflow you want.

1 Like

I will do the macro. Thank you!

1 Like

One more question, sorry.
How come

(::Type{T})(x::Number) where {T<:AbstractChar} = T(UInt32(x))

does not call itself infinitely?

In this case it is because there exists a more specific method for arguments of type UInt32. To see this consider

julia> @which Char(1)
(::Type{T})(x::Number) where T<:AbstractChar in Base at char.jl:48

julia> @which Char(UInt32(1))
Char(u::UInt32) in Base at char.jl:146

It does mean that you have to be careful when creating your own subtypes of AbstractChar to create all the required methods or you will, as you say, get an infinite loop.

1 Like