Fri Nov 16, 2018 2:59 pm
Login Register Lost Password? Contact Us


Is SHARED implicit in modules with only one export?

Comments and questions related to the Enterprise Control Language

Mon Aug 27, 2018 7:01 pm Change Time Zone

Came across this today and am trying to understand why. I'm assuming SHARED is implicitly defined in modules where there's only one export.

This works:
Code: Select all
SharedTest := MODULE
   test_val := '1';

   EXPORT share_export() := FUNCTION
      RETURN test_val;
   END;
   
END;

SharedTest.share_export();


This doesn't:
Code: Select all
SharedTest := MODULE
   test_val := '1';

   EXPORT share_export() := FUNCTION
      RETURN test_val;
   END;
   
   EXPORT share_export2() := FUNCTION
      RETURN test_val;
   END;

END;

SharedTest.share_export();


Of course if I add "SHARED" to test_val it will work in both examples.
So are all variables in a module considered SHARED if there's only one exported attribute in that module?
BGehalo
 
Posts: 13
Joined: Thu Sep 28, 2017 8:06 pm

Mon Aug 27, 2018 7:32 pm Change Time Zone

BGehalo,
I'm assuming SHARED is implicitly defined in modules where there's only one export.
No, that is an incorrect assumption.

So are all variables in a module considered SHARED if there's only one exported attribute in that module?
No.

First, ECL has NO "variables" -- they're definitions (also referred to as "attributes" by old-timers). The word "variable" implies that it can be reassigned different values at different logic points in your code, but in ECL a "definition" can only be defined once (within a given scope of visibility, and visibility scopes are specified by the keywords EXPORT and SHARED), and ECL does not have any assignment statement -- the operator := is not an assignment, but is read as "is defined as" to specify the expression the definition implements. So that this code:
Code: Select all
MyName := 42;
defines "MyName" as the value "42" and should be read as "MyName is defined as 42."

Your real question is: Why is TestVal only visible to the share_export() function and not also to the share_export2() function?

And the answer is: because it is neither SHARED nor EXPORTed, therefore it is local. The visibility scope of a local definition terminates at the end of the very next EXPORT or SHARED definition.

Therefore, your code could be like this:
Code: Select all
SharedTest := MODULE
   test_val := '1'; //local definition, terminates visibility at the "END;"

   EXPORT share_export() := FUNCTION
      RETURN test_val;  //always returns '1'
   END;  //the test_val above is now no longer visible, so its name may be re-used

   test_val := '42';  //local definition, terminates visibility at the "END;"
   
   EXPORT share_export2() := FUNCTION
      RETURN test_val;  //always returns '42'
   END; 

END;

SharedTest.share_export();  //returns '1'
SharedTest.share_export2(); //returns '42'

So it's really all about scoping and visibility, a form of less formal "encapsulation" when compared to the FUNCTION and MODULE structures that are specifically designed for encapsulation purposes.

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1390
Joined: Wed Oct 26, 2011 7:40 pm


Return to ECL

Who is online

Users browsing this forum: No registered users and 1 guest