Type parameters and inner constructors in julia 0.6

Viewed 1169

I am in doubt about how to restrict type parameters for parametric types with abstract types in julia 0.6, using the where syntax.

Consider the example where I want to make a parametric abstract type that takes integers, and define structs inheriting from that. If I try:

abstract type AbstractFoo{T} where T<: Integer end

it fails, but instead I can use the non-where syntax

abstract type AbstractFoo{T<:Integer} end
  1. Is this the recommended format?

Given this, how do I implement my subtype

mutable struct Foo{T} <: AbstractFoo{T} where T <: Integer
   bar::T
end

fails too (Invalid subtyping). I can bypass the where syntax again with

mutable struct Foo{T<:Integer} <: AbstractFoo{T}
   bar::T
end

But that seems to be redundant (because T is already restricted to be an Integer). 2. Could I leave it out?:

mutable struct Foo{T} <: AbstractFoo{T}
    bar::T
end

Finally, with the deprecation of inner constructor syntax, is there any way around defining the inner constructor as:

mutable struct Foo{T} <: AbstractFoo{T}
    bar::T
    Foo{T}(x::T) where T = new(x)
end

This makes Foo(3) impossible - requiring me to use Foo{Int}(3). 3. Is this intentional or is there a better way around this? EDIT: I guess for the inner constructor question I can always define an outer constructor Foo(x::T) where {T} = Foo{T}(x).

2 Answers
Related