Julia: Type aliasing to shorten long type signatures while defining struct type

Created on 7 May 2019  路  3Comments  路  Source: JuliaLang/julia

I'm defining a large structure like this

struct A{T<:Number}
  a::Matrix{T}
  b::Int
  c::Matrix{T}
end

In which I'd be happy to alias Matrix{T} to MT and limit this behavior in the scope of the definition. Simply I want to write code in the way of

struct A{T<:Number}
  MT=Matrix{T}
  a::MT
end

Can I make it within the current syntax?

feature

Most helpful comment

FWIW, this does currently work:

julia> let
          MT = Matrix{S} where S
          struct A{T<:Number}
            a::MT{T}
          end
       end

All 3 comments

This is tricky, since then it becomes harder to tell which statements specify field names. It also conflicts with possible default value syntax (#10146) (and with the existing @kwdef). So this seems unlikely.

FWIW, this does currently work:

julia> let
          MT = Matrix{S} where S
          struct A{T<:Number}
            a::MT{T}
          end
       end

@vtjnash It's exactly what I want. It surprises me that the struct definition is kept outside the let scope. I've been thinking of this way but given up too early. Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arshpreetsingh picture arshpreetsingh  路  3Comments

StefanKarpinski picture StefanKarpinski  路  3Comments

wilburtownsend picture wilburtownsend  路  3Comments

TotalVerb picture TotalVerb  路  3Comments

Keno picture Keno  路  3Comments