Four Js Development Tools Forum

Discussions by product => Genero BDL => Topic started by: Huy Ho on May 07, 2019, 03:44:02 PM

Title: DICTIONARY never NULL ?
Post by: Huy Ho 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 (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
Title: Re: DICTIONARY never NULL ?
Post by: David Heydon 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 Barclay 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".

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)




Title: Re: DICTIONARY never NULL ?
Post by: Sebastien FLAESCH 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 (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
Title: Re: DICTIONARY never NULL ?
Post by: David Heydon on May 09, 2019, 02:03:19 PM
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.