Allow redefinition of PureBasic functions within modules

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Allow redefinition of PureBasic functions within modules

Post by Mistrel »

Modules define their own scope. Therefore we should be able to use the same name as PureBasic library function.

Code: Select all

DeclareModule Memory
  Declare readData(hProcess, *address, *buffer, bufferSize)
EndDeclareModule
But we cannot:

Error:
Line 3: Invalid name: same as a command (from library 'File').
Despite the fact that modules do not inherit anything we define in the global scope, all of PureBasic's libraries are present. This allows some unexpected things such as calling library functions which were never explicitly defined:

Code: Select all

DeclareModule Memory
EndDeclareModule

Module Memory
EndModule

; Works fine
Memory::MessageRequester("","Test")
I'm trying to "do the right thing" by using modules but I always end up having some kind of issue with scope when I try to use them. For example, Memory_readData() is fine but Memory::readData() is not due to a collision with PureBasic's ReadData().

I proposed a suggestion which would also provide a solution for this some time ago, among other related ideas to improve modules:

viewtopic.php?f=3&t=71071
User avatar
Bisonte
Addict
Addict
Posts: 1232
Joined: Tue Oct 09, 2007 2:15 am

Re: Allow redefinition of PureBasic functions within modules

Post by Bisonte »

But how do you want to write a module if purebasic does not
know what you are doing? readData() is your function, but
what should happen if you need the function ReadData()
inside or outside the module... How do you want to tell the
compiler that the original function does not exist anymore ?

If I have functions that collide with the PB functions by name,
I will add a letter or an underscore to it, or even add the "ex"
at the end...

Interfaces or macros would also be a possibility...

Translated with http://www.DeepL.com/Translator
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
mestnyi
Addict
Addict
Posts: 1000
Joined: Mon Nov 25, 2013 6:41 am

Re: Allow redefinition of PureBasic functions within modules

Post by mestnyi »

Bisonte wrote:But how do you want to write a module if purebasic does not
know what you are doing? readData() is your function, but
what should happen if you need the function ReadData()
inside or outside the module... How do you want to tell the
compiler that the original function does not exist anymore ?

If I have functions that collide with the PB functions by name,
I will add a letter or an underscore to it, or even add the "ex"
at the end...

Interfaces or macros would also be a possibility...

Translated with http://www.DeepL.com/Translator

Code: Select all

::ReadData()
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Allow redefinition of PureBasic functions within modules

Post by Mistrel »

I ran into this again today when I tried to add a Log() function to a module. But this is a built-in function of the Math library.

I don't like having to come up with creative names like this. Please fix module scope.

Code: Select all

DeclareModule SomeModule
  Declare Log()
EndDeclareModule

Module SomeModule
EndModule
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Allow redefinition of PureBasic functions within modules

Post by kenmo »

+1

Somebody (I think Luis) did a good writeup on the conflicts of PB modules, which make them less useful than they truly could be.
It's partially why I ended up staying with regular IncludeFiles.
User avatar
NicTheQuick
Addict
Addict
Posts: 1226
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Allow redefinition of PureBasic functions within modules

Post by NicTheQuick »

You can do this with Macros like this:

Code: Select all

DeclareModule Memory_Definition
	Declare.i _AllocateMemory(size.i, flags.i=0)
EndDeclareModule
Module Memory_Definition
	Procedure.i _AllocateMemory(size.i, flags.i=0)
		If size <= 0
			Debug "[DEBUG] Size can not be zero or less."
			ProcedureReturn 0
		EndIf
		Debug "[DEBUG] Allocating " + size + " bytes."
		Protected *ptr = AllocateMemory(size, flags)
		If *ptr
			Debug "[DEBUG] Success: " + *ptr
		Else
			Debug "[DEBUG] Error"
		EndIf
		ProcedureReturn *ptr
	EndProcedure
EndModule

DeclareModule Memory
	Macro AllocateMemory(Size, Flags=0)
		Memory_Definition::_AllocateMemory(Size, Flags)
	EndMacro
EndDeclareModule
Module Memory
EndModule

Define *a, *b, *c

Debug "*a"
*a = AllocateMemory(100)

UseModule Memory

Debug "*b"
*b = AllocateMemory(100)
*c = AllocateMemory(0)

UnuseModule Memory

Debug "*c"
*c = AllocateMemory(100)

FreeMemory(*a)
FreeMemory(*b)
FreeMemory(*c)
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Allow redefinition of PureBasic functions within modules

Post by Mistrel »

That's an interesting workaround to add to my already impressive list of workarounds for things that I wish PureBasic would do natively without a workaround.

Seriously, though. Cool workaround. :)

I hope that when Fred is happy with all of the code transition to PureBasic 6 he will revisit adding improvements to the language. It feels like the core language has been stagnate for the last 10 years with only bug fixes. As much as I love PureBasic, I find myself rarely using it anymore due to how many workarounds I have to do to get the functionality that I want.
Last edited by Mistrel on Tue Sep 14, 2021 11:33 pm, edited 1 time in total.
User avatar
STARGÅTE
Addict
Addict
Posts: 2086
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Allow redefinition of PureBasic functions within modules

Post by STARGÅTE »

NicTheQuick wrote: Mon Sep 13, 2021 10:19 am You can do this with Macros like this:
Wow, the combination of using modules and macros for mapping is a very elegant solution. Thanks Nic.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Allow redefinition of PureBasic functions within modules

Post by Mistrel »

PureBasic macros have always been a highlight and I love them. It does have the unfortunate side-effect of breaking code completion in some cases while in others it can actually provide a workaround to *fix* code completion. Ahh, macros. :)

Here is an example where you might think a macro would work but it doesn't, for those who are curious. It is also related to the topic of module workarounds.

viewtopic.php?f=4&t=71024
Mistrel wrote: Tue Oct 01, 2019 3:31 am This is still a problem.

We can use macros for structures:

Code: Select all

Structure Triangle2i
EndStructure

Macro SHAPE
Triangle2i
EndMacro

Define triangle.SHAPE
But we can't for modules:

Code: Select all

DeclareModule Shape2i
  Interface ITriangle
  EndInterface
EndDeclareModule

Module Shape2i
EndModule

Macro SHAPE
Shape2i
EndMacro

Define triangle.SHAPE::ITriangle
This results in the following error:
Module not found: SHAPE.
User avatar
jacdelad
Addict
Addict
Posts: 1475
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Allow redefinition of PureBasic functions within modules

Post by jacdelad »

Module names are very specific, same with jumpmarks. You cannot redefine them on-the-fly.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Allow redefinition of PureBasic functions within modules

Post by Mistrel »

The purpose of the macro is not to redefine them but to provide an alias. For example, I can include a specific variant of a module and use a macro to reference it with a more generic name rather than tightly coupling my code to any particular implementation. This is illustrated in the code example I provided.
User avatar
jacdelad
Addict
Addict
Posts: 1475
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Allow redefinition of PureBasic functions within modules

Post by jacdelad »

The purpose is not solely to provide an alias, the original purpose is/was to provide a piece of code that is used multiple times and prevent it from spamming a code.
But you can excellently abuse it to provide aliases (and redefine internal functions).
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Allow redefinition of PureBasic functions within modules

Post by Mistrel »

I didn't say that the purpose of a macro is to provide an alias. I was explaining to you that the example I provided was to provide an alias and not to redefine a module name.

A macro is a very powerful tool and can be used for much, much more than just eliminating the repetition of code.
User avatar
NicTheQuick
Addict
Addict
Posts: 1226
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Allow redefinition of PureBasic functions within modules

Post by NicTheQuick »

STARGÅTE wrote: Tue Sep 14, 2021 11:33 pm
NicTheQuick wrote: Mon Sep 13, 2021 10:19 am You can do this with Macros like this:
Wow, the combination of using modules and macros for mapping is a very elegant solution. Thanks Nic.
I mostly like the fact that you can use UnuseModule to disable the mapping. :-D
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Allow redefinition of PureBasic functions within modules

Post by luis »

The idea of using macros to hide the name clash problem in the modules was mentioned some years ago.

2013
viewtopic.php?p=425058#p425058

The whole thread is about people on the two fronts of modules: they are broken how can't you see it/they are great just change your stuff when there is a conflict.

I wouldn't call that "elegant" by any stretch of imagination, but in an emergency it can at least solve a conflict by hiding it.
"Have you tried turning it off and on again ?"
A little PureBasic review
Post Reply