Skip to content

Implement easy-to-use temporary of modifier #5

@ptosi

Description

@ptosi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions