Redefining truthy and falsey values

Description

Consider adopting some behaviors from languages such as JavaScript or Groovy which allow for more clever conditional statements to be constructed. Consider this ticket a discussion on whether this is even possible and what implications it would have. I assume this might also require an on/off flag such as the null support feature.

This table outlines a suggestion of how the following values would behave when cast to booleans:

Value

Truthiness

null

false

Number

false if 0, otherwise true

String - Option 1

false if "0", or "false", or "no"; otherwise true if non-empty
"" throws exception

String - Option 2

false if "", or "0", or "false", or "no"; otherwise true if non-empty

Array

false if empty, otherwise true

Struct

false if empty, otherwise true

Object

true or based on CFC's toBoolean() method

This would allow for the full ternary version of the Elvis operator (Assuming full null support):

It would also simplify checking for the contents of structs and arrays:

I think these changes might be somewhat straightforward and mostly affect the Caster class, but most importantly:

  1. Do we want this in CFML?

  2. Would this have serious implications in backwards compatibility?

Other things to consider that are similar to Groovy would be Java Map, List and Iterator classes. True if not empty, or hasNext() is true.

References:

Activity

Ed Sanford 
16 February 2016 at 07:00

I would go a step further, and provided a flag allowing it were set, treat not only 0,"0","no","false", false, null and empty as false but undefined as false as well.

I like the idea of being able to write less code while still having it be readable, more often.

would be

Adam Cameron 
11 February 2016 at 15:57

Cheers for the elaboration Micha: that's all very clear now.

Given this:
> so for example "0" and 0 always need to have the same meaning in every context

I now think even more that this is a complete no-go for CFML. But with .lucee as you don't need to be hung-up by CFML's need to "0" and 0 to be boolean equivalents, then this is less of an issue.

Michael Offner 
11 February 2016 at 15:27

Let me make an other more simple example:

In ACF you will get the output "java.lang.String" and in Lucee "java.lang.Double" ( that is the reason I did 1+1 in the first example, to have a double with both engines, to keep it simple).

My point is that simple types in CFML are not that clear at all for the average CFML developer, even you are using a literal value or the type of a function argument is clearly defined, you still can have a different type as expected. So the cast of a simple type has to be as clear as possible, so for example "0" and 0 always need to have the same meaning in every context (expect things like jsonSerialize of course).

One point I completely miss in this ticket btw are date objects, they are also simple types, mean you can also convert them to numbers,boolean,strings ...

If someone does not understand what I'm writing and he is interested in what I'm trying to say, he is welcome to ask for clarification, something I do a lot myself in English conversations. So thanks for the clarification on my comment.

Igal Sapir 
11 February 2016 at 14:12

LOL!

Adam Cameron 
11 February 2016 at 14:10

> not be offensive to others

You're the only one taking offence, mate; and I wasn't even talking to you.

And there was nothing blimin' offensive about what I said anyhow. What Micha wrote really isn't that clear, and I think it's mostly down to his command of English not being that excellent. There's nothing wrong with that, but it gets in the way some times.

You seem to be looking around for things to be offended by. I think that's not a good use of your time, and it's not a good use of anyone else's time when you find something to be offended by and tell us. It also doesn't present you in a very good light. I recommend you don't bother.

Details

Assignee

Reporter

Labels

New Issue warning screen

Before you create a new Issue, please post to the mailing list first https://dev.lucee.org

Once the issue has been verified, one of the Lucee team will ask you to file an issue

Priority

Created 22 July 2015 at 19:20
Updated 2 February 2021 at 15:39