# Syntax for outer constructor and thow-away input argument

**URL:** https://discourse.julialang.org/t/syntax-for-outer-constructor-and-thow-away-input-argument/38468
**Category:** New to Julia
**Created:** [April 30, 2020, 7:14am UTC](https://discourse.julialang.org/t/syntax-for-outer-constructor-and-thow-away-input-argument/38468 "2020-04-30T07:14:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![RGerzaguet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgerzaguet/32/45492_2.png) [@RGerzaguet](https://discourse.julialang.org/u/RGerzaguet)
#### Post date: [April 30, 2020, 7:14am UTC](https://discourse.julialang.org/t/syntax-for-outer-constructor-and-thow-away-input-argument/38468/1 "2020-04-30T07:14:12Z")

</div>

Hello there,

A simple semantic question, and a way to call an inner constructor  
Assuming I have a simple structure defined as follows

```
struct Test 
   b::Int;
   m::Int;
   n::Int;
end 

```

But such that we have b = m+n+1. One can built an inner constructor to check that the construction is fine

```
struct Test 
   b::Int;
   m::Int;
   n::Int;
   Test(b::Int,m::Int,n::Int) = begin 
                     @assert b == m+n+1 "b should be m+n+1"
                     return new(b,m,n)
                   end
end 

```

And the structure can be called by

```
Test(8,0,7)

```

And raises an error with Test(8,0,4).

In some cases, I would like to be able to partially defined the structure, i.e call a Test(8,0,_) that returns a Test(8,0,7) or Test(8,_,7) that returns a Test(8,0,7). Kind of throw-away input argument.  
However, I cannot figure out how to call such a function. I was thinking to define function as

```
Q(b::Int,m::Int,_) = Q(b,m,b-m-1);
Q(b::Int,_,n::Int) = Q(b,b-n-1,n);

```

But if definition is possible, I can’t instanciate them with

```
Test(8,0,_)
ERROR: syntax: all-underscore identifier used as rvalue

```

I understand the error, and

- I know that I can handle the construction using keyword
- Can also define Test(8,0,nothing) that can be handled by multiple dispatch, but I was wondering if such an easy syntax as Q(8,0,_) or Q(8,_,7) can be used ?  
Any advice ?

Thanks

---

<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: [April 30, 2020, 7:42am UTC](https://discourse.julialang.org/t/syntax-for-outer-constructor-and-thow-away-input-argument/38468/2 "2020-04-30T07:42:09Z")

</div>

`_` is not a value you can use ([rvalue](https://en.wikipedia.org/wiki/Value_(computer_science)#lrvalue)), it is syntax for _ignoring_ values. This is what the error message says.

I would suggest that you have a `Test(; b, m, n)` outer constructor that just fills in the value not provided. [`range`](https://github.com/JuliaLang/julia/blob/c8797ad6dd5869269d78f067680f59cc36e5286f/base/range.jl#L49) is a example you can follow for the implementation.

An alternative is to define a singleton struct, eg

```julia
struct FillIn end

Q(b::Int, m::Int, ::FillIn) = Q(b,m,b-m-1)

# call as Q(b, m, FillIn())

```

(incidentally, lines do not need to be terminated with `;` in Julia)

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [April 30, 2020, 12:35pm UTC](https://discourse.julialang.org/t/syntax-for-outer-constructor-and-thow-away-input-argument/38468/3 "2020-04-30T12:35:38Z")

</div>

Maybe you could use a colon (`:`) as placeholder for a missing value?

```julia
Q(b, m, n) = (b=b, m=m, n=n)
Q(b::Int, m::Int, ::Colon) = Q(b, m, b-m-1);
Q(b::Int, ::Colon, n::Int) = Q(b, b-n-1, n);

```

```julia
julia> Q(8, 0, :)
(b = 8, m = 0, n = 7)

julia> Q(8, :, 7)
(b = 8, m = 0, n = 7)

```

Such a syntax is consistent with what is done for example in [`reshape`](https://docs.julialang.org/en/v1/base/arrays/#Base.reshape) when one does not want to specify all arguments.

---

<div class="post-metadata">

### Author: ![RGerzaguet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgerzaguet/32/45492_2.png) [@RGerzaguet](https://discourse.julialang.org/u/RGerzaguet)
#### Post date: [April 30, 2020, 1:10pm UTC](https://discourse.julialang.org/t/syntax-for-outer-constructor-and-thow-away-input-argument/38468/4 "2020-04-30T13:10:57Z")

</div>

@Tamas_Papp Thanks for the answer. Outer is indeed a good solution there, even if I prefer the second approach with the specific type handler. For the ;, I am affraid that it is some PTSD from former langages !

@ffevotte Many thanks, this is exaclty what I was looking for !
