title: For-Comprehension Example with \/ and IO
body: <p>Given the following <code>Foo</code> case class:</p>
  
  <pre><code>scala&gt; case class Foo(x: Int)
  defined class Foo
  </code></pre>
  
  <p>I check if it's a valid <code>Foo</code> before constructing it inside of <code>validateFoo</code>:</p>
  
  <pre><code>scala&gt; def validateFoo(foo: Foo): \/[String, Foo] = 
                (if(foo.x &gt; 0) foo.success[String] 
                else ("invalid foo").failure[Foo]).disjunction
  validateFoo: (foo: Foo)scalaz.\/[String,Foo]
  </code></pre>
  
  <p>Lastly, <code>f</code> creates a <code>Foo</code>, and then tries to perform an <code>IO</code> action (example: save <code>Foo</code> to database).</p>
  
  <pre><code>scala&gt; def f(x: Foo): IO[\/[String,Int]] = for {
       |    foo &lt;- validateFoo(x)
       |    ioFoo &lt;- IO { foo }
       | } yield ioFoo
  &lt;console&gt;:19: error: type mismatch;
   found   : scalaz.effect.IO[Foo]
   required: scalaz.\/[?,?]
            ioFoo &lt;- IO { foo }
                  ^
  &lt;console&gt;:18: error: type mismatch;
   found   : scalaz.\/[String,Nothing]
   required: scalaz.effect.IO[scalaz.\/[String,Int]]
            foo &lt;- validateFoo(x)
                ^
  </code></pre>
  
  <p>However, I ran into the above issue when trying to chain the <code>bind</code>'s together in the for-comprehension.</p>
  
  <p>The problem, as I see it, is that the proper return type is <code>IO[\/[String, Int]]</code>. However, I don't know how to validate <code>Foo</code> <strong>and</strong> handle it in the <code>IO</code> monad with a for-comprehension.</p>