# Optional positional arguments NOT occur at end?

**URL:** <https://discourse.julialang.org/t/optional-positional-arguments-not-occur-at-end/25749>\
**Category:** New to Julia\
**Created:** [June 27, 2019, 12:19am UTC](https://discourse.julialang.org/t/optional-positional-arguments-not-occur-at-end/25749 "2019-06-27T00:19:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [June 27, 2019, 12:19am UTC](https://discourse.julialang.org/t/optional-positional-arguments-not-occur-at-end/25749/1 "2019-06-27T00:19:19Z")

</div>

we know that optional positional arguments must occur at end, however, functions like `zeros()` and `ones()` seems to violate the rule:

```julia
zeros([T=Float64,] dims...)
ones([T=Float64,] dims...)

```

I tried to understand how it works, but the source tells me nothing:

```julia
function zeros end
function ones end

```

what’s the trick? thanks

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [June 27, 2019, 12:25am UTC](https://discourse.julialang.org/t/optional-positional-arguments-not-occur-at-end/25749/2 "2019-06-27T00:25:41Z")

</div>

what I could imagine is to define two functions:

```julia
function ones(T::Type, dims...)
   # some code depends on T
end

function ones(dims...)
    # some code with the knowledge of "T == Float64"
end

```

is it the way to do?

---

<div class="post-metadata">

**Author:** ![aharoun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aharoun/32/6887_2.png) [@aharoun](https://discourse.julialang.org/u/aharoun)\
**Post date:** [June 27, 2019, 12:31am UTC](https://discourse.julialang.org/t/optional-positional-arguments-not-occur-at-end/25749/3 "2019-06-27T00:31:43Z")

</div>

I think that is correct.

```julia
for (fname, felt) in ((:zeros, :zero), (:ones, :one))
    @eval begin
        $fname(dims::DimOrInd...) = $fname(dims)
        $fname(::Type{T}, dims::DimOrInd...) where {T} = $fname(T, dims)
        $fname(dims::Tuple{Vararg{DimOrInd}}) = $fname(Float64, dims)
        $fname(::Type{T}, dims::NTuple{N, Union{Integer, OneTo}}) where {T,N} = $fname(T, map(to_dim, dims))
        $fname(::Type{T}, dims::NTuple{N, Integer}) where {T,N} = fill!(Array{T,N}(undef, map(to_dim, dims)), $felt(T))
        $fname(::Type{T}, dims::Tuple{}) where {T} = fill!(Array{T}(undef), $felt(T))
    end
end

```

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [June 27, 2019, 12:33am UTC](https://discourse.julialang.org/t/optional-positional-arguments-not-occur-at-end/25749/4 "2019-06-27T00:33:15Z")

</div>

> [@tomtom](#):
>
> is it the way to do?

Yes, pretty much.

```julia
ones(dims...) = ones(Float64, dims...)

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [June 27, 2019, 12:40am UTC](https://discourse.julialang.org/t/optional-positional-arguments-not-occur-at-end/25749/5 "2019-06-27T00:40:10Z")

</div>

thanks
