Forums

Implicit conversion to bool

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Implicit conversion to bool

Postby Nefarel » Fri Mar 22, 2013 7:25 am

In C they didnt have bool type so they have made implicit conversions to bool in control statements like:

Code: Select all
while(length) // length is integer
{
}

if (car) // car is a pointer
{
}


In C#/Java they have removed that conversions, but Cobra doesn't. It is shorter syntax, but it is less clear and doesnt read naturally in english :) So I think Cobra have made things worse here. What do u think?
Nefarel
 
Posts: 6

Re: Implicit conversion to bool

Postby Charles » Fri Mar 22, 2013 8:41 am

Well, I'm biased since I created the language, but I'm fine with it. :)

Python is another language that allows anything to be used in "if" and "while" statements. I felt comfortable with that experience so I carried the design over to Cobra.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Implicit conversion to bool

Postby torial » Fri Mar 22, 2013 1:37 pm

And Javascript has the concept of truthiness. If you know the semantics of the language, then it is just fine.
torial
 
Posts: 229
Location: IA

Re: Implicit conversion to bool

Postby nerdzero » Fri Mar 22, 2013 1:49 pm

I see what you're saying, Nefarel. I tend to agree. Usually I'm explicit about it even though I don't need to be.

while length <> 0
pass

if car <> nil
pass

As long as you remember 0 is false, nil is false, and everything else is true, you are good to go. You can probably get into trouble with nilable ints and bools though :)
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Implicit conversion to bool

Postby hopscc » Fri Mar 29, 2013 8:41 pm

Usually I like to present things explicitly as well but here I find the boolean 'truthiness' inference on non booleans
both more (notationally) convenient and clearer to understand .

Especially since the truthness options chosen match the most common checks
0 = false (lengths and counts)
nil = false ( nil references)

if list.count
# process list contents otherwise its empty

if string.length # same as string <> ''
# process string contents otherwise its empty

s as Thing?
if s # same as s <> nil
# process s otherwise its a nil ref


Its probably just internalising the semantics (and saving some exposition)
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand


Return to Discussion

Who is online

Users browsing this forum: No registered users and 9 guests

cron