Duplicates Imports should be ignored instead of compiler err

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Rinzwind
Enthusiast
Enthusiast
Posts: 679
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Duplicates Imports should be ignored instead of compiler err

Post by Rinzwind »

#Test = 1
#Test = 1

Compiles fine/

Import "test.lib"
Test()
EndImport

Import "test.lib"
Test()
EndImport

is not fine.

When using lots of includes file and grouping related stuff together you can have imports of the same function multiple times. Would be nice if the compiler allows (ignores) this and only complains when the import is actually different from previous one (just like with constants).
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: Duplicates Imports should be ignored instead of compiler

Post by chi »

If you are already dealing with many include files, why not put all imports into a separate file and include them with XIncludeFile "Imports.pbi"? Doing otherwise is just bad practice.
Et cetera is my worst enemy
BarryG
Addict
Addict
Posts: 4130
Joined: Thu Apr 18, 2019 8:17 am

Re: Duplicates Imports should be ignored instead of compiler

Post by BarryG »

chi wrote:put all imports into a separate file and include them with XIncludeFile "Imports.pbi"
Nice trick!
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Duplicates Imports should be ignored instead of compiler

Post by Josh »

chi wrote:Doing otherwise is just bad practice.
No, it's not. If you try to write IncludeFiles, which bring everything they need themselves, then you have a problem. With many other things like Structure or Enumeration you can help yourself with Defined, with Import you do not have these possibilities.
sorry for my bad english
User avatar
Derren
Enthusiast
Enthusiast
Posts: 316
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: Duplicates Imports should be ignored instead of compiler

Post by Derren »

I guess this is not possible, either?

Code: Select all

Import "test.lib"
Test()
EndImport

Import "another.lib"
Test() ; same function name, should throw an error?
EndImport
If so, you can still use Defined?

Code: Select all

Import "test.lib"
Test()
EndImport

CompilerIf Not Defined(Test(), #PB_Procedure ) ; or  #PB_Function ?
Import "another.lib"
Test() ; same function name, should throw an error?
EndImport
CompilerEndIf

I'm guessing, since I have no experience with Import and nothing to test this with.


Or do it properly and use Modules?
Either really properly and use one Module for each Library, or just put the same Import in a bunch of Modules over and over again. The functions are then only accessible via the Module. Probably blows your exe size up.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Duplicates Imports should be ignored instead of compiler

Post by Josh »

Derren, of course you're right. With 'Defined' I always thought about 'Import' and not about the individual procedures. This works, but it doesn't look very good:

Code: Select all

  Import "User32.lib"
    CompilerIf Not Defined (MessageBoxA, #PB_Procedure)
      MessageBoxA (Window.i, Body$, Title$, Flags.i = 0)
    CompilerEndIf
    CompilerIf Not Defined (MsgBox, #PB_Procedure)
      MsgBox (Window.i, Body$, Title$, Flags.i) As "_MessageBoxA@16"
    CompilerEndIf
  EndImport

  Import "User32.lib"
    CompilerIf Not Defined (MessageBoxA, #PB_Procedure)
      MessageBoxA (Window.i, Body$, Title$, Flags.i = 0)
    CompilerEndIf
    CompilerIf Not Defined (MsgBox, #PB_Procedure)
      MsgBox (Window.i, Body$, Title$, Flags.i) As "_MessageBoxA@16"
    CompilerEndIf
  EndImport
  
  MessageBoxA(0, "Hello", "World 1")
  MsgBox     (0, "Hello", "World 2", 0)
sorry for my bad english
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Duplicates Imports should be ignored instead of compiler

Post by GPI »

you can add a check:

Code: Select all

Import "test.lib"
  Test()
EndImport

CompilerIf Not Defined(Test,#PB_Procedure)
  Import "test.lib"
    Test()
  EndImport
CompilerEndIf
Post Reply