You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: doc/manual/methods.rst
+26
Original file line number
Diff line number
Diff line change
@@ -550,6 +550,32 @@ can also constrain type parameters of methods::
550
550
The ``same_type_numeric`` function behaves much like the ``same_type``
551
551
function defined above, but is only defined for pairs of numbers.
552
552
553
+
.. _man-vararg-fixedlen:
554
+
555
+
Parametrically-constrained Varargs methods
556
+
------------------------------------------
557
+
558
+
Function parameters can also be used to constrain the number of arguments that may be supplied to a "varargs" function (:ref:`man-varargs-functions`). The notation ``Vararg{T,N}`` is used to indicate such a constraint. For example:
559
+
560
+
.. doctest::
561
+
562
+
julia> bar(a,b,x::Vararg{Any,2}) = (a,b,x)
563
+
564
+
julia> bar(1,2,3)
565
+
ERROR: MethodError: `bar` has no matching method bar(::Int, ::Int, ::Int)
566
+
567
+
julia> bar(1,2,3,4)
568
+
(1,2,(3,4))
569
+
570
+
julia> bar(1,2,3,4,5)
571
+
ERROR: MethodError: `bar` has no method matching bar(::Int, ::Int, ::Int, ::Int, ::Int)
572
+
573
+
More usefully, it is possible to constrain varargs methods by a parameter. For example::
574
+
575
+
function getindex{T,N}(A::AbstractArray{T,N}, indexes::Vararg{Number,N})
576
+
577
+
would be called only when the number of ``indexes`` matches the dimensionality of the array.
0 commit comments