# \#constructors

**URL:** https://discourse.julialang.org/tag/constructors/739.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Create \`BigInt\` directedly from MPZ pointer](https://discourse.julialang.org/t/create-bigint-directedly-from-mpz-pointer/138903)

<div class="topic-metadata">

**Author:** [@Chrysoberyl](https://discourse.julialang.org/u/Chrysoberyl)\
**Replies:** 6\
**Last updated:** [August 25, 2026, 5:52am UTC](https://discourse.julialang.org/t/create-bigint-directedly-from-mpz-pointer/138903 "2026-08-25T05:52:11Z")

</div>

I have some FFI code that interfaces with another library, and the target library has GNU multiprecision integers. Is it possible to create a BigInt directly from such a multiprecision integer pointer? The default constr…

---

## [World age warning (no @eval) on constructor of different dispatch](https://discourse.julialang.org/t/world-age-warning-no-eval-on-constructor-of-different-dispatch/135947)

<div class="topic-metadata">

**Author:** [@cnaak](https://discourse.julialang.org/u/cnaak)\
**Replies:** 5\
**Last updated:** [March 2, 2026, 2:51pm UTC](https://discourse.julialang.org/t/world-age-warning-no-eval-on-constructor-of-different-dispatch/135947 "2026-03-02T14:51:51Z")

</div>

Updated to julia-1.12 and started a new package. As soon as I defined my first struct with more than one external constructor (nothing fancy, no @evals), I’ve got the world age warning (future error). MWE: struct examp…

---

## [Conversion of inner "flag" type parameters from supertype "flag" type parameters for composite struct construcor](https://discourse.julialang.org/t/conversion-of-inner-flag-type-parameters-from-supertype-flag-type-parameters-for-composite-struct-construcor/131306)

<div class="topic-metadata">

**Author:** [@kunzaatko](https://discourse.julialang.org/u/kunzaatko)\
**Replies:** 3\
**Last updated:** [August 2, 2025, 1:08pm UTC](https://discourse.julialang.org/t/conversion-of-inner-flag-type-parameters-from-supertype-flag-type-parameters-for-composite-struct-construcor/131306 "2025-08-02T13:08:17Z")

</div>

I have a type structure that looks something like this: abstract type AbstractA{T} end struct A1{T} \<: AbstractA{T} end # no field of type \`T\` A1() = A1{Any}() struct A2{T} \<: AbstractA{T} end A2() = A2{Any}() and I wo…

---

## [Struct with read-only and write-only properties](https://discourse.julialang.org/t/struct-with-read-only-and-write-only-properties/129179)

<div class="topic-metadata">

**Author:** [@mattsignorelli](https://discourse.julialang.org/u/mattsignorelli)\
**Replies:** 7\
**Last updated:** [May 21, 2025, 7:15pm UTC](https://discourse.julialang.org/t/struct-with-read-only-and-write-only-properties/129179 "2025-05-21T19:15:16Z")

</div>

Suppose I have the following mutable struct @kwdef mutable struct Rectangle{T} x::T = 0 y::T = 0 function Rectangle(args...) return new{promote\_type(typeof.(args)...)}(args...) end end I would like for this…

---

## [Limitations of \`Base.convert\` for the construction of parametric types](https://discourse.julialang.org/t/limitations-of-base-convert-for-the-construction-of-parametric-types/127977)

<div class="topic-metadata">

**Author:** [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Replies:** 4\
**Last updated:** [April 11, 2025, 9:35pm UTC](https://discourse.julialang.org/t/limitations-of-base-convert-for-the-construction-of-parametric-types/127977 "2025-04-11T21:35:25Z")

</div>

I have a couple of user-defined types in which one contains the other, as in the following minimal example: struct MyType x::Float64 end struct Container c::MyType end In this example, MyType is some kind of F…

---

## [Parametric structures and Inner constructors that don't depend on the types](https://discourse.julialang.org/t/parametric-structures-and-inner-constructors-that-dont-depend-on-the-types/127459)

<div class="topic-metadata">

**Author:** [@Maucejo](https://discourse.julialang.org/u/Maucejo)\
**Replies:** 2\
**Last updated:** [March 28, 2025, 5:24pm UTC](https://discourse.julialang.org/t/parametric-structures-and-inner-constructors-that-dont-depend-on-the-types/127459 "2025-03-28T17:24:05Z")

</div>

Hi all, I apologize for the title which is not clear at all, but I don’t know how to properly express my need concisely. I currently try to define a parametric struct for which the inner constructor doesn’t depend on th…

---

## [Subtyped parameters in constructors](https://discourse.julialang.org/t/subtyped-parameters-in-constructors/127287)

<div class="topic-metadata">

**Author:** [@Deduction42](https://discourse.julialang.org/u/Deduction42)\
**Replies:** 4\
**Last updated:** [March 24, 2025, 12:01am UTC](https://discourse.julialang.org/t/subtyped-parameters-in-constructors/127287 "2025-03-24T00:01:01Z")

</div>

I was playing around with vague constructors and I noticed some very interesting and useful behavior of arrays. I can use “subtype” notation in array constructors! julia\> S = Diagonal(randn(5)) 5×5 Diagonal{Float64, Vec…

---

## [Better way to get the corresponding constructor from composite-type instances?](https://discourse.julialang.org/t/better-way-to-get-the-corresponding-constructor-from-composite-type-instances/122661)

<div class="topic-metadata">

**Author:** [@frankwswang](https://discourse.julialang.org/u/frankwswang)\
**Replies:** 4\
**Last updated:** [November 15, 2024, 3:27pm UTC](https://discourse.julialang.org/t/better-way-to-get-the-corresponding-constructor-from-composite-type-instances/122661 "2024-11-15T15:27:25Z")

</div>

MWE: julia\> abstract type MyType{T} \<: Any end julia\> struct myT1{T} \<: MyType{T} a::T end julia\> struct myT2{T} \<: MyType{T} a::T end julia\> getBuilder(a::MyType) = getfield(Main, (nameof…

---

## [Any way to skip a constructor for Accessors.jl-like syntax?](https://discourse.julialang.org/t/any-way-to-skip-a-constructor-for-accessors-jl-like-syntax/122678)

<div class="topic-metadata">

**Author:** [@Benny](https://discourse.julialang.org/u/Benny)\
**Replies:** 3\
**Last updated:** [November 15, 2024, 3:24pm UTC](https://discourse.julialang.org/t/any-way-to-skip-a-constructor-for-accessors-jl-like-syntax/122678 "2024-11-15T15:24:45Z")

</div>

So this internal :new expression seems to be working again. I’m wondering if it’s possible to unsafely skip inner constructors’ validation to instantiate tweaks to existing instances with setting syntax like Accessors.jl…

---

## [New blog post about Julia parametric types and constructors](https://discourse.julialang.org/t/new-blog-post-about-julia-parametric-types-and-constructors/120717)

<div class="topic-metadata">

**Author:** [@Maurizio\_Tomasi](https://discourse.julialang.org/u/Maurizio_Tomasi)\
**Replies:** 11\
**Last updated:** [October 8, 2024, 7:53am UTC](https://discourse.julialang.org/t/new-blog-post-about-julia-parametric-types-and-constructors/120717 "2024-10-08T07:53:34Z")

</div>

Hi all, I have published the first part of a two-part blog post describing how Julia’s parametric constructor works. I wrote this because several of my students use Julia to implement simulation codes, and I have found …

---

## [Constructors for parametric types](https://discourse.julialang.org/t/constructors-for-parametric-types/119971)

<div class="topic-metadata">

**Author:** [@Maurizio\_Tomasi](https://discourse.julialang.org/u/Maurizio_Tomasi)\
**Replies:** 5\
**Last updated:** [September 28, 2024, 8:52am UTC](https://discourse.julialang.org/t/constructors-for-parametric-types/119971 "2024-09-28T08:52:11Z")

</div>

Hi all, I am studying how Julia handles constructors in parametric types and have found a puzzling behavior. Consider this trivial type: struct Test1{R \<: Real} x::R end I can instantiate Test1 either with or with…

---

## [Differing construction and conversion for subtypes of Number](https://discourse.julialang.org/t/differing-construction-and-conversion-for-subtypes-of-number/119886)

<div class="topic-metadata">

**Author:** [@brainandforce](https://discourse.julialang.org/u/brainandforce)\
**Replies:** 0\
**Last updated:** [September 26, 2024, 12:18am UTC](https://discourse.julialang.org/t/differing-construction-and-conversion-for-subtypes-of-number/119886 "2024-09-26T00:18:48Z")

</div>

After reading a few threads here, I’ve come to understand that for subtypes of Number, construction and conversion should have nearly identical behavior (with one exception: it is expected that construction always return…

---

## [Bypass inner constructor / call \`new\` outside inner constructor](https://discourse.julialang.org/t/bypass-inner-constructor-call-new-outside-inner-constructor/112142)

<div class="topic-metadata">

**Author:** [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Replies:** 10\
**Last updated:** [March 27, 2024, 4:20pm UTC](https://discourse.julialang.org/t/bypass-inner-constructor-call-new-outside-inner-constructor/112142 "2024-03-27T16:20:18Z")

</div>

I have defined a struct with an inner constructor that enforces some invariants, similar to the example in the Documentation julia\> struct OrderedPair x::Real y::Real OrderedPair(x,y) =…

---

## [How to build typed StructArrays from NTuples](https://discourse.julialang.org/t/how-to-build-typed-structarrays-from-ntuples/112023)

<div class="topic-metadata">

**Author:** [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Replies:** 0\
**Last updated:** [March 23, 2024, 4:34pm UTC](https://discourse.julialang.org/t/how-to-build-typed-structarrays-from-ntuples/112023 "2024-03-23T16:34:52Z")

</div>

In the documentation of StructArrays, the Advanced techniques · StructArrays section describes how custom structs could be passed to a StructArray constructor by overloading component, createinstance and staticschema. H…

---

## [Type parameters & constructors](https://discourse.julialang.org/t/type-parameters-constructors/111874)

<div class="topic-metadata">

**Author:** [@mdsalerno](https://discourse.julialang.org/u/mdsalerno)\
**Replies:** 3\
**Last updated:** [March 21, 2024, 1:55am UTC](https://discourse.julialang.org/t/type-parameters-constructors/111874 "2024-03-21T01:55:10Z")

</div>

I’ve defined the following type: struct ComplexNumber{T\<:Real} \<: Number re::T im::T end I am having trouble understanding why the following two outer constructors behave differently: ComplexNumber(x::Real, y:…

---

## [@kwdef kwargs](https://discourse.julialang.org/t/kwdef-kwargs/109853)

<div class="topic-metadata">

**Author:** [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Replies:** 2\
**Last updated:** [February 8, 2024, 12:45am UTC](https://discourse.julialang.org/t/kwdef-kwargs/109853 "2024-02-08T00:45:25Z")

</div>

Could there be a flag added to @kwdef (say @kwargs) that adds a kwargs... to the constructor inputs so that @kwdef @kwargs struct X a b end X(; a=1, b=2, c=3 ) Would work the same as X(; a=1, b=2 ) i.e. a…

---

## [Should Any have an identity constructor method?](https://discourse.julialang.org/t/should-any-have-an-identity-constructor-method/109500)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 10\
**Last updated:** [January 31, 2024, 10:31am UTC](https://discourse.julialang.org/t/should-any-have-an-identity-constructor-method/109500 "2024-01-31T10:31:00Z")

</div>

Many non-concrete types have constructors, for example we have AbstractFloat(0.3) == 0.3 and Union{Int,Float64}(0.3) == 0.3. Should we also have a method like Any(x) = x? Then we would have map(Any, \[3,4,5\]) == map(ident…

---

## [Creating Circular Referencing Structs](https://discourse.julialang.org/t/creating-circular-referencing-structs/107933)

<div class="topic-metadata">

**Author:** [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Replies:** 3\
**Last updated:** [December 22, 2023, 12:24am UTC](https://discourse.julialang.org/t/creating-circular-referencing-structs/107933 "2023-12-22T00:24:48Z")

</div>

This works abstract type Abstract\_B end mutable struct A obj::Abstract\_B function A() x = new() x.obj = B(x) end end struct B\<:Abstract\_B obj::A end Can it be done without t…

---

## [Best practices for defining inner constructors for parametric types](https://discourse.julialang.org/t/best-practices-for-defining-inner-constructors-for-parametric-types/106494)

<div class="topic-metadata">

**Author:** [@brainandforce](https://discourse.julialang.org/u/brainandforce)\
**Replies:** 3\
**Last updated:** [November 20, 2023, 11:31pm UTC](https://discourse.julialang.org/t/best-practices-for-defining-inner-constructors-for-parametric-types/106494 "2023-11-20T23:31:57Z")

</div>

In much of my development, I have parametric types that need inner constructors to be defined with some validation code: struct MyType{T\<:Number} a::T b::T # Inner constructor goes here. end However, I’m no…

---

## [Outer constructor not getting called despite being in stack trace?](https://discourse.julialang.org/t/outer-constructor-not-getting-called-despite-being-in-stack-trace/103801)

<div class="topic-metadata">

**Author:** [@deaminase](https://discourse.julialang.org/u/deaminase)\
**Replies:** 3\
**Last updated:** [September 13, 2023, 6:42am UTC](https://discourse.julialang.org/t/outer-constructor-not-getting-called-despite-being-in-stack-trace/103801 "2023-09-13T06:42:50Z")

</div>

Somehow Julia’s constructors manage to consistently confuse me. Here is a pared-down example of what I’m trying and failing to do: const Optional{T} = Union{Some{T}, Nothing} struct State{InpIterState,Input} full\_inp…

---

## [Inner and Outer constructors with Parameters.jl prevent incremental compilation?](https://discourse.julialang.org/t/inner-and-outer-constructors-with-parameters-jl-prevent-incremental-compilation/103061)

<div class="topic-metadata">

**Author:** [@Iddingsite](https://discourse.julialang.org/u/Iddingsite)\
**Replies:** 2\
**Last updated:** [August 22, 2023, 4:21pm UTC](https://discourse.julialang.org/t/inner-and-outer-constructors-with-parameters-jl-prevent-incremental-compilation/103061 "2023-08-22T16:21:49Z")

</div>

Hi all! I am trying to familiarise myself with outer and inner constructors and I would like to know if I am doing something wrong. I wrote a structure with 1 outer and 1 inner constructor, using also Parameters and Uni…

---

## [Constructors for structs with NamedTuple fields](https://discourse.julialang.org/t/constructors-for-structs-with-namedtuple-fields/101758)

<div class="topic-metadata">

**Author:** [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Replies:** 23\
**Last updated:** [July 21, 2023, 12:26am UTC](https://discourse.julialang.org/t/constructors-for-structs-with-namedtuple-fields/101758 "2023-07-21T00:26:07Z")

</div>

I am creating a struct with a field that is a named tuple. I am getting different behavior when I define a constructor inside vs. outside the struct. Below, F1() runs fine but F2() gives a “UndefVarError: T not defined” …

---

## [Inside closures, captured constructors are much slower than captured functions](https://discourse.julialang.org/t/inside-closures-captured-constructors-are-much-slower-than-captured-functions/99638)

<div class="topic-metadata">

**Author:** [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Replies:** 2\
**Last updated:** [May 31, 2023, 12:47pm UTC](https://discourse.julialang.org/t/inside-closures-captured-constructors-are-much-slower-than-captured-functions/99638 "2023-05-31T12:47:54Z")

</div>

I have noticed that inside function closures, using a captured variable with a type value as a constructor is much slower than using a variable with a function value. Consider the following (silly) example: function g(f…

---

## [ConstructFrom.jl - Construct objects without spelling out their types](https://discourse.julialang.org/t/constructfrom-jl-construct-objects-without-spelling-out-their-types/97459)

<div class="topic-metadata">

**Author:** [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Replies:** 2\
**Last updated:** [April 20, 2023, 4:22am UTC](https://discourse.julialang.org/t/constructfrom-jl-construct-objects-without-spelling-out-their-types/97459 "2023-04-20T04:22:03Z")

</div>

Over the past couple of weeks, I’ve frequently had to write code like the following. struct MyType data::Dict{Int, Dict{Float64, String}} end MyType() = MyType(Dict{Int, Dict{Float64, String}}()) …

---

## [Class Constructor](https://discourse.julialang.org/t/class-constructor/95350)

<div class="topic-metadata">

**Author:** [@Veenty](https://discourse.julialang.org/u/Veenty)\
**Replies:** 4\
**Last updated:** [February 28, 2023, 8:31pm UTC](https://discourse.julialang.org/t/class-constructor/95350 "2023-02-28T20:31:13Z")

</div>

I’m trying to generate a struct, and one of the of the elements of the struct depends on a parameter of the class. I’m not really sure how to do it tho Example of what I’m doing struct SpectralDiscretization1D Spa…

---

## [Validating the content of a struct field](https://discourse.julialang.org/t/validating-the-content-of-a-struct-field/94717)

<div class="topic-metadata">

**Author:** [@Sibbe\_Bakker](https://discourse.julialang.org/u/Sibbe_Bakker)\
**Replies:** 5\
**Last updated:** [February 16, 2023, 11:14am UTC](https://discourse.julialang.org/t/validating-the-content-of-a-struct-field/94717 "2023-02-16T11:14:12Z")

</div>

Hello all, I am a beginner in the julia language and I quite like it. I am trying to make a struct that checks whether the input is valid: struct Dna # Basic elements of a DNA string sequence::String identi…

---

## [Type() vs type() -- Are constructor methods or conversion functions more idiomatic?](https://discourse.julialang.org/t/type-vs-type-are-constructor-methods-or-conversion-functions-more-idiomatic/93233)

<div class="topic-metadata">

**Author:** [@SortofDamocles](https://discourse.julialang.org/u/SortofDamocles)\
**Replies:** 4\
**Last updated:** [January 20, 2023, 10:17am UTC](https://discourse.julialang.org/t/type-vs-type-are-constructor-methods-or-conversion-functions-more-idiomatic/93233 "2023-01-20T10:17:55Z")

</div>

I am writing some code with structs that represent dual forms of the same underlying thing, e.g. struct NormalForm normal\_data descriptive\_parameter end NormalForm(data) = NormalForm(data, process(data)) struct…

---

## [Base.@kwdef breaks in tests only](https://discourse.julialang.org/t/base-kwdef-breaks-in-tests-only/91356)

<div class="topic-metadata">

**Author:** [@eott-siz](https://discourse.julialang.org/u/eott-siz)\
**Replies:** 2\
**Last updated:** [December 7, 2022, 3:13pm UTC](https://discourse.julialang.org/t/base-kwdef-breaks-in-tests-only/91356 "2022-12-07T15:13:42Z")

</div>

Julia v1.7.3 I have encountered a problem with using Base.@kwdef that only occurs in tests, not when code is executed normally. An example: File src/Example.jl: module Example Base.@kwdef mutable struct Foo bar :…

---

## [Puzzling behavior of "wrong" inner constructor](https://discourse.julialang.org/t/puzzling-behavior-of-wrong-inner-constructor/89954)

<div class="topic-metadata">

**Author:** [@simsurace](https://discourse.julialang.org/u/simsurace)\
**Replies:** 4\
**Last updated:** [November 9, 2022, 6:58am UTC](https://discourse.julialang.org/t/puzzling-behavior-of-wrong-inner-constructor/89954 "2022-11-09T06:58:08Z")

</div>

Today I was quite surprised by this mistake of mine in an inner constructor not throwing an error, but leading to a silent bug. My actual case had many arguments and I missed several, but here is a MWE: julia\> struct My…

---

## [Outer constructor using a function using the structure](https://discourse.julialang.org/t/outer-constructor-using-a-function-using-the-structure/88260)

<div class="topic-metadata">

**Author:** [@dgagnon](https://discourse.julialang.org/u/dgagnon)\
**Replies:** 4\
**Last updated:** [October 10, 2022, 10:35am UTC](https://discourse.julialang.org/t/outer-constructor-using-a-function-using-the-structure/88260 "2022-10-10T10:35:59Z")

</div>

I have a problem with a “complex” outer constructor (for me anyway). I have the following structure: struct MQC circuit\_list::Vector{QuantumCircuit} connector\_list::Vector{Connector} wire\_list::Vector{Wire} MQC() =…

[Next page](https://discourse.julialang.org/tag/constructors/739.md?match_all_tags=true&page=1&tags%5B%5D=constructors)
