<html><head><title>Module</title></head><body bgcolor="#FFFFFF" link="#207BC3" vlink="#003B83" alink="#003B83"><font face="Arial" size="2"><p align="center"><b><font size="5">Module</font></b></p><br><b>Syntax</b><blockquote><pre><font face="Courier New, Courier, mono"size="2"><b><font color="#207BC3">DeclareModule</font></b> <name>...<b><font color="#207BC3">EndDeclareModule</font></b><b><font color="#207BC3">Module</font></b> <name>...<b><font color="#207BC3">EndModule</font></b><b><font color="#207BC3">UseModule</font></b> <name><b><font color="#207BC3">UnuseModule</font></b> <name></font></pre></blockquote><b>Description</b><br><blockquote>Modules are an easy way to isolate code part from the main code, allowing code reuse and sharingwithout risk of name conflict. In some other programming languages, modules are known as 'namespaces'.A module must have a <b><font color="#207BC3">DeclareModule</font></b> section (which is the public interface) and an associated<b><font color="#207BC3">Module</font></b> section (which is the implementation).Only items declared in the <b><font color="#207BC3">DeclareModule</font></b> section will be accessible from outside the module. All thecode put in the <b><font color="#207BC3">Module</font></b> section will be kept private to this module. Items from main codelike procedures, variables etc. won't be accessible inside the module, even if they are <a href="global.html">global</a>. A module can be seenas a blackbox, an empty code sheet where item names can not conflict with the main code. It makes it easierto write specific code, as simple names can be reused within each module without risk of conflict.<br><br>Items allowed in the <b><font color="#207BC3">DeclareModule</font></b> section can be the following: procedure (only procedure declaration allowed), structure, macro, variable,constant, enumeration, array, list, map and label.<br><br>To access a module item from outside the module, the module name has to be specified followed by the '::' separator. When explicitly specifyingthe module name, the module item is available everywhere in the code source, even in another module.All items in the <b><font color="#207BC3">DeclareModule</font></b> section can be automatically imported into another module or in the main code using <b><font color="#207BC3">UseModule</font></b>.In this case, if a module name conflict, the module items won't be imported and a compiler error will be raised.<b><font color="#207BC3">UnuseModule</font></b> remove the module items. <b><font color="#207BC3">UseModule</font></b> is not mandatory to access a module item, but the module name needs to be specified.<br><br>To share information between modules, a common module can be created and then used in every modules which needs it. It's the common wayhave global data available for all modules.<br><br>Default items available in modules are all SpiderBasic commands, structure and constants. Therefore module items can not be named likeinternal SpiderBasic commands, structure or constants.<br><br>All code put inside <b><font color="#207BC3">DeclareModule</font></b> and <b><font color="#207BC3">Module</font></b> sections is executed like any other code when the program flow reaches the module.<br><br>When the statements <b><font color="#207BC3">Define</font></b>, <b><font color="#207BC3">EnableExplicit</font></b>, <b><font color="#207BC3">EnableASM</font></b> are used inside a module, they have no effect outside the respective module, and vice versa.<br><br>Note: modules are not mandatory in SpiderBasic but are recommended when building big projects. They help to build more maintainablecode, even if it is slightly more verbose than module-free code. Having a <b><font color="#207BC3">DeclareModule</font></b> section make the module pretty much self-documentedfor use when reusing and sharing it.</blockquote><p><b>Example</b></p><blockquote><pre><font face="Courier New, Courier, mono"size="2"><font color="#207BC3">; Every items in this sections will be available from outside</font><font color="#207BC3">;</font><b><font color="#207BC3">DeclareModule</font></b> Ferrari<font color="#924B72">#FerrariName$</font> = "458 Italia"<b><font color="#207BC3">Declare</font></b> <font color="#207BC3">CreateFerrari</font>()<b><font color="#207BC3">EndDeclareModule</font></b><font color="#207BC3">; Every items in this sections will be private. Every names can be used without conflict</font><font color="#207BC3">;</font><b><font color="#207BC3">Module</font></b> Ferrari<b><font color="#207BC3">Global</font></b> Initialized = <font color="#924B72">#False</font><b><font color="#207BC3">Procedure</font></b> <font color="#207BC3">Init</font>() <font color="#207BC3">; Private init procedure</font><b><font color="#207BC3">If</font></b> Initialized = <font color="#924B72">#False</font>Initialized = <font color="#924B72">#True</font><b><font color="#207BC3">Debug</font></b> "InitFerrari()"<b><font color="#207BC3">EndIf</font></b><b><font color="#207BC3">EndProcedure</font></b><b><font color="#207BC3">Procedure</font></b> <font color="#207BC3">CreateFerrari</font>()<font color="#207BC3"> Init</font>()<b><font color="#207BC3">Debug</font></b> "CreateFerrari()"<b><font color="#207BC3">EndProcedure</font></b><b><font color="#207BC3">EndModule</font></b><b><font color="#207BC3">Procedure</font></b> <font color="#207BC3">Init</font>() <font color="#207BC3">; Main init procedure, doesn't conflict with the Ferrari Init() procedure</font><b><font color="#207BC3">Debug</font></b> "Main init()"<b><font color="#207BC3">EndProcedure</font></b><font color="#207BC3"> Init</font>()Ferrari::<font color="#207BC3">CreateFerrari</font>()<b><font color="#207BC3">Debug</font></b> Ferrari::<font color="#924B72">#FerrariName$</font><b><font color="#207BC3">Debug</font></b> "------------------------------"<b><font color="#207BC3">UseModule</font></b> Ferrari <font color="#207BC3">; Now import all public item directly in the main program scope</font><font color="#207BC3"> CreateFerrari</font>()<b><font color="#207BC3">Debug</font></b> <font color="#924B72">#FerrariName$</font></font></pre></blockquote><p><b>Example:</b> With a common module</p><blockquote><pre><font face="Courier New, Courier, mono"size="2"><font color="#207BC3">; The common module, which will be used by others to share data</font><font color="#207BC3">;</font><b><font color="#207BC3">DeclareModule</font></b> Cars<b><font color="#207BC3">Global</font></b> NbCars = 0<b><font color="#207BC3">EndDeclareModule</font></b><b><font color="#207BC3">Module</font></b> Cars<b><font color="#207BC3">EndModule</font></b><font color="#207BC3">; First car module</font><font color="#207BC3">;</font><b><font color="#207BC3">DeclareModule</font></b> Ferrari<b><font color="#207BC3">EndDeclareModule</font></b><b><font color="#207BC3">Module</font></b> Ferrari<b><font color="#207BC3">UseModule</font></b> CarsNbCars+1<b><font color="#207BC3">EndModule</font></b><font color="#207BC3">; Second car module</font><font color="#207BC3">;</font><b><font color="#207BC3">DeclareModule</font></b> Porche<b><font color="#207BC3">EndDeclareModule</font></b><b><font color="#207BC3">Module</font></b> Porche<b><font color="#207BC3">UseModule</font></b> CarsNbCars+1<b><font color="#207BC3">EndModule</font></b><b><font color="#207BC3">Debug</font></b> Cars::NbCars</font></pre></body></html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。