Page 1 of 1
"With" Keyword for Modules
Posted: Wed May 28, 2014 10:21 am
by c4s
With already works with structures:
Code: Select all
Structure Person
Name$
Age.l
Size.l
EndStructure
Friend.Person
With Friend
\Name$ = "Yann"
\Age = 30
\Size = 196
Debug \Size+\Size
EndWith
Why not make it work for modules with a very similar syntax?
Code: Select all
DeclareModule MyModule
Declare DoThis(a, b, c, d)
Declare DoThat(e, f, g, h)
Declare DoSomethingElse(i)
EndDeclareModule
With MyModule
::DoThis(1, 2, 3, 4)
::DoThat(5, 6, 7, 8)
::DoSomethingElse(9)
Debug "Return: " + ::DoThis(0, 0, 0, 0)
EndWith
Note that I'm aware of "UseModule:UnuseModule" but I think it's rather different as it implies naming conflicts and doesn't force the cleaner, straight forward, block-like structure of
With. I'm so used to "With:EndWith" that it just felt right to have it for modules too - but to my surprise it's not supported. However, this feature request isn't very well thought-out, so there might be some obvious downsides that make it unnecessary.
Re: "With" Keyword for Modules
Posted: Tue Mar 03, 2015 7:59 pm
by uwekel
Good idea

+1
Re: "With" Keyword for Modules
Posted: Wed Mar 04, 2015 12:04 am
by davido
Interesting idea, so +1
Re: "With" Keyword for Modules
Posted: Wed Mar 04, 2015 12:31 am
by Shield
Well that's what UseModule / UnuseModule are for

Re: "With" Keyword for Modules
Posted: Wed Mar 04, 2015 8:22 am
by uwekel
No, UseModule() imports everything to the current namespace, what can cause naming conflicts.
Re: "With" Keyword for Modules
Posted: Thu Sep 24, 2015 2:17 pm
by Blue
Very good idea +1
@Shield :
As
uwekel pointed out, UseModule / UnuseModule imports everything from the module.
Think of this suggestion as just a more compact, and clearer, way of writing
Code: Select all
a = ModuleAA::num1
b = ModuleAA::num2
..
..
With ModuleAA
a = num1
b = num2
EndWith
And, BTW, once the compiler knows which module num1 and num2 refer to, we could do
without the :: module marker.

Re: "With" Keyword for Modules
Posted: Thu Sep 24, 2015 4:10 pm
by Tenaja
Blue wrote:Very good idea +1
@Shield :
As
uwekel pointed out, UseModule / UnuseModule imports everything from the module.
Think of this suggestion as just a more compact, and clearer, way of writing
Code: Select all
a = ModuleAA::num1
b = ModuleAA::num2
..
..
With ModuleAA
a = num1
b = num2
EndWith
And, BTW, once the compiler knows which module num1 and num2 refer to, we could do
without the :: module marker.

There is a risk involved with eliminating the :: syntax, because using it guarantees you are referencing a module. Otherwise, you may have a global var named num1, in addition to the module var, and then you are prevented from referencing both within the with. This would be especially tricky with larger modules, because you could also have a b in it, so the a=num1 would work but the b=num2 would fail, and debugging could be confusing.
I would guess that is likely the reason the \ is kept on structures. In addition, keeping the :: would be more consistent with PB syntax.
Re: "With" Keyword for Modules
Posted: Thu Sep 24, 2015 4:46 pm
by Blue
Hi Tenaja.
Tenaja wrote:[...]There is a risk involved with eliminating the :: syntax, because using it guarantees you are referencing a module.
I guess that would entirely depend on
how the feature is implemented, wouldn't it ?
The risk you mention is very real indeed,
if the user is free to reference *any* variable
on the right hand-side of expressions within the With / EndWith construct.
If the compiler strictly enforces a rule such that
Code: Select all
With ModuleAA
a = num1
b = num2
EndWith
can ONLY stand for
Code: Select all
a = ModuleAA::num1
b = ModuleAA::num2
there would be no confusion possible.
Referencing an element *not* within the module would generate an error.
In my view, a strict implementation would enforce greater clarity and serve a more useful purpose overall.
But then, those who see
EnableExplicit as a limitation to their freedom of expression will disagree with me.

Re: "With" Keyword for Modules
Posted: Thu Sep 24, 2015 11:50 pm
by Tenaja
Sure, that would work, but then it would not be consistent with the With/Endwith use of Structures, which can be referenced on either side. I do not see that type of inconsistency being implemented, since Fred works hard to not only maintain consistency, but he's even updated long-standing commands to improve it.
Re: "With" Keyword for Modules
Posted: Fri Sep 25, 2015 12:06 am
by Blue
Tenaja wrote:[...]but then it would not be consistent with the With/Endwith use of Structures...
Agreed. I did not think of that. And it *is* an important point.
Plus i also realize now that the kind of strict approach i had in mind would limit much too drastically the usefulness of that kind of short-hand access to Modules.
And once more we see demonstrated how enlightening discussions can be.
Thank you Tenaja.
Re: "With" Keyword for Modules
Posted: Fri Sep 25, 2015 8:56 am
by Bisonte
WithModule / EndWithModule are very useful.
And I think the best way to call, is like c4s describe it.
Code: Select all
WithModule MyModule
Var = ::MyProc1(Value)
ForEach ::MyModuleList()
Debug ::MyModuleList()\Value
Next
Title.s = "Hello" + ::#MyTextConstant
EndWithModule