Mayank Khurana
Mayank Khurana

Reputation: 436

Whats the replacement for typealias in Julia?

julia> typealias
ERROR: UndefVarError: typealias not defined

it's working in Julia:0.5 but is not working in above versions

help?> typealias
search: typealias

  Introduce a new name for an already expressible type. For example, in
  base/boot.jl, UInt is type aliased to either UInt64 or UInt32 as appropriate
  for the size of pointers on the system:

  if is(Int,Int64)
      typealias UInt UInt64
  else
      typealias UInt UInt32
  end

  For parametric types, typealias can be convenient for providing names in
  cases where some parameter choices are fixed. In base for example:

  typealias Vector{T} Array{T,1}

So is there any other function or keyword that can be used and works the same way?

Upvotes: 12

Views: 4014

Answers (3)

Christoph Rackwitz
Christoph Rackwitz

Reputation: 15518

In Julia version 0.5 they still had the typealias keyword.

In version 0.6, that got deprecated (and later removed) in favor of plain assignment.

Compare the sections of these docs:

(official docs.julialang.org for v0.5 appears mangled)

This still holds for the current version (v1.11).

Example from the docs:

# v0.5 and before
if is(Int,Int64)
    typealias UInt UInt64
else
    typealias UInt UInt32
end

# v0.6 and onwards
if Int === Int64
    const UInt = UInt64
else
    const UInt = UInt32
end

Upvotes: 0

user2138149
user2138149

Reputation: 17266

I think it's even simpler than this.

newType = existingType

is sufficient.

Since Types behave a bit like values in Julia, you can just assign them like regular variables.

Upvotes: 0

Cameron Bieganek
Cameron Bieganek

Reputation: 7694

There are two ways to define a type alias, which can be seen in the following example:

julia> const MyVector1 = Array{T,1} where T
Array{T,1} where T

julia> MyVector2{T} = Array{T,1}
Array{T,1} where T

You can see that both of these type aliases are equivalent to the built-in Vector type alias:

julia> Vector
Array{T,1} where T

See Type Aliases and UnionAll Types in the manual for more information.

Upvotes: 16

Related Questions