# Optional parameters with default types in parametric structure constructors

**URL:** <https://discourse.julialang.org/t/optional-parameters-with-default-types-in-parametric-structure-constructors/99812>\
**Category:** Internals & Design\
**Created:** [June 3, 2023, 3:30pm UTC](https://discourse.julialang.org/t/optional-parameters-with-default-types-in-parametric-structure-constructors/99812 "2023-06-03T15:30:41Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [June 4, 2023, 10:51am UTC](https://discourse.julialang.org/t/optional-parameters-with-default-types-in-parametric-structure-constructors/99812/11 "2023-06-04T10:51:29Z")

</div>

Ok, now I see what you are trying to do.

Someone please correct me, but I think this not really the Julian way of writing code, at least rarely do you see the `MyType{..}(...)` syntax used in the wild to perform implicit conversions (you do see `MyType{...}(...)` used when there are parameteric types that can’t be inferred from the constructors arguments!).  
~~Also: The fact that you have to do this with inner constructors is, I think, also a hint on that this is not Julian, because the `MyType{...}(...)` syntax was disallowed for functions (e.g. outer constructors) some time ago~~ That is not correct, you can do that also with out constructors. The syntax `MyStruct{...}(...)` is just reserved to mean type application (cf. [Reclaim parametric method syntax - #5 by Keno](https://discourse.julialang.org/t/reclaim-parametric-method-syntax/7145/5), [How to correctly define and call templated/parametric methods using the new 'where' syntax](https://discourse.julialang.org/t/how-to-correctly-define-and-call-templated-parametric-methods-using-the-new-where-syntax/24129)).

However, you can still do what you want with only three (or four if you want to dispatch on `::Missing`) inner constructors

```julia
const DRK=Float64 # default real kind
const DIK=Int64 # default integer kind

const EleType{RK}=Union{RK,AbstractArray{RK}} where {RK<:Real} # element type
const MaxType{IK}=Union{IK,Missing} where {IK<:Integer} # integer or missing (=inf)

struct LatVal{ET<:EleType}
  u::ET
  t::ET
end
LatVal(th) = LatVal(sin.(th),cos.(th))

struct PSIter{ET<:EleType,IK<:Integer,MK<:MaxType{IK}}
    lVal::LatVal{ET}
    mMax::MK
    mMin::IK

    function PSIter(th,mMax,mMin=zero(DIK))
      mMax, mMin = promote(mMax, mMin)
      return new{typeof(th),typeof(mMin),typeof(mMax)}(LatVal(th),mMax,mMin)
    end
    function PSIter(th,mMax::Missing,mMin=zero(DIK))
      return new{typeof(th),typeof(mMin),Missing}(LatVal(th),mMax,mMin)
    end

    PSIter{ET}(th,mMax=missing,mMin=zero(DIK)) where ET = PSIter(ET(th),mMax,mMin)
    PSIter{ET,IK}(th,mMax=missing,mMin=zero(DIK)) where {ET,IK} = PSIter(ET(th),mMax,IK(mMin))
end

Base.IteratorSize(::Type{PSIter{ET,IK,Missing}}) where {ET,IK} = Base.IsInfinite()
Base.IteratorSize(::Type{PSIter{ET,IK,IK}}) where {ET,IK} = Base.HasLength()

```

---

_[View the full topic](https://discourse.julialang.org/t/optional-parameters-with-default-types-in-parametric-structure-constructors/99812)._
