New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

|x| operator is limited & buggy #219

vpax opened this Issue Nov 27, 2018 · 4 comments


None yet
5 participants
Copy link

vpax commented Nov 27, 2018

Right now, |x| is hardwired to have a type of count, but this isn't actually correctly enforced. For example, if I print |5-9| I get 18446744073709551612, and |5 sec - 9 sec| has an actual type of double (which is the internal type of interval).

We should either (1) extend |x| to be absolute value, with consistent typing [preferred], or (2) add type-checking so it can't be misused.


This comment has been minimized.

Copy link

dizbot commented Nov 27, 2018


This comment has been minimized.

Copy link

mavam commented Nov 29, 2018

Would that imply that operator |x| is polymorphic? It

  1. returns the number of elements if x is a container
  2. returns the absolute value for arithmetic data types

It makes sense notationally, but it requires that the user now needs to factor in type information. I would imagine that the context makes it obvious most of the time.


This comment has been minimized.

Copy link

vpax commented Nov 29, 2018

@mavam yes, polymorphic. Same as other operators, such as the recent ones introduced for sets and vectors, so I don't see this as any significant conceptual change. (In fact, it's also the case for the '/' operator, which means different things in the expression "8 / 2" vs. "".)


This comment has been minimized.

Copy link

ckreibich commented Nov 30, 2018

2005 strikes back! 👍 CHANGES sez:

- New operator |x|, a sizeof operator (Christian Kreibich).  It yields
  absolute values for numerical values, file size for files, the number
  of enums for an enum type, the number of addresses in subnets, number
  of fields in records, numeric equivalent for addresses, the number of
  elements in vectors/sets/tables, and the length of strings.

I do not remember how we drew the line at that feature set. Perhaps hours left until Jupiter 🍺?

I remember that back then I was very excited that |x| on a container would report its number of elements. Today I find this less convincing — for example, both Python and Go use len() vs abs(). But if people accept the polymorphism (I guess it's always been there, per the above), then I'm all for completing it.

I was relieved to see that we have btests for the operator, but none of the tests apply statements, and some of the differences are subtle (there's a test for |-10|, but not for something like |5-9|). Clearly a total minefield and I'm glad you caught it.

@jsiwek jsiwek added this to the 2.7 milestone Jan 11, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment