# How to design a new data type like Complex

**URL:** <https://discourse.julialang.org/t/how-to-design-a-new-data-type-like-complex/70718>\
**Category:** New to Julia\
**Tags:** question, type\
**Created:** [November 1, 2021, 7:29am UTC](https://discourse.julialang.org/t/how-to-design-a-new-data-type-like-complex/70718 "2021-11-01T07:29:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruizhi\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ruizhi_yang/32/10344_2.png) [@Ruizhi\_Yang](https://discourse.julialang.org/u/Ruizhi_Yang)\
**Post date:** [November 1, 2021, 7:29am UTC](https://discourse.julialang.org/t/how-to-design-a-new-data-type-like-complex/70718/1 "2021-11-01T07:29:28Z")

</div>

Hello,

I want to design a new data type that acts like Complex. What’s amazing is that by the identifier “im”, Julia compiler could recognize for example 1+1im as a complex number. That looks like a special form of struct to me.

My need comes from usage habit of people in some specific fields. For example in electricity, people use “L” to denote a inductor and “C” to denote a capacitor. In osscasions that they use these symbols a lot, type in “1e-9L” to denote a inductor that has value=1e-9 is certainly more convenient than input “Inductor(1e-9)”, if the struct Inductor is defined.

I have no idea how to google this. Thank you for any help or hint.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 1, 2021, 7:39am UTC](https://discourse.julialang.org/t/how-to-design-a-new-data-type-like-complex/70718/2 "2021-11-01T07:39:10Z")

</div>

Yes, you can do this yourself. There’s no special parsing for `im`, so you can create your own notation `L` and `C`.

The best way to get started is probably looking at the source of complex. Try typing

```julia
jl> @edit complex(0, 1)

```

to see the source.

---

<div class="post-metadata">

**Author:** ![Ruizhi\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ruizhi_yang/32/10344_2.png) [@Ruizhi\_Yang](https://discourse.julialang.org/u/Ruizhi_Yang)\
**Post date:** [November 1, 2021, 7:47am UTC](https://discourse.julialang.org/t/how-to-design-a-new-data-type-like-complex/70718/3 "2021-11-01T07:47:06Z")

</div>

Thank you very much! That’s exactly what I need.

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [November 1, 2021, 4:21pm UTC](https://discourse.julialang.org/t/how-to-design-a-new-data-type-like-complex/70718/4 "2021-11-01T16:21:28Z")

</div>

> [@DNF](#):
>
> `@edit complex(0, 1)`

Yes, thanks, but this throws an error here:

```julia
julia> @edit complex(0, 1)
ERROR: IOError: could not spawn `code -g 'C:\Users\xxx\AppData\Local\Programs\Julia-1.7.0-rc1\share\julia\base\complex.jl:171'`: no such file or directory (ENOENT)
Stacktrace:
  [1] _spawn_primitive(file::String, cmd::Cmd, stdio::Vector{Any})
    @ Base .\process.jl:100
  [2] #690
    @ .\process.jl:113 [inlined]
  [3] setup_stdios(f::Base.var"#690#691"{Cmd}, stdios::Vector{Any})
    @ Base .\process.jl:197
  [4] _spawn
    @ .\process.jl:112 [inlined]
  [5] _spawn(::Base.CmdRedirect, ::Vector{Any})
    @ Base .\process.jl:140
  [6] run(::Base.CmdRedirect; wait::Bool)
    @ Base .\process.jl:449
  [7] (::InteractiveUtils.var"#2#3"{Bool, InteractiveUtils.var"#11#21", Vector{String}})(cmd::Cmd, path::String, line::Int32)
    @ InteractiveUtils C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.7\InteractiveUtils\src\editless.jl:98
  [8] edit(path::String, line::Int32)
    @ InteractiveUtils C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.7\InteractiveUtils\src\editless.jl:205
  [9] edit(f::Any, t::Any)
    @ InteractiveUtils C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.7\InteractiveUtils\src\editless.jl:234
 [10] top-level scope
    @ REPL[57]:1

```

complex.jl exists in

```julia
C:\Users\xxx\AppData\Local\Programs\Julia-1.7.0-rc1\share\julia\test
C:\Users\xxx\AppData\Local\Programs\Julia-1.7.0-rc1\share\julia\base

```

Opened another [thread](https://discourse.julialang.org/t/edit-error-ioerror-could-not-spawn-code-g-c-users/70752).

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [November 1, 2021, 5:57pm UTC](https://discourse.julialang.org/t/how-to-design-a-new-data-type-like-complex/70718/5 "2021-11-01T17:57:22Z")

</div>

> [@Ruizhi\_Yang](#):
>
> What’s amazing is that by the identifier “im”, Julia compiler could recognize for example 1+1im as a complex number. That looks like a special form of struct to me.

There’s really nothing special about `im`. It’s literally just defined as `Complex(false, true)`:

> <https://github.com/JuliaLang/julia/blob/31b9fd2d5960d1e24348235bb2a8d36e6d194612/base/complex.jl#L36>

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 1, 2021, 6:28pm UTC](https://discourse.julialang.org/t/how-to-design-a-new-data-type-like-complex/70718/6 "2021-11-01T18:28:12Z")

</div>

> [@Ruizhi\_Yang](#):
>
> What’s amazing is that by the identifier “im”, Julia compiler could recognize for example 1+1im as a complex number. That looks like a special form of struct to me.

In addition to what @simeonschaub said, the extra piece of information is that Julia allows you to write `3im` and get `3 * im`. This works for any variable name, so `im` is not special:

```julia
julia> x = 2
2

julia> 3x
6

```

---

<div class="post-metadata">

**Author:** ![Ruizhi\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ruizhi_yang/32/10344_2.png) [@Ruizhi\_Yang](https://discourse.julialang.org/u/Ruizhi_Yang)\
**Post date:** [November 2, 2021, 2:36am UTC](https://discourse.julialang.org/t/how-to-design-a-new-data-type-like-complex/70718/7 "2021-11-02T02:36:26Z")

</div>

That’s right, I looked into the source code and found this as well. Thanks 🙂

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [November 2, 2021, 7:20am UTC](https://discourse.julialang.org/t/how-to-design-a-new-data-type-like-complex/70718/8 "2021-11-02T07:20:51Z")

</div>

You might want to check out [https://github.com/PainterQubits/Unitful.jl](https://github.com/PainterQubits/Unitful.jl) as well.

---

<div class="post-metadata">

**Author:** ![Ruizhi\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ruizhi_yang/32/10344_2.png) [@Ruizhi\_Yang](https://discourse.julialang.org/u/Ruizhi_Yang)\
**Post date:** [November 2, 2021, 7:27am UTC](https://discourse.julialang.org/t/how-to-design-a-new-data-type-like-complex/70718/9 "2021-11-02T07:27:34Z")

</div>

That seems to be an ideal solution, I think I’ll switch my code to Unitful.jl at some point.
