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

New constructors for Writer? #920

Closed
davegurnell opened this issue Mar 7, 2016 · 1 comment
Closed

New constructors for Writer? #920

davegurnell opened this issue Mar 7, 2016 · 1 comment

Comments

@davegurnell
Copy link

I'd like to propose adding direct support for constructing a Writer from a log, and ensuring that there are syntactically similar equivalents for creating from a log, a result, and a log and a result together.

There's currently support for creating Writers using the pure syntax and the Writer.apply method. However, any other constructors require you to know about WriterT.

One possibility would be to add a tell syntax like the one in Scalaz:

scala> import scalaz._, Scalaz._

scala> Vector("Log message 1", "Log message 2").tell
// res1: Writer[Vector[String],Unit] = 
//   WriterT((Vector(Log message 1, Log message 2),()))

This would give us pure and tell to create from a result or a log, and Writer.apply to create from both values together.

One could also add an equivalent of Scalaz' set syntax, although IMO the name would have to change:

scala> "Result".set(Vector("Log message 1", "Log message 2"))
// res2: Writer[Vector[String],String] =
//   WriterT((Vector(Log message 1, Log message 2),Result))

Another possibility would be adding Writer.tell and Writer.put / Writer.value to mirror the constructors on WriterT.

I realise this is duplicating functionality that's already there with WriterT, but I think there's a cleaner learning curve if people can learn about Reader, Writer, and State before they jump into monad transformers.

Keen to know peoples' thoughts!

@davegurnell
Copy link
Author

Concretely, I propose adding two bits of syntax:

logValue.tell

resultValue.writer(logValue)

The former is a direct port from Scalaz. The latter is like the set syntax un Scalaz, but renamed for clarity. Combined with pure, this would provide comprehensive syntax for creating Writers.

The syntax would go in cats.syntax.writer, be included in cats.syntax.all, and have tests in WriterSpec.

@stew stew closed this as completed in 989d728 Mar 9, 2016
stew added a commit that referenced this issue Mar 9, 2016
Add .tell and .writer syntax for creating Writers. Fixes #920.
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

No branches or pull requests

1 participant