Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 932507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:40:48+00:00 2026-05-15T20:40:48+00:00

I try to define the Reader monad with scalaz like this: import scalaz._ import

  • 0

I try to define the Reader monad with scalaz like this:

import scalaz._
import Scalaz._

final class Reader[E,A](private[Reader] val runReader: E => A)

object Reader {
  def apply[E,A](f: E => A) = new Reader[E,A](f)
  def env[E]: Reader[E,E] = Reader(identity _)

  implicit def ReaderMonad[E] = new Monad[PartialApply1Of2[Reader,E]#Apply] {
    def pure[A](a: => A) = Reader(_ => a)

    def bind[A,B](m: Reader[E,A], k: A => Reader[E,B]) =
      Reader(e => k(m.runReader(e)).runReader(e))
  }
}


object Test {
  import Reader._

  class Env(val s: String)

  def post(s: String): Reader[Env, Option[String]] =
    env >>= (e => if (e.s == s) some(s).pure else none.pure)
}

but I get a compiler error:

reader.scala:27: reassignment to val
     env >>= (e => if (e.s == s) some(s).pure else none.pure)
         ^

Why is that?

Thanks,
Levi

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:40:49+00:00Added an answer on May 15, 2026 at 8:40 pm

    This error is fairly opaque, even by Scala’s standards. Method names ending with = are treated specially — they are first considered as a normal identifier, and failing that, they are expanded to a self assignment.

    scala> def env[A] = 0
    env: [A]Int
    
    scala> env >>= 0
    <console>:7: error: reassignment to val
           env >>= 0
               ^
    
    scala> env = env >> 0
    <console>:6: error: reassignment to val
           env = env >> 0
               ^
    

    If you’re confused about the syntactic interpretation of your program, it’s a good idea to run scalac -Xprint:parser to see what’s going on. Similarly, you can use -Xprint:typer or -Xprint:jvm to see later phases of the program transformation.

    So, how do you call >>= on your Reader? First of all, you’ll need to explicitly pass the type argument Env to env. The resulting Reader[Env, Env] must then be converted to a MA[M[_], A]. For simple type constructors, the implicit conversion MAs#ma will suffice. However the two param type constructor Reader must be partially applied — this means it can’t be inferred and instead you must provide a specific implicit conversion.

    The situation would be vastly improved if Adriaan ever finds a spare afternoon to implement higher-order unification for type constructor inference. 🙂

    Until then, here’s your code. A few more comments are inline.

    import scalaz._
    import Scalaz._
    
    final class Reader[E, A](private[Reader] val runReader: E => A)
    
    object Reader {
      def apply[E, A](f: E => A) = new Reader[E, A](f)
    
      def env[E]: Reader[E, E] = Reader(identity _)
    
      implicit def ReaderMonad[E]: Monad[PartialApply1Of2[Reader, E]#Apply] = new Monad[PartialApply1Of2[Reader, E]#Apply] {
        def pure[A](a: => A) = Reader(_ => a)
    
        def bind[A, B](m: Reader[E, A], k: A => Reader[E, B]) =
          Reader(e => k(m.runReader(e)).runReader(e))
      }
    
      // No Higher Order Unification in Scala, so we need partially applied type constructors cannot be inferred.
      // That's the main reason for defining function in Scalaz on MA, we can create one implicit conversion
      // to extract the partially applied type constructor in the type parameter `M` of `MA[M[_], A]`.
      //
      // I'm in the habit of explicitly annotating the return types of implicit defs, it's not strictly necessary
      // but there are a few corner cases it pays to avoid.
      implicit def ReaderMA[E, A](r: Reader[E, A]): MA[PartialApply1Of2[Reader, E]#Apply, A] = ma[PartialApply1Of2[Reader, E]#Apply, A](r)
    }
    
    
    object Test {
      import Reader._
    
      class Env(val s: String)
    
      def post(s: String): Reader[Env, Option[String]] =
        // Need to pass the type arg `Env` explicitly here.
        env[Env] >>= {e =>
          // Intermediate value and type annotation not needed, just here for clarity.
          val o: Option[String] = (e.s === s).guard[Option](s)
          // Again, the partially applied type constructor can't be inferred, so we have to explicitly pass it.
          o.pure[PartialApply1Of2[Reader, Env]#Apply]
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this function: (define (try try-block catch-block finally-block) ; Implements try/catch/finally like
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
Is that possible to define a function without referencing to self this way? def
I have a header menu and try to define different CSS classes for each
I know how to do a global variable, but whenever I try to define
I get this error FB is not defined when i try to run this
try: recursive_function() except RuntimeError e: # is this a max. recursion depth exceeded exception?
when I try to render the view, browser show this error 01:46:11,371 GRAVE [javax.enterprise.resource.webcontainer.jsf.application]
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import java.io.FileReader; public class Main { public static void main(String[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.