Jira (PUP-9753) XOR operator in Puppet Language

15 views
Skip to first unread message

PU (JIRA)

unread,
Jun 14, 2019, 5:07:03 AM6/14/19
to puppe...@googlegroups.com
PU created an issue
 
Puppet / New Feature PUP-9753
XOR operator in Puppet Language
Issue Type: New Feature New Feature
Assignee: Unassigned
Components: Language
Created: 2019/06/14 2:06 AM
Labels: operator, Language
Priority: Normal Normal
Reporter: PU

Sometimes there are some configuration values which have both to be set in combination. So you want to check if both or none of them are set. Usually this would be done using XOR. But unfortunately there isn't such an operator in Puppet Language at the moment.

Of course there are alternative ways to get to the same result:

(($a and !$b) or ($b and !$a))

But a native XOR operator would make it much easier and better to understand:

$a xor $b

So I think it would be very much appreciated if this operator will be implemented in Puppet Language.

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v7.7.1#77002-sha1:e75ca93)
Atlassian logo

Henrik Lindberg (JIRA)

unread,
Jun 14, 2019, 8:42:03 AM6/14/19
to puppe...@googlegroups.com
Henrik Lindberg commented on New Feature PUP-9753
 
Re: XOR operator in Puppet Language

If $a and $b are booleans you can just do $a Unable to render embedded object: File (= $b}}, if not boolean do {{) not found.$a != !$b (which also works for booleans) and you have your xor. Which is also easily implemented as a function.

IMO, we don't need an XOR built into the language since it is trivially constructed out of what is there already.

PU (JIRA)

unread,
Jun 14, 2019, 9:55:03 AM6/14/19
to puppe...@googlegroups.com
PU commented on New Feature PUP-9753

Unfortunately your proposed solution isn't readable. So please change it accordingly.

Henrik Lindberg (JIRA)

unread,
Jun 14, 2019, 2:57:02 PM6/14/19
to puppe...@googlegroups.com

Updated with better formatting

Henrik Lindberg (JIRA)

unread,
Jun 15, 2019, 5:36:02 AM6/15/19
to puppe...@googlegroups.com

Explanations to the above examples:

```

!$b

```

The ¡ is a boolean not, which requires a boolean value. To get one from any value, it checks if value is truthy or not:

  • false and undef become false
  • and all other values become true
    The ! (not) operator then inverts the boolean

For the XOR operation

$a != $b

It does not matter if booleans are inverted or not since we are only concerned about values being different - i.e. the same result is obtained for true != false as for false != true.

As an extra: if you want a boolean reflecting the "truthy value" you can do !!$a, which first converts to inverted boolean and then inverts that to reflect the "truthyness" of the value.

PU (JIRA)

unread,
Jun 17, 2019, 7:16:03 AM6/17/19
to puppe...@googlegroups.com
PU commented on New Feature PUP-9753

Thank you for this alternative solution and the explanations. At the moment I agree that with this alternative an XOR operator isn't really needed although it would be better understandable.

Henrik Lindberg (JIRA)

unread,
Jun 17, 2019, 7:25:02 AM6/17/19
to puppe...@googlegroups.com

IMO, XOR would be easier to understand for those that understand what XOR is, for others it is just trading one difficulty for another.

Jorie Tappa (JIRA)

unread,
Jun 17, 2019, 12:47:02 PM6/17/19
to puppe...@googlegroups.com
Jorie Tappa updated an issue
 
Change By: Jorie Tappa
Team: Server
Reply all
Reply to author
Forward
0 new messages