Uploaded image for project: 'Puppet'
  1. Puppet
  2. PUP-4193

Add ability to use a lambda as case option for advanced matching



    • New Feature
    • Status: Closed
    • Normal
    • Resolution: Won't Do
    • None
    • None
    • None
    • None
    • 2
    • Language 2015-04-29, Language 2015-05-13



      The original request was for multiple things, and those have been broken up into :

      This ticket adds the ability to use a lambda as a case option (more than one lambda can be used in an options list). Matching of a lambda is done by splatting the test expression as arguments in a call to the lambda. The option is selected unless the result is false or undef.


      notice case [2,1] {
       |$x, $y| { $x  == $y } : { 'equal' }
       |$x, $y| { $x  <  $y } : { 'ascending' }
       |$x, $y| { $x  >  $y } : { 'descending' }

      This example will notice descending.

      The intended use case is when a case should match based on a combination of test values.


      ML-like languages have a language feature called 'pattern matching' (which has nothing to do with regular expressions) - they provide a very powerful control structure to make decisions based on multiple criteria; at their simplest they are a convenient shorthand for nested switch statements. They can also be used to make decisions based on the shape of data structures, and to pull values out of them, though I am not sure how much of that would be useful for Puppet.

      A lot of Puppet code does exactly that: nest a switch statement on one variable inside the branches of a switch statement on another variable, and a shorthand for this could simplify quite a few Puppet manifests.

      As an example, I attach a version of apt::params from the puppetlabs/apt module that shows what this construct would look like. It cuts the size of params.pp almost in half, and more importantly makes it much clearer what values decisions are based on, and what variables get set because of those decisions.

      I'll also add some links to what pattern matching in other languages can do, to give a better idea of what this is all about.

      QA Risk Analysis

      risk: medium (validate only)
      probability: medium (new feature, but similar to other langs)
      severity: medium (other syntax, workarounds)
      test layer: unit/integration


        Issue Links



              Unassigned Unassigned
              lutter David Lutterkort
              0 Vote for this issue
              8 Start watching this issue



                Zendesk Support