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

WIP: string formatting Julep #49

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
move performance to own section
  • Loading branch information
simonbyrne authored Feb 5, 2018
commit dc5e5dbd9bf0a5ccd714d047951b3b576043e7dc
26 changes: 17 additions & 9 deletions Formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ The general proposal is to specify the format via additional arguments to interp

The multiple-argument ones are currently syntax errors, so will not present backward compatibility problems. The intention is that keyword arguments expose the straightforward features, and more complicated features and extensions can be provided by Julia objects passed as second arguments.

### Implementation and performance considerations
### Implementation

Interpolation would call a function `stringformat`. The above would each call something like

Expand All @@ -132,7 +132,13 @@ Interpolation would call a function `stringformat`. The above would each call so

where `stringformat` is the appropriately defined function returning a formatted string.

However this may result in large numbers of intermediate allocations of small strings. Ideally we would want this to lower to something like
## Performance considerations

When considering performance, it is worth considering the desired result.

### Output to string

The user wants to output a Julia `String` object, similar to `string`/`@sprintf` behaviour. In this case, we want to avoid the intermediate allocations of the results of `stringformat`. Ideally we would want this to lower to something like

io = IOBuffer()
print(io, "π is approximately ")
Expand All @@ -141,18 +147,20 @@ However this may result in large numbers of intermediate allocations of small st
String(take!(io))

where `printformat` instead writes the formatted string directly to `io`.

If the string is to be written directly to IO device, then we would of course want to do that directly rather than to an intermediate buffer. Ideally we would like this to be possible via directly calling `print`, i.e.

print(io, "π is approximately ", stringformat(pi, fracdigits=4), ".")

If not, we could provide a macro `@print` which would do the necessary rewriting, e.g.
### Output to IO

@print(io, "π is approximately $(pi, fracdigits=4).")
The user wants to output directly to an IO device (say `IOStream` or `IOBuffer`). In this case, we want to avoid allocating any string at all, i.e. we want to lower

would be rewritten to
print(io, "π is approximately ", stringformat(pi, fracdigits=4), ".")

directly to

print(io, "π is approximately ")
printformat(io, pi, fracdigits=4)
print(io, ".")

If this is not possible, we could provide a macro `@print` which would do the necessary rewriting, e.g.

@print(io, "π is approximately $(pi, fracdigits=4).")