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 readUtf8 convenience method #2955

Merged
merged 3 commits into from
Aug 9, 2022
Merged

Conversation

keirlawson
Copy link
Contributor

No description provided.

Comment on lines 426 to 427
def readUtf8(path: Path): F[String] =
readAll(path).through(text.utf8.decode).compile.lastOrError
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right? I think it should be

Suggested change
def readUtf8(path: Path): F[String] =
readAll(path).through(text.utf8.decode).compile.lastOrError
def readUtf8(path: Path): F[String] =
readAll(path).through(text.utf8.decode).compile.string

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signature wise, I think we should return a Stream[F, String] and let the user decide whether to accumulate, etc. I suggest these signatures:

def readUtf8(path: Path): Stream[F, String] =
  readAll(path).through(text.utf8.decode)

def readUtf8Lines(path: Path): Stream[F, String] =
  readUtf8(path).through(text.lines)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the accumulation case is likely very common (lots of time you just want to read in a file in a oner), could we also have an convenience method for it? No sure what you would call it though...

Copy link
Member

@mpilquist mpilquist Jul 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this?

def readUtf8AsSingleString(path: Path): F[String] =
  readUtf8(path).compile.string

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Please ignore me but) ehhh. It's super easy to tack on .compile.string and that's an ugly method name. I understand .through(text.utf8.decode) is annoying since it requires an import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect :) the name is a bit on the wordy side but I don't exactly have a better suggestion so that works for me :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the reason I'm keen to avoid the .compile.string is lots of the time I just want to read a file, and I don't care that it is streaming under the hood, its not hard obviously, but feels like the code shouldn't need to be concerned with streaming at all. This often comes up when teaching as well, ie we build toy apps that do things with files but I don't necessarily want to complicate things with streaming concerns. I do agree that the name is clumsy though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lean towards not including the F[String] variant but certainly sympathetic to concerns raised by @keirlawson. I do think the simple method names (e.g. readAll, readUtf8, readUtf8Lines) should all be stream based. I'm okay with a handful of conveniences that tack on stream compilation but they shouldn't be able to be mistaken for the "default" methods. Something you're intentionally reaching for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, have updated in line with this discussion

@mpilquist mpilquist merged commit 4539df9 into typelevel:main Aug 9, 2022
@keirlawson keirlawson deleted the add-readutf8 branch August 9, 2022 12:26
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

Successfully merging this pull request may close these issues.

3 participants