Skip to content
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

Implement easy-to-use temporary of modifier #5

Open
ptosi opened this issue Oct 1, 2021 · 0 comments
Open

Implement easy-to-use temporary of modifier #5

ptosi opened this issue Oct 1, 2021 · 0 comments

Comments

@ptosi
Copy link

ptosi commented Oct 1, 2021

It's nice to be able to set of to another base but is still a bit verbose, in particular when done for a single operation.

Could cork be extended to support an operator that temporarily switches of to what makes sense in a "do what I mean" manner?

For example, the ? operator could result in hex values being printed as decimal and decimal as hex, irrespective of of. Consider

cork> set of dec
cork> 1 + 1
2
cork> 42?
0x2a
cork> set of hex
cork> 0x10 + 0x10
0x20
cork> 0x100?
256

instead of having to manually manage of:

cork> set of hex
cork> 42
0x2a
cork> set of dec
cork> set of dec
cork> 0x100
256
cork> set of hex

I can't decide whether ?'s behavior should be dependent on of, though ...

If support for more niche bases is desired (or to disambiguate complex expressions), ? could also take an optional right operand that represents the requested base:

cork> set of hex
cork> 0x20 + 3
0x23
cork> 0x20 + 3?
Failed to parse "0x20 + 3?":  --> 1:9
  |
1 | 0x20 + 3?
  |         ^---
  |
  = ambiguous expression, expected right operand for base
cork> 0x20 + 3?b
0b100011
cork> 0x20 + 3?d
35

Another approach could also be to overload the case of receiving a single literal of the current of by reflecting it in the alternative base:

cork> set of hex
cork> 0x100
256
cork> set of dec
cork> 256
0x100

which is definitely more informative than the current behavior:

cork> set of hex
cork> 0x100
0x100
cork> set of dec
cork> 256
256

If worth the added complexity, support for other alternative bases than the respective "default ones" (i.e. hex for decimal and decimal for hex), could be implemented through a new set property. For example:

cork> set of hex
cork> set alter_of bin
cork> 0x100
0b100000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant