Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: DICTIONARY never NULL ?  (Read 9876 times)
Huy H.
Posts: 45


« 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
David H.
Posts: 158


« Reply #1 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...
Reuben B.
Four Js
Posts: 1119


« Reply #2 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)




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


« Reply #3 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
David H.
Posts: 158


« Reply #4 on: May 09, 2019, 02:03:19 pm »

Just sort the keys array.

Good point, I forgot about that one! Thanks.



Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines