Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>1:</h2> <p>In general, when you need to generate different things for different number of arguments passed you don't need to use default argument values at all, e.g.:</p> <pre><code>.margin(@top, @bottom, @right, @left) { /* right and left are passed */ } .margin(@top, @bottom) { /* right and left are not passed */ } .margin() { /* no arguments passed */ } // etc. </code></pre> <p>Note that each of these mixins can reuse the others, for example <code>.margin(@top, @bottom)</code> can do something special for the "no right and left case" and then call <code>.margin(@top, @bottom, 0, 0)</code> to perform the main job.</p> <h2>2:</h2> <p>But if you still need these defaults for some reason you can use some special default value that can't be a valid margin, e.g. something like this:</p> <pre><code>.margin(@top: undefined, @bottom: undefined, @right: undefined, @left: undefined) { .test-args(); .test-args() when (@right = undefined) { /* right is not passed */ } .test-args() when (@left = undefined) { /* left is not passed */ } .test-args() when not(@right = undefined) and not(@left = undefined) { /* right and left are passed */ } // etc. } </code></pre> <h2>3:</h2> <p>And the third option would be to use variadic arguments and test their count, but this one is the most verbose and dumb I guess:</p> <pre><code>.margin(@args...) { .eval-args(length(@args)); // requires LESS 1.5.+ .eval-args(@nargs) { // default values: @top: not passed; @bottom: not passed; @right: not passed; @left: not passed; } .eval-args(@nargs) when (@nargs &gt; 0) { @top: extract(@args, 1); } .eval-args(@nargs) when (@nargs &gt; 1) { @bottom: extract(@args, 2); } .eval-args(@nargs) when (@nargs &gt; 2) { @right: extract(@args, 3); } .eval-args(@nargs) when (@nargs &gt; 3) { @left: extract(@args, 4); } args: @top, @bottom, @right, @left; } </code></pre> <p>Though it may probably have its pros in some special use-cases.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload