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

Callable type, function, and block definitions, can be declared with return type

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Normal
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: PUP 4.8.0
    • Component/s: None
    • Labels:
      None
    • Template:
    • Acceptance Criteria:
      Hide

      1. A Puppet function can be declared with a return type that is declared with the right shift operator between the parameter declaration and the function body:

      function f(Integer $x) >> Integer { $x + 1 }
      

      2. A Puppet block can be declared with a return type that is declared with the right shift operator between the parameter declaration and the function body:

      [1,2].map |Integer $x| >> Integer { $x + 1 }
      

      3. A Ruby function dispatch can be declared with a return_type

      Puppet::Functions.create_function('testing::test') do
        dispatch :test do
          param 'Integer', :x
          return_type 'Integer'
        end
        def test(x)
           x + 1
         end
      end
      

      4. For criteria 1-3, the return type is used to validate the returned value.
      5. A callable, that without return type can be declared as Callable[<param spec>], can be declared with a return type as Callable[[<param spec>], <return type>].
      6. Two callable types, A and B that have no declared return type and where B is assignable to A, will no longer be assignable if both types are given a return type and the return type of B is not assignable to the return type of A.

      Show
      1. A Puppet function can be declared with a return type that is declared with the right shift operator between the parameter declaration and the function body: function f(Integer $x ) >> Integer { $x + 1 } 2. A Puppet block can be declared with a return type that is declared with the right shift operator between the parameter declaration and the function body: [1,2].map |Integer $x | >> Integer { $x + 1 } 3. A Ruby function dispatch can be declared with a return_type Puppet::Functions.create_function( 'testing::test' ) do dispatch :test do param 'Integer' , :x return_type 'Integer' end def test(x) x + 1 end end 4. For criteria 1-3, the return type is used to validate the returned value. 5. A callable, that without return type can be declared as Callable[<param spec>] , can be declared with a return type as Callable[[<param spec>], <return type>] . 6. Two callable types, A and B that have no declared return type and where B is assignable to A, will no longer be assignable if both types are given a return type and the return type of B is not assignable to the return type of A.
    • Story Points:
      2
    • Sprint:
      Language 2016-08-10, Language 2016-08-24
    • Release Notes:
      New Feature
    • Release Notes Summary:
      Hide
      It is now possible to specify the return type of functions and lambdas to get an automatic assertion that the function returned a value of the expected type. This helps reduce the amount of checking done at the calling end.
      As an example `function min(Integer $x, $y) >> Integer` defines that the function `min` returns an `Integer`.
      Show
      It is now possible to specify the return type of functions and lambdas to get an automatic assertion that the function returned a value of the expected type. This helps reduce the amount of checking done at the calling end. As an example `function min(Integer $x, $y) >> Integer` defines that the function `min` returns an `Integer`.

      Description

      As a step towards using the type system more extensively we need to have the ability to specify the return (produced) type of a Callable.

      The current API is Callable[ T1, T2, ..., min, max] where each T represent the type of a parameter, and the optional min,max controls a variable number of arguments of the last parameter.

      To enable also being able to describe the return type, the Callable should as an alternative accept an array in the first position to describe the parameters, and a second T argument to describe the return type. As an example, the following would describe a function accepting a Float and an integer, and returning a String.

      Callable[[Float, Integer], String]
      

      This also opens up a future possibility to use a hash as the first argument. That can be used to describe the callable signature of something that accepts arguments by name as opposed to arguments by position. (This is a future issue, and not in scope for this ticket).

      In addition:

      • A Callable without described return type defaults to Any
      • The content of the parameters array (in first position), abides by the same rules as in the existing Callable.

        Attachments

          Activity

            jsd-sla-details-panel

              People

              • Assignee:
                Unassigned
                Reporter:
                henrik.lindberg Henrik Lindberg
              • Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: