"With" Keyword for Modules

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

"With" Keyword for Modules

Post 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.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: "With" Keyword for Modules

Post by uwekel »

Good idea :-) +1
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: "With" Keyword for Modules

Post by davido »

Interesting idea, so +1
DE AA EB
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: "With" Keyword for Modules

Post by Shield »

Well that's what UseModule / UnuseModule are for :?:
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: "With" Keyword for Modules

Post by uwekel »

No, UseModule() imports everything to the current namespace, what can cause naming conflicts.
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: "With" Keyword for Modules

Post 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. :wink:
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: "With" Keyword for Modules

Post 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. :wink:
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.
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: "With" Keyword for Modules

Post 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
:arrow: can ONLY stand for :idea:

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. :| :D
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: "With" Keyword for Modules

Post 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.
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: "With" Keyword for Modules

Post 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. 8)

Thank you Tenaja.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: "With" Keyword for Modules

Post 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
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Post Reply