www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Initializing global arrays in modules via this()?

reply AEon <AEon_member pathlink.com> writes:
Initializing global arrays in modules (i.e. outside functions), someone
mentioned this() as a solution:

<code>
module aepar_global;

const int MLdataNo = 13;
int[] metaData;

// Initializing Arrays (before main() is called)
this()
{
metaData.length = MLdataNo;	
// or metaData.length = metaData.length + MLdataNo;
}
</code>

Alas this yields an error:

// aepar_global.d(115): constructor aepar_global.this constructors only are for
class definitions

And since 

int[MLdataNo] metaData;

does not work either, I am a bit at loss what to do?

AEon
Mar 22 2005
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
Its a simple mistake: you need to make the module constructor static.  Ie:

# static this() {
#   metaData.length = MLdataNo;
# }

The error message could be a little more helpful, I suppose.  Maybe by 
suggesting that what you seem to want is a 'static this()'.

-- Chris Sauls

AEon wrote:
 // Initializing Arrays (before main() is called)
 this()
 {
 metaData.length = MLdataNo;	
 // or metaData.length = metaData.length + MLdataNo;
 }
 </code>
 
 Alas this yields an error:
 
 // aepar_global.d(115): constructor aepar_global.this constructors only are for
 class definitions

Mar 22 2005
parent reply AEon <AEon_member pathlink.com> writes:
Chris Sauls says...

Its a simple mistake: you need to make the module constructor static.  Ie:

# static this() {
#   metaData.length = MLdataNo;
# }

The error message could be a little more helpful, I suppose.  Maybe by 
suggesting that what you seem to want is a 'static this()'.

Ah thanx... alas since it is not possible to search newsgroups, I was not able to find the initial post, that not doubt was correct. AEon
Mar 22 2005
parent J C Calvarese <jcc7 cox.net> writes:
In article <d1pqbb$aqt$1 digitaldaemon.com>, AEon says...

Ah thanx... alas since it is not possible to search newsgroups, ...

To a certain extent, you can search the newsgroups: http://www.digitalmars.com/advancedsearch.html See also: http://www.prowiki.org/wiki4d/wiki.cgi?NewsDmD#NewsreadervsWebInterface jcc7
Mar 22 2005