chipsalliance / chipsalliance/chisel

Printable should adopt a `f"`-like handling of `%`

Open
#1,522 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Scala
Stars
4.8k
Forks
658
Avg merge
18h 59m
Merged PRs (30d)
14

Description

Currently to do format Strings with the printable String, you have to use verbose method invocations, eg.
```scala
printf(p"$myUInt == ${Hexadecimal(myUInt)}\n")
```
The default is decimal so no need there, but to get hex you have to use `Hexadecimal`. This causes people to use C-style format Strings:
```scala
printf(p"%d == %x\n", myUInt, myUInt)
```

In Scala, you can do format strings with `f"` like so:

```scala
println(f"$myInt == $myInt%x")
```
We should do something similar for Printable

**Problem**
Because `%` wasn't needed by printable, I stupidly didn't require it to be escaped, so changing this behavior for `p"` would be a backwards incompatible change.

So, the answer is that we should probably introduce a new interpolator. We could choose a different single-character, or perhaps do `pf"..."` for "printable format", eg.
```scala
printf(pf"$myUInt == $myUInt%x\n")
```

**Type of issue**: feature request

**Impact**: API addition (no impact on existing code) | API modification

**Development Phase**: proposal

**What is the use case for changing the behavior?**

See above

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.