Page 1 of 1

Implicit conversion to bool

PostPosted: Fri Mar 22, 2013 7:25 am
by Nefarel
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?

Re: Implicit conversion to bool

PostPosted: Fri Mar 22, 2013 8:41 am
by Charles
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.

Re: Implicit conversion to bool

PostPosted: Fri Mar 22, 2013 1:37 pm
by torial
And Javascript has the concept of truthiness. If you know the semantics of the language, then it is just fine.

Re: Implicit conversion to bool

PostPosted: Fri Mar 22, 2013 1:49 pm
by nerdzero
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 :)

Re: Implicit conversion to bool

PostPosted: Fri Mar 29, 2013 8:41 pm
by hopscc
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)