You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Strange behavior of && operator (maybe also exists with other logic operation) #37
Given a method that accepts a call-by-name parameter, it means the method decides when and how many times the parameter evaluates.
Boolean.&& is one of that kind of methods. Technically, there is no way to pass a monadic expression to methods like that. You could not pass an Option[Boolean] to a => Boolean typed method because type mismatch.
Instead, the only thing monadic macro can do is evaluating the monadic expression before passing it to a method. The following code is generated by monadic for you example code.
{
valmonad$macro$1: scalaz.Traverse[Option] with scalaz.MonadPlus[Option] with scalaz.Each[Option] with scalaz.Index[Option] with scalaz.Length[Option] with scalaz.Cozip[Option] with scalaz.Zip[Option] with scalaz.Unzip[Option] with scalaz.Align[Option] with scalaz.IsEmpty[Option] with scalaz.Cobind[Option] with scalaz.Optional[Option]{defpoint[A](a: =>A):Some[A]; defcojoin[A](a: Option[A]):Option[Some[A]]} = scalaz.std.option.optionInstance;
monad$macro$1.bind(com.thoughtworks.each.Monadic.EachOps[Some, Int](scala.Some.apply[Int](1)).underlying)(((element$macro$2: Int) => {
vali:Boolean= element$macro$2.<(0);
monad$macro$1.map({
scala.this.Predef.println("foo");
monad$macro$1.map(com.thoughtworks.each.Monadic.EachOps[Some, Int](scala.Some.apply[Int](2)).underlying)(((element$macro$3: Int) => element$macro$3.>(0)))
})(((element$macro$4: Boolean) => i.&&(element$macro$4)))
}))
}
In brief, there is no way to pass a monadic expression to a method that accept call-by-name parameters and still keep the same behavior as the behavior when passing a direct expression. No way, even if you manually create the monadic expression without using Each library.
In order to achieve the correspond behavior with a monadic expression, you must replace call-by-name parameter => T to a explicit function () => M[T], for example
Activity
Atry commentedon Nov 2, 2015
Given a method that accepts a call-by-name parameter, it means the method decides when and how many times the parameter evaluates.
Boolean.&&is one of that kind of methods. Technically, there is no way to pass a monadic expression to methods like that. You could not pass anOption[Boolean]to a=> Booleantyped method because type mismatch.Instead, the only thing
monadicmacro can do is evaluating the monadic expression before passing it to a method. The following code is generated bymonadicfor you example code.{ val monad$macro$1: scalaz.Traverse[Option] with scalaz.MonadPlus[Option] with scalaz.Each[Option] with scalaz.Index[Option] with scalaz.Length[Option] with scalaz.Cozip[Option] with scalaz.Zip[Option] with scalaz.Unzip[Option] with scalaz.Align[Option] with scalaz.IsEmpty[Option] with scalaz.Cobind[Option] with scalaz.Optional[Option]{def point[A](a: => A): Some[A]; def cojoin[A](a: Option[A]): Option[Some[A]]} = scalaz.std.option.optionInstance; monad$macro$1.bind(com.thoughtworks.each.Monadic.EachOps[Some, Int](scala.Some.apply[Int](1)).underlying)(((element$macro$2: Int) => { val i: Boolean = element$macro$2.<(0); monad$macro$1.map({ scala.this.Predef.println("foo"); monad$macro$1.map(com.thoughtworks.each.Monadic.EachOps[Some, Int](scala.Some.apply[Int](2)).underlying)(((element$macro$3: Int) => element$macro$3.>(0))) })(((element$macro$4: Boolean) => i.&&(element$macro$4))) })) }In brief, there is no way to pass a monadic expression to a method that accept call-by-name parameters and still keep the same behavior as the behavior when passing a direct expression. No way, even if you manually create the monadic expression without using Each library.
Atry commentedon Nov 2, 2015
In order to achieve the correspond behavior with a monadic expression, you must replace call-by-name parameter
=> Tto a explicit function() => M[T], for examplejilen commentedon Dec 10, 2015
We should point this out in the
READMEfile.For a trade system like this will lead to terrible behavior
Atry commentedon Dec 10, 2015
Could you create a Pull Request to the
README.md?Atry commentedon Jan 8, 2016
How do you think if we translate
Boolean.&&to aifexpressions to enable behavior like a call-by-name parameter?jilen commentedon Jan 8, 2016
That seems reasonable. And it will be better if we can raise an warning for other
call-by-nameparameter.Atry commentedon Mar 20, 2017
I think it's better to disable
.eachmagic in a call-by-value parameters.The following code should not compile any more
jilen commentedon Mar 20, 2017
@Atry Can't agree anymore
MarisaKirisame commentedon Mar 20, 2017
👍
Update sbt-sonatype to 3.9.16 in template (ThoughtWorksInc#37)