Skip to content

possible changes to inner constructors #8135

Closed
@mlhetland

Description

@mlhetland

I'm trying to create a type whose inner constructor does some adjustment/normalization, and at the same time I want to deduce type parameters from the constructor arguments. And I'm having trouble making that work…

immutable A{N}
    x::NTuple{N, Int}
    y::Int
    A(x, y) = new(x, y + 1)
end

#a = A((1, 2), 3)       # Doesn't work
a = A{2}((1, 2), 3)     # Works (w/cumbersome param)
println(a)

immutable B{N} 
    x::NTuple{N, Int}
    y::Int
end

#B(x, y) = B(x, y + 1)  # Stack overflow...
b = B((1, 2), 3)        # Works (w/wrong answer)
println(b)

The following works, and it solves my problem, but I don't quite understand why it should be necessary, and I wonder if it might be a bug that it is?

immutable A{N}
    x::NTuple{N, Int}
    y::Int
    A(x, y) = new(x, y + 1)
end

A{N}(x::NTuple{N, Int}, y::Int) = A{N}(x, y)

As @tknopp pointed out, it seems the default constructor is removed.

Could I somehow override the more specific default constructor (which doesn't have Any-arguments), and supply the proper type parameter to new or something? (Haven't been able to make that work.)

(See also discussion on Julia-Users.)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions