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

Add read(io, ::Tuple) method to read multiple values at once #42551

Open
sostock opened this issue Oct 8, 2021 · 1 comment
Open

Add read(io, ::Tuple) method to read multiple values at once #42551

sostock opened this issue Oct 8, 2021 · 1 comment
Labels
domain:io Involving the I/O subsystem: libuv, read, write, etc. kind:feature Indicates new feature / enhancement requests

Comments

@sostock
Copy link
Contributor

sostock commented Oct 8, 2021

Inspired by this discourse thread, I think it would be useful to add a Tuple method to read (and write), so that one can do

a, b, c = read(file, Tuple{UInt8, Float32, Int16})

instead of

a = read(file, UInt8)
b = read(file, Float32)
c = read(file, Int16)

One can already work around this in a couple of ways, like

a, b, c = (read(file, T) for T = (UInt8, Float32, Int16))
a, b, c = read.(Ref(file), (UInt8, Float32, Int16))
a, b, c = map(T -> read(file, T), (UInt8, Float32, Int16))

but I think it would be nice to have an explicit method for it.

@sostock sostock added the kind:feature Indicates new feature / enhancement requests label Oct 8, 2021
@JeffBezanson JeffBezanson added the domain:io Involving the I/O subsystem: libuv, read, write, etc. label Oct 8, 2021
@JeffBezanson
Copy link
Sponsor Member

One issue here is alignment. A type like Tuple{UInt8, Float32} has alignment padding, so reading one is not the same as reading a UInt8 followed by a Float32. We could maybe use read(file, UInt8, Float32), but that prevents adding new trailing positional arguments to read in the future, and is also surprisingly hard to compile. For now a macro is probably the best solution actually, a, b, c = @read file UInt8 Float32 Int16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:io Involving the I/O subsystem: libuv, read, write, etc. kind:feature Indicates new feature / enhancement requests
Projects
None yet
Development

No branches or pull requests

2 participants