title: How to type-constrain the entries of a Raku function's array argument?
body: <p>I am trying to define a subroutine in <code>Raku</code> whose argument is, say, an <a href="https://docs.raku.org/routine/Array" rel="noreferrer">Array</a> of <a href="https://docs.raku.org/type/Int" rel="noreferrer">Int</a>s (imposing that as a constraint, i.e. rejecting arguments that are <em>not</em> <code>Array</code>s of <code>Int</code>s).</p>
  <p><strong>Question:</strong> What is the &quot;best&quot; (most idiomatic, or straightforward, or whatever you think 'best' should mean here) way to achieve that?</p>
  <p>Examples run in the <code>Raku</code> REPL follow.</p>
  <p><strong>What I was hoping would work</strong></p>
  <pre><code>&gt; sub f(Int @a) {1}
  &amp;f
  &gt; f([1,2,3])
  Type check failed in binding to parameter '@a'; expected Positional[Int] but got Array ([1, 2, 3])
    in sub f at &lt;unknown file&gt; line 1
    in block &lt;unit&gt; at &lt;unknown file&gt; line 1
  </code></pre>
  <p><strong>Another non-working example</strong></p>
  <pre><code>&gt; sub f(@a where *.all ~~ Int) {1}
  &amp;f
  &gt; f([1,2,3])
  Constraint type check failed in binding to parameter '@a'; expected anonymous constraint to be met but got Array ([1, 2, 3])
    in sub f at &lt;unknown file&gt; line 1
    in block &lt;unit&gt; at &lt;unknown file&gt; line 1
  </code></pre>
  <p>even though</p>
  <pre><code>&gt; [1,2,3].all ~~ Int
  True
  </code></pre>
  <p><strong>What does work</strong></p>
  <p>Both variations</p>
  <pre><code>&gt; sub f(@a where { @a.all ~~ Int }) {1}
  </code></pre>
  <p>and</p>
  <pre><code>&gt; sub f(@a where { $_.all ~~ Int }) {1}
  </code></pre>
  <p>do give me what I want:</p>
  <pre><code>&gt; f([5])
  1
  &gt; f(['x'])
  Constraint type check failed in binding to parameter '@a'; expected anonymous constraint to be met but got Array ([&quot;x&quot;])
    in sub f at &lt;unknown file&gt; line 1
    in block &lt;unit&gt; at &lt;unknown file&gt; line 1
  </code></pre>
  <p>I've used that in the past, but it strikes me as a bit clumsy/verbose..</p>
  <p><strong>Additional remark</strong></p>
  <p>The syntax <code>Int @a</code> I initially tried isn't completely bogus, but I can't tell when it's supposed to go through and when not.</p>
  <p>I can do this in a <a href="https://docs.raku.org/syntax/class" rel="noreferrer">class</a> for instance:</p>
  <pre><code>&gt; class A { has Int @.a }
  (A)
  
  &gt; A.new(a =&gt; [1,2,3])
  A.new(a =&gt; Array[Int].new(1, 2, 3))
  
  &gt; A.new(a =&gt; [1,2,'x'])
  Type check failed in assignment to @!a; expected Int but got Str (&quot;x&quot;)
    in block &lt;unit&gt; at &lt;unknown file&gt; line 1
  </code></pre>
  <p><strong>Edit 1</strong></p>
  <p>As per <a href="https://docs.raku.org/language/list#Typing" rel="noreferrer">the docs</a>, this works</p>
  <pre><code>&gt; sub f(Int @a) {1}
  &amp;f
  
  &gt; my Int @a = 1,2,3 # or (1,2,3), or [1,2,3]
  &gt; f(@a)
  1
  </code></pre>
  <p>But if I omit the <code>Int</code> in the declaration of <code>@a</code> I get back to the errors reported before. And as noted, I can't run <code>f</code> on anonymous arrays, with <code>f([1,2,3])</code>.</p>