This page is no longer maintained — Please continue to the home page at www.scala-lang.org

'and', 'or' and 'not' operators for booleans

9 replies
rlemaire
Joined: 2009-11-06,
User offline. Last seen 2 years 46 weeks ago.
Hi.

This is the first time I post on this list so hello everybody !

I learned scala a few months ago (although I've tried it before that) and really like it. I consider giving time to improve the tools in the close future.

Like other c-like languages it uses '&&', '||' and '!' as, respectively 'and', 'or' and 'not'. And ever since I use c-like languages (mostly java ...) I miss the 'and', 'or' and 'not ' keywords.

I just started a topic on the forum about that.

Of course it's very easy to add it with an implicit conversion, but an explicit import is needed and it's even easier to add it in the Boolean class. Is there any plan to do it ?

I'm sorry if it sounds like a silly request, focused on a detail. I think it would make scala statements more expressive, and internal DSLs even easier.

Thanks for reading,

Regards
--
Raphaël Lemaire
anli
Joined: 2008-08-19,
User offline. Last seen 1 day 19 min ago.
Re: 'and', 'or' and 'not' operators for booleans

On Monday 07 December 2009 23:37:25 Raphaël Lemaire wrote:
> Hi.
>
> This is the first time I post on this list so hello everybody !
>
> I learned scala a few months ago (although I've tried it before that) and
> really like it. I consider giving time to improve the tools in the close
> future.
>
> Like other c-like languages it uses '&&', '||' and '!' as, respectively
> 'and', 'or' and 'not'. And ever since I use c-like languages (mostly java
> ...) I miss the 'and', 'or' and 'not ' keywords.
>
> I just started a topic
> on the forum about
> that.
>
> Of course it's very easy to add it with an implicit conversion, but an
> explicit import is needed and it's even easier to add it in the Boolean
> class. Is there any plan to do it ?
>
> I'm sorry if it sounds like a silly request, focused on a detail. I think
> it would make scala statements more expressive, and internal DSLs even
> easier.

If I understand correctly, it isn't as simple as synonyms: there are
precedence rules concerning operator-like methods names. But as for all others
(more readabilty - especially wrt ! name) - agree.

ewilligers
Joined: 2008-08-20,
User offline. Last seen 3 years 17 weeks ago.
Re: 'and', 'or' and 'not' operators for booleans

Raphaël Lemaire wrote:
> Like other c-like languages it uses '&&', '||' and '!' as, respectively
> 'and', 'or' and 'not'. And ever since I use c-like languages (mostly
> java ...) I miss the 'and', 'or' and 'not ' keywords.

> ... I
> think it would make scala statements more expressive, and internal DSLs
> even easier.

Hi Raphaël,

Welcome to the list.

For DSLs readable by non-developers, using implicit "keywords" makes sense.

For developers, I think it is important to use the existing language
rather than recreate Bournegol
http://minnie.tuhs.org/UnixTree/V7/usr/src/cmd/sh/mac.h.html

And the existing language wasn't designed in a vacuum, it's target
audience is most familiar with c-like languages. I think there's been a
similar discussion in the past regarding either mod/div or if/then.

Best Regards,
Eric.

Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: 'and', 'or' and 'not' operators for booleans

I have added my own methods to Boolean using implicits. The single
required import gets a whole lot more than just that though. In other
words, soon you'll be wanting other things so why not get started here?

http://code.google.com/p/scalaz/source/browse/trunk/core/src/main/scala/...

Raphaël Lemaire wrote:
> Hi.
>
> This is the first time I post on this list so hello everybody !
>
> I learned scala a few months ago (although I've tried it before that)
> and really like it. I consider giving time to improve the tools in the
> close future.
>
> Like other c-like languages it uses '&&', '||' and '!' as,
> respectively 'and', 'or' and 'not'. And ever since I use c-like
> languages (mostly java ...) I miss the 'and', 'or' and 'not ' keywords.
>
> I just started a topic
> on the forum about
> that.
>
> Of course it's very easy to add it with an implicit conversion, but an
> explicit import is needed and it's even easier to add it in the
> Boolean class. Is there any plan to do it ?
>
> I'm sorry if it sounds like a silly request, focused on a detail. I
> think it would make scala statements more expressive, and internal
> DSLs even easier.
>
> Thanks for reading,
>
> Regards
> --
> Raphaël Lemaire

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Re: 'and', 'or' and 'not' operators for booleans

A problem with that would be the precedence. One would probably
prefer a and b == c and d to parse as (a and b) == (c and d), but with
Scala's precedence rules, it would parse as a and (b == c) and d.

2009/12/7 Eric Willigers :
> Raphaël Lemaire wrote:
>>
>> Like other c-like languages it uses '&&', '||' and '!' as, respectively 'and', 'or' and 'not'. And ever since I use c-like languages (mostly java ...) I miss the 'and', 'or' and 'not ' keywords.
>
>> ... I think it would make scala statements more expressive, and internal DSLs even easier.
>
> Hi Raphaël,
>
> Welcome to the list.
>
> For DSLs readable by non-developers, using implicit "keywords" makes sense.
>
> For developers, I think it is important to use the existing language rather than recreate Bournegol
>  http://minnie.tuhs.org/UnixTree/V7/usr/src/cmd/sh/mac.h.html
>
> And the existing language wasn't designed in a vacuum, it's target audience is most familiar with c-like languages. I think there's been a similar discussion in the past regarding either mod/div or if/then.
>
> Best Regards,
> Eric.
>
>

rlemaire
Joined: 2009-11-06,
User offline. Last seen 2 years 46 weeks ago.
Re: 'and', 'or' and 'not' operators for booleans
Hello,

You project is interesting. And indeed  I find here some things that I could have wanted (like 'unless')
--
Raphaël Lemaire


2009/12/7 Tony Morris <tonymorris [at] gmail [dot] com>
I have added my own methods to Boolean using implicits. The single
required import gets a whole lot more than just that though. In other
words, soon you'll be wanting other things so why not get started here?

http://code.google.com/p/scalaz/source/browse/trunk/core/src/main/scala/scalaz/BooleanW.scala

Raphaël Lemaire wrote:
> Hi.
>
> This is the first time I post on this list so hello everybody !
>
> I learned scala a few months ago (although I've tried it before that)
> and really like it. I consider giving time to improve the tools in the
> close future.
>
> Like other c-like languages it uses '&&', '||' and '!' as,
> respectively 'and', 'or' and 'not'. And ever since I use c-like
> languages (mostly java ...) I miss the 'and', 'or' and 'not ' keywords.
>
> I just started a topic
> <http://scala-forum.org/read.php?4,788,788#msg-788> on the forum about
> that.
>
> Of course it's very easy to add it with an implicit conversion, but an
> explicit import is needed and it's even easier to add it in the
> Boolean class. Is there any plan to do it ?
>
> I'm sorry if it sounds like a silly request, focused on a detail. I
> think it would make scala statements more expressive, and internal
> DSLs even easier.
>
> Thanks for reading,
>
> Regards
> --
> Raphaël Lemaire

--
Tony Morris
http://tmorris.net/



rlemaire
Joined: 2009-11-06,
User offline. Last seen 2 years 46 weeks ago.
Re: Re: 'and', 'or' and 'not' operators for booleans
Yes, that would be a problem.
--
Raphaël Lemaire

06 70 59 26 34


On Tue, Dec 8, 2009 at 00:19, Ricky Clarkson <ricky [dot] clarkson [at] gmail [dot] com> wrote:
A problem with that would be the precedence.  One would probably
prefer a and b == c and d to parse as (a and b) == (c and d), but with
Scala's precedence rules, it would parse as a and (b == c) and d.

2009/12/7 Eric Willigers <ewilligers [at] gmail [dot] com>:
> Raphaël Lemaire wrote:
>>
>> Like other c-like languages it uses '&&', '||' and '!' as, respectively 'and', 'or' and 'not'. And ever since I use c-like languages (mostly java ...) I miss the 'and', 'or' and 'not ' keywords.
>
>> ... I think it would make scala statements more expressive, and internal DSLs even easier.
>
> Hi Raphaël,
>
> Welcome to the list.
>
> For DSLs readable by non-developers, using implicit "keywords" makes sense.
>
> For developers, I think it is important to use the existing language rather than recreate Bournegol
>  http://minnie.tuhs.org/UnixTree/V7/usr/src/cmd/sh/mac.h.html
>
> And the existing language wasn't designed in a vacuum, it's target audience is most familiar with c-like languages. I think there's been a similar discussion in the past regarding either mod/div or if/then.
>
> Best Regards,
> Eric.
>
>



--
Ricky Clarkson
Java and Scala Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky [dot] clarkson [at] gmail [dot] com
Google Wave: ricky [dot] clarkson [at] googlewave [dot] com

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: 'and', 'or' and 'not' operators for booleans
I am not sure why one would prefer text operators to symbolic ones if one is doing more than a little logic.  People who do logic professionally (e.g. mathematicians, philosophers) always use the symbolic form.  They even invent symbols for lesser-used logical constructs like "there exists".

If one is using only a little logic, then the lack of short-circuiting and the different precedence of the text operators shouldn't impose that much of a burden.

(And anyway, (a && b==c && d) parses that way with the boolean operators also.  Comparing whether two truth values are the same is the "surprising" operation; normally, you want to compare objects in a compound logical statement.)

  --Rex

P.S. To verify the precedence, try this:
  case class Bool(b:Boolean) { def and(c:Boolean) = (b&&c) }
  implicit def wrapBoolean(b:Boolean) = Bool(b)
  implicit def unwrapBoolean(b:Bool) = b.b
  (true and false == false and false)
  (true and false) == (false and false)
  (true and (false == false) and false)
  (true && false == false && false)
  (true && false) == (false && false)
  (true && (false == false) && false)

2009/12/7 Raphaël Lemaire <lemaire [dot] raphael [at] gmail [dot] com>
Hi.

This is the first time I post on this list so hello everybody !

I learned scala a few months ago (although I've tried it before that) and really like it. I consider giving time to improve the tools in the close future.

Like other c-like languages it uses '&&', '||' and '!' as, respectively 'and', 'or' and 'not'. And ever since I use c-like languages (mostly java ...) I miss the 'and', 'or' and 'not ' keywords.

I just started a topic on the forum about that.

Of course it's very easy to add it with an implicit conversion, but an explicit import is needed and it's even easier to add it in the Boolean class. Is there any plan to do it ?

I'm sorry if it sounds like a silly request, focused on a detail. I think it would make scala statements more expressive, and internal DSLs even easier.

Thanks for reading,

Regards
--
Raphaël Lemaire

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: 'and', 'or' and 'not' operators for booleans

You are correct, though there would be other cases where the
difference in precedence between && and 'and' would cause issues.

2009/12/8 Rex Kerr :
> I am not sure why one would prefer text operators to symbolic ones if one is
> doing more than a little logic.  People who do logic professionally (e.g.
> mathematicians, philosophers) always use the symbolic form.  They even
> invent symbols for lesser-used logical constructs like "there exists".
>
> If one is using only a little logic, then the lack of short-circuiting and
> the different precedence of the text operators shouldn't impose that much of
> a burden.
>
> (And anyway, (a && b==c && d) parses that way with the boolean operators
> also.  Comparing whether two truth values are the same is the "surprising"
> operation; normally, you want to compare objects in a compound logical
> statement.)
>
>   --Rex
>
> P.S. To verify the precedence, try this:
>   case class Bool(b:Boolean) { def and(c:Boolean) = (b&&c) }
>   implicit def wrapBoolean(b:Boolean) = Bool(b)
>   implicit def unwrapBoolean(b:Bool) = b.b
>   (true and false == false and false)
>   (true and false) == (false and false)
>   (true and (false == false) and false)
>   (true && false == false && false)
>   (true && false) == (false && false)
>   (true && (false == false) && false)
>
> 2009/12/7 Raphaël Lemaire
>>
>> Hi.
>>
>> This is the first time I post on this list so hello everybody !
>>
>> I learned scala a few months ago (although I've tried it before that) and
>> really like it. I consider giving time to improve the tools in the close
>> future.
>>
>> Like other c-like languages it uses '&&', '||' and '!' as, respectively
>> 'and', 'or' and 'not'. And ever since I use c-like languages (mostly java
>> ...) I miss the 'and', 'or' and 'not ' keywords.
>>
>> I just started a topic on the forum about that.
>>
>> Of course it's very easy to add it with an implicit conversion, but an
>> explicit import is needed and it's even easier to add it in the Boolean
>> class. Is there any plan to do it ?
>>
>> I'm sorry if it sounds like a silly request, focused on a detail. I think
>> it would make scala statements more expressive, and internal DSLs even
>> easier.
>>
>> Thanks for reading,
>>
>> Regards
>> --
>> Raphaël Lemaire
>
>

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: 'and', 'or' and 'not' operators for booleans
To make the code read like business rules. Which is a bit dangerous, actually, as sometimes "or" is used to mean "and" and vice versa.

On Tue, Dec 8, 2009 at 1:47 PM, Rex Kerr <ichoran [at] gmail [dot] com> wrote:
I am not sure why one would prefer text operators to symbolic ones if one is doing more than a little logic.  People who do logic professionally (e.g. mathematicians, philosophers) always use the symbolic form.  They even invent symbols for lesser-used logical constructs like "there exists".

If one is using only a little logic, then the lack of short-circuiting and the different precedence of the text operators shouldn't impose that much of a burden.

(And anyway, (a && b==c && d) parses that way with the boolean operators also.  Comparing whether two truth values are the same is the "surprising" operation; normally, you want to compare objects in a compound logical statement.)

  --Rex

P.S. To verify the precedence, try this:
  case class Bool(b:Boolean) { def and(c:Boolean) = (b&&c) }
  implicit def wrapBoolean(b:Boolean) = Bool(b)
  implicit def unwrapBoolean(b:Bool) = b.b
  (true and false == false and false)
  (true and false) == (false and false)
  (true and (false == false) and false)
  (true && false == false && false)
  (true && false) == (false && false)
  (true && (false == false) && false)

2009/12/7 Raphaël Lemaire <lemaire [dot] raphael [at] gmail [dot] com>
Hi.

This is the first time I post on this list so hello everybody !

I learned scala a few months ago (although I've tried it before that) and really like it. I consider giving time to improve the tools in the close future.

Like other c-like languages it uses '&&', '||' and '!' as, respectively 'and', 'or' and 'not'. And ever since I use c-like languages (mostly java ...) I miss the 'and', 'or' and 'not ' keywords.

I just started a topic on the forum about that.

Of course it's very easy to add it with an implicit conversion, but an explicit import is needed and it's even easier to add it in the Boolean class. Is there any plan to do it ?

I'm sorry if it sounds like a silly request, focused on a detail. I think it would make scala statements more expressive, and internal DSLs even easier.

Thanks for reading,

Regards
--
Raphaël Lemaire




--
Daniel C. Sobral

I travel to the future all the time.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland