Four Js Development Tools Forum

Discussions by product => Genero BDL => Topic started by: Huy H. on May 07, 2019, 03:44:02 pm



Title: DICTIONARY never NULL ?
Post by: Huy H. on May 07, 2019, 03:44:02 pm
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
  1. MAIN
  2.  
  3.  CALL check_dictionary(NULL)
  4.  
  5. END MAIN
  6.  
  7. FUNCTION check_dictionary(dict)
  8.  
  9.  DEFINE dict DICTIONARY OF STRING
  10.  
  11.  DISPLAY "Passed in Dictionary"
  12.  IF dict IS NULL THEN
  13.    DISPLAY "dict IS NULL"
  14.  ELSE
  15.    DISPLAY "dict IS NOT NULL"
  16.  END IF
  17.  
  18.  DISPLAY ""
  19.  DISPLAY "LET dict = NULL"
  20.  LET dict = NULL
  21.  IF dict IS NULL THEN
  22.    DISPLAY "dict IS NULL"
  23.  ELSE
  24.    DISPLAY "dict IS NOT NULL"
  25.  END IF
  26.  
  27.  DISPLAY ""
  28.  DISPLAY "INITIALIZE dict to NULL"
  29.  INITIALIZE dict TO NULL
  30.  IF dict IS NULL THEN
  31.    DISPLAY "dict IS NULL"
  32.  ELSE
  33.    DISPLAY "dict IS NOT NULL"
  34.  END IF
  35.  
  36. END FUNCTION
  37.  

Output:
Passed in Dictionary
dict IS NOT NULL

LET dict = NULL
dict IS NOT NULL

INITIALIZE dict to NULL
dict IS NOT NULL


Title: Re: DICTIONARY never NULL ?
Post by: David H. on May 08, 2019, 10:11:06 am
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...


Title: Re: DICTIONARY never NULL ?
Post by: Reuben B. on May 09, 2019, 01:24:10 am
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".

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

Just sort the keys array.

Code
  1. LET keys = dict.getkeys()
  2. CALL keys.sort(NULL, FALSE)





Title: Re: DICTIONARY never NULL ?
Post by: Sebastien F. on May 09, 2019, 12:46:25 pm
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
  1. MAIN
  2.    DEFINE dict DICTIONARY OF STRING
  3.    IF dict IS NULL THEN
  4. | The variable 'dict' is too complex a type to be used in an expression.
  5. | See error number -4340.
  6.        DISPLAY "dict IS NULL"
  7.    ELSE
  8.        DISPLAY "dict IS NOT NULL"
  9.    END IF
  10. END MAIN

Seb


Title: Re: DICTIONARY never NULL ?
Post by: David H. on May 09, 2019, 02:03:19 pm
Just sort the keys array.

Good point, I forgot about that one! Thanks.