# Automatically make all fields of an (immutable) struct concrete type

**URL:** <https://discourse.julialang.org/t/automatically-make-all-fields-of-an-immutable-struct-concrete-type/36081>\
**Category:** General Usage\
**Created:** [March 17, 2020, 7:43am UTC](https://discourse.julialang.org/t/automatically-make-all-fields-of-an-immutable-struct-concrete-type/36081 "2020-03-17T07:43:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lassepe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lassepe/32/10050_2.png) [@lassepe](https://discourse.julialang.org/u/lassepe)\
**Post date:** [March 17, 2020, 7:43am UTC](https://discourse.julialang.org/t/automatically-make-all-fields-of-an-immutable-struct-concrete-type/36081/1 "2020-03-17T07:43:06Z")

</div>

I often find myself introducing a bunch of type parameters to make working with the declared struct faster. For example, I would write

```julia
struct Foo{TA, TB}
a::TA 
b::TB 
end

```

and the only purpose of `TA` and `TB` is to make sure `Foo` is a concrete type. If wrote

```julia
struct Foo
a
b
end

```

this this would not be the case, because field `:a` and field `:b` are of type `Any`.

Is there any way to automatically introduce template parameters for all the fields? Ideally, I would like to be able to write something like

```julia
struct Foo
a::Real
b::Integer
end

```

as a shorthand for

```julia
struct Foo{TA<:Real, TB<:Integer}
a::TA 
b::TB 
end

```

At least for immutable structs I don’t see why a field should ever have a non-concrete type (?)

In summary:

1. Is there a less verbose way to “concretize” the types of all fields in a struct?
2. Why does Julia allow abstract field types for immutable structs?

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [March 17, 2020, 7:51am UTC](https://discourse.julialang.org/t/automatically-make-all-fields-of-an-immutable-struct-concrete-type/36081/2 "2020-03-17T07:51:56Z")

</div>

This can be implemented in a macro. [QuickTypes.jl](https://github.com/cstjean/QuickTypes.jl) has it, quoting the readme:

```julia
using QuickTypes

@qstruct_fp Plane(nwheels, weight::Number; brand=:zoomba)

```

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [March 17, 2020, 8:19am UTC](https://discourse.julialang.org/t/automatically-make-all-fields-of-an-immutable-struct-concrete-type/36081/3 "2020-03-17T08:19:35Z")

</div>

As for the second question, this can be useful in a couple of situations.

- One is, when you want to avoid generating too much code. Then it can make sense to avoid specialization on fields that are irrelevant for performance hot spots.

- Another one is, when you want to wrap things that are technically of different type into a single concrete type. Like

```julia
struct Predicate
     f
end
(p::Predicate)(x) = p.f(x)::Bool

@assert typeof(Predicate(isfinite)) == typeof(Predicate(isnan))

```
