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

Add default value to Default type

    XMLWordPrintable

Details

    • New Feature
    • Status: Closed
    • Normal
    • Resolution: Won't Do
    • None
    • None
    • None
    • None
    • 3
    • Language 2016-02-24, Language 2016-03-09
    • New Feature
    • The Default type now has the ability to also define a default value of a stated type. This in combination with type aliases makes it possible to use the same default value everywhere the type alias is used. e.g Default[Integer, 42].

    Description

      It is of value to let a Default type define a default value (to be used when no value was given). This centralises the control over what the default value should be - without this, every place that optional type is used the user needs to set the same default value using a default value expression.

      The syntax is:

      Default[T, <default_value>]
      

      There is one quirk in making this work in a backwards compatible way. Currently, only default matches a Default. This needs to change so that a Default without a default Type, vaue parameters value matches undef. If a Type and a default is given, the type does not match default, but it matches T. It does not match an undef unless T matches undef. The default value is information at a different level - used when instantiating an instance of an object having a parameter of Default[T, val] type, or when an argument is not given. The default value parameter has no meaning when the type is used for matching.

      • Default creates a type that matches default
      • Default[T] is an error (it is the same as T
      • Default[T, <t value>] matches what T matches, <t value> must match T
      • undef only matches if T matches undef

      The rationale for these rules are that it is of value to be able to have an undef default value. Users rarely (if ever) wants to use the special literal default as a default value. We can however add the special rule that only applies to argument/parameter assignment.

      • If a default value is given, and the argument data type is Default[T, <t value>, then the parameter is assigned <t-value>

      This results in the following positive outcomes:

      • For functions it is possible to explicitly state that the default value is wanted, which is good in situations where there are more than one parameter and you only really care about setting a value for the second of those.
      • Longer term, in defines and classes, there can be a differentiation between giving undef and giving default - now undef plays that role. (We allow default to also play that role until we deprecate that possibility and instead take that as an explicit assignment of undef.

      At runtime when matching parameters, the following rules applies when the parameter's datattype is Default[T, t]:

      • if user gives a value !~ T, it is an error
      • If user gives a value - this value wins
        • unless the target is a class or definition parameter
          • in which case a given undef has the same outcome as if user did not give a value.
          • in which case a given default has the same outcome as if user did not give a value.
      • If user gave no value (or special case above applies)
        • If data binding returns a value other than undef, this value wins
      • else (in order)
        • if there is a default value expression for the parameter it wins
        • if the parameter's Default type has a default value it wins
        • If the parameter's type is a complex type including Default types and they unambiguously defines a default value, then this default value wins.
        • If a complex parameter type is used and there is a default value ambiguity, an error is raised "default value specified by multiple Default types, use a default value expression for the parameter to resolve this ambiguity".

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              henrik.lindberg Henrik Lindberg
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Zendesk Support