# Hard aliasing for types

**URL:** <https://discourse.julialang.org/t/hard-aliasing-for-types/7303>\
**Category:** General Usage\
**Created:** [November 25, 2017, 11:20am UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303 "2017-11-25T11:20:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikolajpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikolajpp/32/2742_2.png) [@mikolajpp](https://discourse.julialang.org/u/mikolajpp)\
**Post date:** [November 25, 2017, 11:20am UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/1 "2017-11-25T11:20:14Z")

</div>

Hello there,

Is it possible to hard alias primitive types? For example,  
I would like to have type MyNumber, on which I will do dispatch for custom arithmetic operators.  
The obvious solution is

struct MyNumber n::Number end

Suppose I will do heavy calculations with this type. Is it the most efficient way?  
Or is something like  
“alias MyNumber = Number”  
possible, where MyNumber becomes a distinct type.  
The “const MyNumber = Number” obviously does not work like that.

---

<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:** [November 25, 2017, 11:45am UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/2 "2017-11-25T11:45:33Z")

</div>

You are conflating two concepts: _aliasing_ and _wrapping_.

_Alias_ a type as

```julia
const Type1 = Type2

```

From then on, they will be synonyms, ie `Type1 === Type2`. Consequently, methods that work for one will work for the other (because it is the same).

_Wrap_ a type as

```julia
struct MyWrapper{T}
    value::T 
end

```

The type parameter is [necessary for performance](https://docs.julialang.org/en/latest/manual/performance-tips/#Avoid-containers-with-abstract-type-parameters-1).

You will need to define the relevant functions, forwarding them to the value and possibly rewrapping. For some types (eg numbers), there are libraries that make this easier (just be more specific on what you want).

AFAIK there is no solution which would avoid the need to define the methods yet make the types distinct.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 25, 2017, 12:20pm UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/3 "2017-11-25T12:20:48Z")

</div>

> **[GitHub - SimonDanisch/AbstractNumbers.jl: Define your own number types in...](https://github.com/SimonDanisch/AbstractNumbers.jl)**
>
> Define your own number types in Julia super easily! - GitHub - SimonDanisch/AbstractNumbers.jl: Define your own number types in Julia super easily!

---

<div class="post-metadata">

**Author:** ![mikolajpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikolajpp/32/2742_2.png) [@mikolajpp](https://discourse.julialang.org/u/mikolajpp)\
**Post date:** [November 25, 2017, 1:41pm UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/4 "2017-11-25T13:41:22Z")

</div>

Yes I know my solution is not aliasing, hence I asked  
if there is another way.

I never intended to have untyped wrapper.

To be more specific, I am writing an algebra system for Julia.  
Since I have to capture expressions for delayed computations and/or transformations  
I defined several types such as

```julia
struct MySymbol
      s::Symbol
end

```

and so on.

What you are suggesting is basically

```julia
type MyWrapper{T}
       value::T 
end

const MySymbol = MyWrapper{Symbol} 

```

but I fail to see any difference.  
The performance issues comes when dealing with untyped fields, which  
is not the case here.

---

<div class="post-metadata">

**Author:** ![mikolajpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikolajpp/32/2742_2.png) [@mikolajpp](https://discourse.julialang.org/u/mikolajpp)\
**Post date:** [November 25, 2017, 1:41pm UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/5 "2017-11-25T13:41:35Z")

</div>

Thanks Kris,

Nonetheless I need more than numbers. I have my own Symbol, Expr, Number, and String  
which have to be captured for doing algebra.

Anyway, it is not like it is a lot of hassle, just a couple of additional lines.

---

<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:** [November 25, 2017, 1:56pm UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/6 "2017-11-25T13:56:42Z")

</div>

> [@mikolajpp](#):
>
> The performance issues comes when dealing with untyped fields, which is not the case here.

But it was, in your original example (`Number` is an abstract type).

If you are defining several wrapper types with similar but occasionally different behavior, consider defining a single wrapper type and specializing on parameter types for behavior that is different. Whether this simplifies your code is of course something only you can tell.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 25, 2017, 4:19pm UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/7 "2017-11-25T16:19:54Z")

</div>

x-ref: [https://github.com/JuliaLang/julia/issues/9821](https://github.com/JuliaLang/julia/issues/9821)

---

<div class="post-metadata">

**Author:** ![mikolajpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikolajpp/32/2742_2.png) [@mikolajpp](https://discourse.julialang.org/u/mikolajpp)\
**Post date:** [November 25, 2017, 4:30pm UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/8 "2017-11-25T16:30:59Z")

</div>

Maybe I misunderstand something.

So there is a difference between

```julia
MyWrapper{Number}

```

and my original code?

As I understand that doc, it says we should avoid abstract types  
when we can specialize. But in my case, I can not do that.  
I need a “new” Number type, and obviously can not specialize.

---

<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:** [November 25, 2017, 4:51pm UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/9 "2017-11-25T16:51:45Z")

</div>

I thought you meant the type `Base.Number`, while now it is apparent that you were using it as a placeholder for a type name.

---

<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 25, 2017, 4:52pm UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/10 "2017-11-25T16:52:51Z")

</div>

It’s a little hard to tell exactly what you want, but if the field inside your type is always a `Symbol`, then it’s true that you don’t need a parameterized type; you can just do:

```julia
struct MySymbol
  x::Symbol 
end

```

and if you want your type to be dispatched like a `Number`, you can do:

```julia
struct MySymbol <: Number
  x::Symbol 
end

```

---

<div class="post-metadata">

**Author:** ![mikolajpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikolajpp/32/2742_2.png) [@mikolajpp](https://discourse.julialang.org/u/mikolajpp)\
**Post date:** [November 25, 2017, 5:21pm UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/11 "2017-11-25T17:21:32Z")

</div>

Okay, to be more specific here is the code I went with:

```julia
"An abstract symbolic expression"
abstract type ASymbolic end;

type AWrapper{T} <: ASymbolic
    val::T
end

# Need our own dispatch on types
const ANumber = AWrapper{Number}
const AVar = AWrapper{Symbol}
const AExpr = AWrapper{Expr}

"Result of symbolic computation"
const AResult = Union{ANumber, AVar, AExpr}

```

It is part of computer algebra system which is trying to leverage full of julia, instead  
of just being a wrapper to some other library.

Thanks for your replies.

---

<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 25, 2017, 5:42pm UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/12 "2017-11-25T17:42:35Z")

</div>

> `const ANumber = AWrapper{Number}`

This will perform poorly because `Number` is an abstract type: [https://docs.julialang.org/en/stable/manual/performance-tips/#Avoid-fields-with-abstract-type-1](https://docs.julialang.org/en/stable/manual/performance-tips/#Avoid-fields-with-abstract-type-1)

You could instead do:

```julia
julia> const ANumber = AWrapper{T} where T <: Number

julia> AWrapper{Int} <: ANumber
true

julia> AWrapper{Float64} <: ANumber
true

```

---

<div class="post-metadata">

**Author:** ![mikolajpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikolajpp/32/2742_2.png) [@mikolajpp](https://discourse.julialang.org/u/mikolajpp)\
**Post date:** [November 26, 2017, 5:31am UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/13 "2017-11-26T05:31:22Z")

</div>

Okay, then I might just deal with two numeric types in symbolic expressions as well. Somehow I thought  
it would complicate the code more, but it is really just a matter of defining another type and doing some additional testing.

For example, when detecting expression of the form

```julia
coef1*x + coef2*x

```

The new test for factorization would just need to look for types

```julia
if typeof(coef1) <: Number 
 ...

```

Thanks to wonderful macros  
a lot of operators are generated automatically anyway.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [November 26, 2017, 6:57am UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/14 "2017-11-26T06:57:14Z")

</div>

As a suggestion, if you want to try to leverage all of Julia, I think something like [Reduce](https://github.com/chakravala/Reduce.jl) is the way to go.

For example, if you want to calculate partial Normal moments

```julia
julia> using Reduce, Compat, SpecialFunctions
Reduce (Free PSL version, revision 4218), 22-Sep-2017 ...

julia> Reduce.Rational(false);

julia> @generated function normal_moment(x, ::Val{N} = Val{0}()) where N
           ex = int(:( x^$N * exp(-x^2/2 ) ), :x)
           root_2π = sqrt(2π)
           :( $ex / $root_2π )
       end

julia> normal_moment(0)
0.0

julia> normal_moment(0.5)
0.19146246127401312

julia> normal_moment(0.5, Val(2))
0.015429797891863349

julia> normal_moment(1.5, Val(2))
0.23891640523230437

julia> normal_moment(10, Val(4)) ###Pick a big number to get the kurtosis of a standard normal.
1.5000000000000002

```

This could be useful if you wanted to integrate [Hermite polynomials](https://en.wikipedia.org/wiki/Hermite_polynomials).

If you like statistics, you can also have a lot of fun with moment generating functions:

```julia
julia> @generated function gamma_moment(a, b, ::Val{N} = Val{0}()) where N
           ex = :((1 - t/b)^-a)
           for i ∈ 1:N
               ex = df( ex, :t )
           end
           quote
               t = 0
               $ex
           end
       end
gamma_moment (generic function with 2 methods)

julia> gamma_moment(2, 2, Val(1)) ##The mean
1.0

julia> gamma_var(α, β) = gamma_moment(α, β, Val(2)) - gamma_moment(α, β, Val(1))
1.5

julia> gamma_var(2, 2)
0.5

julia> gamma_var(3.4, 1.7)
1.1764705882352953

julia> gamma_var(4, 2)
1.0

```

Be warned though that if the Val you’re using isn’t known at compile time, the code wont be type stable.

---

<div class="post-metadata">

**Author:** ![mikolajpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikolajpp/32/2742_2.png) [@mikolajpp](https://discourse.julialang.org/u/mikolajpp)\
**Post date:** [November 26, 2017, 12:50pm UTC](https://discourse.julialang.org/t/hard-aliasing-for-types/7303/15 "2017-11-26T12:50:56Z")

</div>

Thanks Elrod, this is helpful.

I will definitely have a look at this package. Just by superficially looking, it is unclear to me  
if this package can be used to achieve, ex. quickly implementing rules for doing computations ex. in a Fock space, or any other type of symbolic tasks.  
Also it seems it is just an interface to some external program, while I want it to be 100% julia for maximum hackability.

I looked through the code, and the approach is pretty much the same I want to take.  
I also parse expression using julia AST capabilities and so on.
