# Please explain syntax

**URL:** <https://discourse.julialang.org/t/please-explain-syntax/16148>\
**Category:** General Usage\
**Tags:** first-steps\
**Created:** [October 11, 2018, 3:48am UTC](https://discourse.julialang.org/t/please-explain-syntax/16148 "2018-10-11T03:48:30Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![govorunov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/govorunov/32/5361_2.png) [@govorunov](https://discourse.julialang.org/u/govorunov)\
**Post date:** [October 11, 2018, 11:21am UTC](https://discourse.julialang.org/t/please-explain-syntax/16148/21 "2018-10-11T11:21:45Z")

</div>

> [@Liso](#):
>
> Do you now something like this (do it in REPL)?
> 
> ```julia
> julia> @edit Char(1)
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [October 11, 2018, 11:28am UTC](https://discourse.julialang.org/t/please-explain-syntax/16148/22 "2018-10-11T11:28:43Z")

</div>

> [@oxinabox](#):
>
> Namespaces prevent you doing dispatch.

Is it really true?

```julia
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! 🙂

```julia
julia> using LinearAlgebra

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

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 11, 2018, 11:40am UTC](https://discourse.julialang.org/t/please-explain-syntax/16148/23 "2018-10-11T11:40:55Z")

</div>

> [@govorunov](#):
>
> So the sophisticated system you are describing works for packages, not modules.

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

> [@govorunov](#):
>
> Yet Julia also has modules for some reason

Yes. The reason is namespace management.

> [@govorunov](#):
>
> I think it is convenient being able to reflect modules structure with file/directory structure within one package/program.

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

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [October 11, 2018, 12:34pm UTC](https://discourse.julialang.org/t/please-explain-syntax/16148/24 "2018-10-11T12:34:30Z")

</div>

> [@govorunov](#):
>
> To my understanding packages and modules are generally different things. One package may contain multiple modules and sub-modules. Can it contain sub-packages?

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](https://discourse.julialang.org/t/test-specific-dependencies/15550).

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [October 11, 2018, 12:48pm UTC](https://discourse.julialang.org/t/please-explain-syntax/16148/25 "2018-10-11T12:48:50Z")

</div>

So if I understand you correctly, you want to organize _your own_ code as follows:

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

```

Then import submodules inside MainModule.jl as:

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

```

And later use then from another package as:

```julia
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.:

```julia
@using_module Submodule1

```

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

---

<div class="post-metadata">

**Author:** ![govorunov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/govorunov/32/5361_2.png) [@govorunov](https://discourse.julialang.org/u/govorunov)\
**Post date:** [October 11, 2018, 10:20pm UTC](https://discourse.julialang.org/t/please-explain-syntax/16148/26 "2018-10-11T22:20:17Z")

</div>

I will do the macro. Thank you!

---

<div class="post-metadata">

**Author:** ![govorunov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/govorunov/32/5361_2.png) [@govorunov](https://discourse.julialang.org/u/govorunov)\
**Post date:** [October 11, 2018, 10:39pm UTC](https://discourse.julialang.org/t/please-explain-syntax/16148/27 "2018-10-11T22:39:26Z")

</div>

One more question, sorry.  
How come

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

```

does not call itself infinitely?

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [October 11, 2018, 11:12pm UTC](https://discourse.julialang.org/t/please-explain-syntax/16148/28 "2018-10-11T23:12:13Z")

</div>

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

```julia
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.

[Previous page](https://discourse.julialang.org/t/please-explain-syntax/16148.md?page=1)
