How to convert bool to Int?

8,887 views
Skip to first unread message

罗勇刚(Yonggang Luo)

unread,
May 7, 2014, 11:26:33 AM5/7/14
to Dart
if that's not possible, suggest to add toInt() method to bool

--
         此致

罗勇刚
Yours
    sincerely,
Yonggang Luo

Rhys Brett-Bowen

unread,
May 7, 2014, 11:33:33 AM5/7/14
to mi...@dartlang.org
int a = myBool ? 1 : 0;


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

罗勇刚(Yonggang Luo)

unread,
May 7, 2014, 11:47:56 AM5/7/14
to Dart
That's the answer, but not the one I am looking for. It's a bit verbose, anyway, thanks for your answer.

I am looking for a simpler way, such as bool.toInt().

Cogman

unread,
May 7, 2014, 12:43:06 PM5/7/14
to mi...@dartlang.org
int toInt(bool val) => val ? 1 : 0;

Throw this method in some utility class and then you can to int your bools in about the same speed as if they had added the functionality to the basic bool object.  

Usage:

toInt(true);

Peter Ahé

unread,
May 7, 2014, 2:08:25 PM5/7/14
to mi...@dartlang.org
If you think this belongs in the class bool, you can file a feature request at http://dartbug.com/new.

Cheers,
Peter

Matthew Butler

unread,
May 7, 2014, 2:11:42 PM5/7/14
to mi...@dartlang.org, luoyo...@gmail.com
I believe long ago there was discussion about this and in the opposite direction early on (pre-M1) of Dart. However there are a few problems with casting a bool to an int. For instance why arbitrarily '1' for true? Why not 500? Or -132001238 ?

Basically there are conventions that programmers (particularly from C/c-like languages) in which 1 == true and 0 == false, however other languages don't necessarily follow that same convention. Ruby for instance sees 0 as true (because it's not nil). Same for some other languages. Following that convention should true.toInt() return 1 and false.toInt() return null?

As mentioned by others, it would be best to implement your own method/function to provide the results you would expect in your own code.

:)

Matt

Randal L. Schwartz

unread,
May 7, 2014, 4:04:41 PM5/7/14
to mi...@dartlang.org
>>>>> "Matthew" == Matthew Butler <butler....@gmail.com> writes:

Matthew> Basically there are conventions that programmers (particularly
Matthew> from C/c-like languages) in which 1 == true and 0 == false,
Matthew> however other languages don't necessarily follow that same
Matthew> convention. Ruby for instance sees 0 as true (because it's not
Matthew> nil). Same for some other languages. Following that convention
Matthew> should true.toInt() return 1 and false.toInt() return null?

I remember some sort of microcomputer BASIC returned 0 for false, and -1
for true, being an integer where all bits were 1.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix consulting, Technical writing, Comedy, etc. etc.
Still trying to think of something clever for the fourth line of this .sig

罗勇刚(Yonggang Luo)

unread,
May 7, 2014, 10:43:33 PM5/7/14
to Dart

For bool, its a bit, if its not null, then it's natural for 0 stand for false, 1 stand for true,  if you want the reverse result, use ! Operator.
For the possible of being null, cause toInt is a member function, if it's null, call to this will cause exception, so that s not a problem.  I think, sometimes, following the obvious way is the best way, no need to create inconvenience intentionly

罗勇刚(Yonggang Luo)

unread,
May 9, 2014, 2:28:07 AM5/9/14
to Matthew Butler, Dart


2014年5月8日 上午2:11于 "Matthew Butler" <butler....@gmail.com>写道:
>
> I believe long ago there was discussion about this and in the opposite direction early on (pre-M1) of Dart. However there are a few problems with casting a bool to an int. For instance why arbitrarily '1' for true? Why not 500? Or -132001238 ?
>

its not based on other language, but the nature of bool itself,  bool stand for two state (true , false). can be naturally represent as 1/0. Why not true=0, false=1, its basically because true=1 is more convention, reference to mathematical logic.

Reply all
Reply to author
Forward
0 new messages