Project

General

Profile

Support #890

ImportByRef and ImportByValue behave in an unexpected manner (i.e. fail when I think they should succeed)

Added by John Abbott almost 8 years ago. Updated about 4 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Manual/documentation
Target version:
Start date:
15 Jun 2016
Due date:
% Done:

100%

Estimated time:
1.50 h
Spent time:

Description

While cleaning, I found some old CoCoA-5 "bugs". In this case there are two simple examples where ImportByRef gives an error which I did not expect; also the hint for resolving the problem is unhelpful (i.e. it says to use import!)

In the following excerpt reading the definition of gun produces an error

Define fun(x)
  PrintLn RingOf;
  Return RingOf(x);
EndDefine; -- fun

Define gun(x)
  ImportByRef fun;
  Return fun(x);
EndDefine; -- gun

The on-line documentation suggests that ImportByRef and ImportByValue work only inside anonymous functions; so I tried the following:

Define fun(x)
  PrintLn RingOf;
  Return RingOf(x);
EndDefine; -- fun

Define hun(x)
  fun2 := func() ImportByRef fun; return fun; endfunc;
  Return fun2(x);
EndDefine; -- hun

CoCoA-5 still gives an error when reading the definition of hun.

Note that replacing ImportByRef with TopLevel works in both cases.
Is this what we want?


Related issues

Related to CoCoA-5 - Bug #726: TopLevel cannot "import" a package variableClosed2015-06-06

History

#1 Updated by John Abbott almost 8 years ago

While TopLevel and ImportByRef are very similar there is a difference: namely, that TopLevel will search only in the top level even if there is a local variable with the same name; in contrast the meaning of ImportByRef could change if some local variables (with the same name) are removed or newly created.

Since the meaning of ImportByRef and ImportByValue can vary depending on a non-local change, it is probably best to discourage use of these commands (for non-experts anyway).

#2 Updated by John Abbott almost 8 years ago

  • Status changed from New to In Progress
  • % Done changed from 0 to 10

I am guessing that ImportByRef and ImportByValue may be used only for importing local variables in an outer lexical scope (and not for names at top level) -- if this is true, then I think it is probably the right design/semantics. Unfortunately the documentation is too vague; it must be improved! I suggest writing the documentation in such a way that it makes TopLevel very visible, and "discourages" use of ImportByRef and ImportByValue.

Another unfortunate aspect is the wording of the error message:

--> ERROR: Cannot find fun
--> WHERE: at line 12 (column 30) of bug1.cocoa5
-->   fun2 := func() ImportByRef fun; return fun; endfunc;
-->                              ^^^
--> ERROR: Cannot find a variable named "fun" in scope, but there is one in an outside scope.  You're probably missing an import statement
--> WHERE: at line 12 (column 42) of bug1.cocoa5
--> ...  fun2 := func() ImportByRef fun; return fun; endfunc;
-->                                             ^^^

It seems that the parser/interpreter did find the name (at top level), but then it should recommend using TopLevel and not an import statement.

#3 Updated by John Abbott almost 8 years ago

  • Related to Bug #726: TopLevel cannot "import" a package variable added

#4 Updated by John Abbott about 4 years ago

  • Target version changed from CoCoA-5.?.? to CoCoA-5.3.0

The doc for ImportByRef and ImportByValue starts with a big warning. Is this still necessary?

Check this issue; maybe we can fix the doc and close it?

#5 Updated by John Abbott about 4 years ago

  • Status changed from In Progress to Feedback
  • Assignee set to John Abbott
  • % Done changed from 10 to 90
  • Estimated time set to 1.23 h

I have improved the documentation (which is where I believe the problem lay).
The impl seems to be reasonable; but it is hard to give a good, concise explanation in the doc -- I hope the current version is OK.

#6 Updated by John Abbott about 4 years ago

  • Status changed from Feedback to Closed
  • % Done changed from 90 to 100
  • Estimated time changed from 1.23 h to 1.50 h

Also available in: Atom PDF