# Type parameters by default?

**URL:** <https://discourse.julialang.org/t/type-parameters-by-default/45577>\
**Category:** Performance\
**Tags:** parametric-types\
**Created:** [August 26, 2020, 2:13pm UTC](https://discourse.julialang.org/t/type-parameters-by-default/45577 "2020-08-26T14:13:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 26, 2020, 2:13pm UTC](https://discourse.julialang.org/t/type-parameters-by-default/45577/1 "2020-08-26T14:13:25Z")

</div>

Hi everyone,

I’ve noticed a pattern in my Julia code and I wondered if it would make sense to have some compiler technology to automatically add type parameters for fields that have no explicit type.

For example, consider a view type on top of an array:

```julia
struct View
  arr
  ind
end

Base.getindex(v::View, i) = getindex(v.arr[ind[i]])

```

If we just add parametric types as in:

```julia
struct View{A,I}
  arr::A
  ind::I
end

```

The code using `View` becomes much faster as Julia is aware of the type of the fields as opposed to the default type Any. Would it make sense to change this behavior where the default `Any` type is actually replaced by a type parameter? Is there a major downside to this feature? I feel that it would make code much cleaner by default without compromising performance.

Perhaps my question is, what is the rationale for the default field type `Any` if it produces slow code?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [August 26, 2020, 2:33pm UTC](https://discourse.julialang.org/t/type-parameters-by-default/45577/2 "2020-08-26T14:33:25Z")

</div>

The downside is that compilation time would increase. Also, error messages would become much longer with all the type parameters (try DiffEq errors). And it is a breaking change. Thus I don’t think it would fly.

Note that you could achieve this with a macro. Alternatively, just use named tuples. Which are essentially this feature but without a name. Note that Parameters.jl has some features which are somewhat related: `@deftype` and `@with_kw` applied to NamedTuples.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 26, 2020, 2:39pm UTC](https://discourse.julialang.org/t/type-parameters-by-default/45577/3 "2020-08-26T14:39:20Z")

</div>

This could have been a reasonable design choice (ask for `Any` if you want that, otherwise the field is parametrized implicitly), but given that the change would be breaking this is unlikely to happen.

I would follow @mauro3’s suggestion and have a macro

```julia
@implicittypes struct Foo
    a::T
    b
end

```

that expands to something like

```julia
struct Foo{T,S1}
    a::T
    b::S1
end

```

where `S1` etc is generated not to conflict with other type parameters. It is a nice exercise with

[https://github.com/FluxML/MacroTools.jl](https://github.com/FluxML/MacroTools.jl)

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [August 26, 2020, 2:41pm UTC](https://discourse.julialang.org/t/type-parameters-by-default/45577/4 "2020-08-26T14:41:16Z")

</div>

There’s one such macro in [QuickTypes.jl](https://github.com/cstjean/QuickTypes.jl)

```julia
# `_fp` (for Fully Parametric) automatically adds type parameters. For example:
@qstruct_fp Plane(nwheels, weight::Number; brand=:zoomba)
# is equivalent to `@qstruct Plane{T, U <: Number, V}(nwheels::T, weight::U; brand::V=:zoomba)`
# For even greater specialization, see `?@qstruct_np`.

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 26, 2020, 6:23pm UTC](https://discourse.julialang.org/t/type-parameters-by-default/45577/5 "2020-08-26T18:23:27Z")

</div>

Yeah, it would have been an interesting choice for performance I guess.
