# Constructors for structs with NamedTuple fields

**URL:** <https://discourse.julialang.org/t/constructors-for-structs-with-namedtuple-fields/101758>\
**Category:** New to Julia\
**Tags:** struct, namedtuple, constructors\
**Created:** [July 19, 2023, 12:10am UTC](https://discourse.julialang.org/t/constructors-for-structs-with-namedtuple-fields/101758 "2023-07-19T00:10:21Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [July 20, 2023, 3:01am UTC](https://discourse.julialang.org/t/constructors-for-structs-with-namedtuple-fields/101758/21 "2023-07-20T03:01:27Z")

</div>

One more thing. You can define your own “catch-all” method and do what you want with everything that is not an `Integer` (I think is better to do this instead of deleting methods):

```julia
@kwdef struct A
    x::Integer = 0
end

A(x) = error("I really don't want to convert stuff.")
A(x) = error("I really don't want to convert stuff (overriding).")

A('a')
# ERROR: I really don't want to convert stuff (overriding).
# Stacktrace:
# [1] error(s::String)
# @ Base ./error.jl:35
# [2] A(x::Char)
# @ Main ~/code/playtest/innerconstr.jl:23
# [3] top-level scope
# @ ~/code/playtest/innerconstr.jl:25

A(1) # works

```

Regardless of what was generated by @kwdef as `A(x)` method, the most recent one is in effect (you can see that I also generated two of them - but only the last one counts).

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [July 20, 2023, 10:37pm UTC](https://discourse.julialang.org/t/constructors-for-structs-with-namedtuple-fields/101758/22 "2023-07-20T22:37:24Z")

</div>

@Benny, I think we (or at least, I) need someone with internals knowledge.

After some more digging, I reached [this](https://github.com/JuliaLang/julia/blob/d080fe6c9bbc2076a7228d43f2651831ef7f8bac/src/datatype.c#L1401C33-L1401C33) stage - and, well, that is a pretty large code base to be able to derive any non-speculative conclusions (also, my C knowledge ends at a now forgotten page number from K&R book - no real-world project whatsoever).

However, this becomes time-consuming with no real progress for me (unless the permalink above can be considered progress), and I am not even sure I am on the right path. Should we tag somebody? Or open an issue?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 20, 2023, 10:43pm UTC](https://discourse.julialang.org/t/constructors-for-structs-with-namedtuple-fields/101758/23 "2023-07-20T22:43:12Z")

</div>

If you’re referring to the `new` mystery, maybe a new discourse thread tagged Internals and Design since this has gone beyond the specific use case here. As for an issue I wouldn’t even know what it should be, maybe some people on that thread could help narrow that down.

---

<div class="post-metadata">

**Author:** ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Post date:** [July 21, 2023, 12:26am UTC](https://discourse.julialang.org/t/constructors-for-structs-with-namedtuple-fields/101758/24 "2023-07-21T00:26:07Z")

</div>

I see 4 issues. All of these seem concerning enough to me to escalate, but I am not qualified enough to be certain. And I don’t know who to raise these with.

1. `convert` has strange behavior here:

```julia
instance = (a=NaN,)
type1 = NamedTuple{(:a,), Tuple{A}} where A<:Real # abstract type

instance::type1 # confirms that instance is of type1
convert(type1, instance) # ERROR: UndefVarError: `T` not defined

```

1. internal type constructor tries to apply convert even when the type assertion already holds:

```julia
struct F
    x::type1
    F(x::type1=instance) = new(x)
end
F() # UndefVarError: `T` not defined

```

1. The default struct constructor has a method that accepts arguments without type restrictions, even though the struct field is type-annotated:

```julia
julia> struct A
       a::Int64
       end

julia> methods(A)
# 2 methods for type constructor:
 [1] A(a::Int64)
     @ REPL[7]:2
 [2] A(a)
     @ REPL[7]:2

```

1. function definitions allow default values of improper types:

```julia
f(x::Integer="Not Integer") = "Why is this allowed?" # does not throw error
f() # throws an error

```

Edit: I’ve opened two issues on github:

> <https://github.com/JuliaLang/julia/issues/50627#issue-1816276753>
>
> \`\`\`julia
> instance = (a=NaN,)
> type = NamedTuple{(:a,), Tuple{A}} where A\<:Real …# abstract type
> 
> instance::type # confirms that instance is of type
> 
> struct F
> x::type
> F(x::type=instance) = new(x)
> end
> F() # applies \`convert\` and gives error - UndefVarError: \`T\` not defined
> \`\`\`
> 
> possibly related issue: https://github.com/JuliaLang/julia/issues/50626#issue-1816272573
> 
> context: \[discourse discussion\](https://discourse.julialang.org/t/constructors-for-structs-with-namedtuple-fields/101758/24?u=sadish-d)

> <https://github.com/JuliaLang/julia/issues/50626#issue-1816272573>
>
> \`\`\`julia
> instance = (a=NaN,)
> type = NamedTuple{(:a,), Tuple{A}} where A\<:Real …# abstract type
> 
> instance::type # confirms that instance is of type
> convert(type, instance) # ERROR: UndefVarError: \`T\` not defined
> \`\`\`
> 
> 
> \`\`\`
> julia\> versioninfo()
> Julia Version 1.9.0
> Commit 8e630552924 (2023-05-07 11:25 UTC)
> Platform Info:
> OS: macOS (arm64-apple-darwin22.4.0)
> CPU: 8 × Apple M1
> WORD\_SIZE: 64
> LIBM: libopenlibm
> LLVM: libLLVM-14.0.6 (ORCJIT, apple-m1)
> Threads: 1 on 4 virtual cores
> Environment:
> JULIA\_EDITOR = code
> \`\`\`
> 
> possibly related issue: https://github.com/JuliaLang/julia/issues/50627#issue-1816276753
> 
> context: \[discourse discussion\](https://discourse.julialang.org/t/constructors-for-structs-with-namedtuple-fields/101758/24?u=sadish-d)

[Previous page](https://discourse.julialang.org/t/constructors-for-structs-with-namedtuple-fields/101758.md?page=1)
