Condition Evaluation Consistency

From NDjango
Revision as of 21:46, 11 June 2009 by (Talk) (How NDjango evaluates conditions in if tag)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

How NDjango evaluates conditions in if tag

At this time - expression is processed by the following function: <source lang="ocaml"> let eval context (bool_expr: FilterExpression) =

   match fst (bool_expr.Resolve context true) with
   | None -> false
   | Some v -> 
       match v with 
       | :? System.Boolean as b -> b                           // boolean value, take literal
       | :? IEnumerable as e -> e.GetEnumerator().MoveNext()   // some sort of collection, take if empty
       | null -> false                                         // null evaluates to false
       | _ -> true                                             // anything else. true because it's there

</source> so, false, empty IEnumerable and null are treated as false

How Django/python evaluates conditions

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

  • None
  • False
  • zero of any numeric type, for example, 0, 0L, 0.0, 0j.
  • any empty sequence, for example, , (), [].
  • any empty mapping, for example, {}. (not our case)
  • instances of user-defined classes, if the class defines a __nonzero__() or __len__() method, when that method returns the integer zero or bool value False. (not our case)

All other values are considered true — so objects of many types are always true.

The difference is that NDjango has no check for 0 in case of value-types and this functionality should be placed outside of if.Node, because it should be used in some filters (yesno,default) and probably somewhere else, where we need to evaluate expression for true/false in "python" style