DICTIONARY never NULL ?

Started by Huy H., May 07, 2019, 03:44:02 PM

Previous topic - Next topic

Huy H.

Here I go again with NULL...

I recently found a bug in our code where where are checking if a passed in parameter (happens to be a DICTIONARY) to a function is null.  Turns out the dictionary can never be null.  This is very odd -- is that the indented behavior? I guess this is similar behavior to ARRAY?

Code (genero) Select

MAIN

  CALL check_dictionary(NULL)

END MAIN

FUNCTION check_dictionary(dict)

  DEFINE dict DICTIONARY OF STRING

  DISPLAY "Passed in Dictionary"
  IF dict IS NULL THEN
    DISPLAY "dict IS NULL"
  ELSE
    DISPLAY "dict IS NOT NULL"
  END IF

  DISPLAY ""
  DISPLAY "LET dict = NULL"
  LET dict = NULL
  IF dict IS NULL THEN
    DISPLAY "dict IS NULL"
  ELSE
    DISPLAY "dict IS NOT NULL"
  END IF

  DISPLAY ""
  DISPLAY "INITIALIZE dict to NULL"
  INITIALIZE dict TO NULL
  IF dict IS NULL THEN
    DISPLAY "dict IS NULL"
  ELSE
    DISPLAY "dict IS NOT NULL"
  END IF

END FUNCTION


Output:
Passed in Dictionary
dict IS NOT NULL

LET dict = NULL
dict IS NOT NULL

INITIALIZE dict to NULL
dict IS NOT NULL

David H.

According to the documentation the DICTIONARY array is passed to/from functions by reference on the stack and not by value. Use the getLength() method to check if the dictionary is empty.


On the subject of DICTIONARY it would be nice to have the option to sort them into key order...

Reuben B.

I am surprised that "IF dict IS NULL" compiles.  I would expect similar to  "IF arr IS NULL" which generate a -4340 error, and that the correct test would be  "IF dict.getLength() = 0" just like dynamic array case "IF arr.getlength() = 0".

QuoteOn the subject of DICTIONARY it would be nice to have the option to sort them into key order...

Just sort the keys array.

Code (genero) Select
LET keys = dict.getkeys()
CALL keys.sort(NULL, FALSE)




Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero

Sebastien F.

Hello,

In upcoming FGL versions 3.10.18 and FGL 3.20.04 (this is not in 3.20 EAP3!), the compiler will raise the error -4340 when using a dictionary in expressions like IF dict IS NULL THEN:

Code (genero) Select
MAIN
    DEFINE dict DICTIONARY OF STRING
    IF dict IS NULL THEN
| The variable 'dict' is too complex a type to be used in an expression.
| See error number -4340.
        DISPLAY "dict IS NULL"
    ELSE
        DISPLAY "dict IS NOT NULL"
    END IF
END MAIN


Seb

David H.

Quote from: Reuben B. on May 09, 2019, 01:24:10 AM
Just sort the keys array.

Good point, I forgot about that one! Thanks.