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

Fix Free Monad example #4253

Merged
merged 1 commit into from
Jun 25, 2022
Merged

Fix Free Monad example #4253

merged 1 commit into from
Jun 25, 2022

Conversation

youta32449999
Copy link
Contributor

Type Mismatch Error occurred impureCompiler and pureCompiler in scalaVersion 3.1.3

kvs.get(key).map(_.asInstanceOf[A])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Found:    Option[A]
Required: cats.Id[A]

State.inspect(_.get(key).map(_.asInstanceOf[A]))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Found:    Option[A]
Required: A

Copy link
Member

@armanbilge armanbilge left a comment

Choose a reason for hiding this comment

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

Thanks!

@armanbilge armanbilge merged commit e7499ff into typelevel:main Jun 25, 2022
State.inspect(_.get(key).map(_.asInstanceOf[A]))
State.inspect(_.get(key).asInstanceOf[A])
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I may have merged this a bit too hastily 😓

.get returns an Option, so we can't cast to it to A. Seems like what we should be doing here is directly calling .apply on the key.

Copy link
Member

Choose a reason for hiding this comment

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

Also, it's annoying that mdoc in CI passes for this even if it's not working.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for review my pull request.

The definition of Get is case class Get[T](key: String) extends KVStoreA[Option[T]].
In this case, I think type A is Option[T]🤔.

Copy link
Member

Choose a reason for hiding this comment

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

@youta32449999 ah, indeed your are right.,I am confusing A with T. Hasty reviewing again! 😅 Sorry about that, and thanks for clarifying. I guess the reason it worked before is because of type erasure at runtime, so that the mistake did not matter.

It looks to be correct on the website. Thank you again for fixing this!
https://typelevel.org/cats/datatypes/freemonad.html#_5-run-your-program

Copy link
Member

Choose a reason for hiding this comment

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

I think I am also confused by the comment.

// the program will crash if a key is not found,

Since Get returns an Option, in what situation will it crash?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I think it will crash if a type is incorrectly specified.
For example:

def program: KVStore[Option[Int]] = for {
  _ <- put("wild-cats", "two")  // expect Int, but value is String.
  _ <- update[Int]("wild-cats", (_ + 12))
  _ <- put("tame-cats", 5)
  n <- get[Int]("wild-cats")
  _ <- delete("tame-cats")
} yield n

So, I'm going to fix the comment as below.
the program will crash if a type is incorrectly specified.

@armanbilge armanbilge changed the title Documentation: update freemonad.md Fix Free Monad example Jun 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants