Real Simple Question?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Oct 22 13:51:14 PDT 2016


On Saturday, October 22, 2016 20:35:27 WhatMeWorry via Digitalmars-d-learn 
wrote:
> This is probably so simple that there's no example anywhere.
>
> Basically, I've got a huge array definition (see below) which I
> reuse over and over again in different projects.
>
> GLfloat[] vertices =
> [
>      // Positions          // Texture Coords
>      -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
>       0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
>                   .....
>               (lots and lots of values)
>                   .....
>
> I'd like to place this one array into a separate file and just
> include it with a one line statement in many projects.  I'm
> thinking mixins and/or imports but then if I knew how to do this,
> I wouldn't be asking.
>
> Thanks in advance.

Just put it in a separate module and then import it. e.g.

file: mypackage/constants.d
==================
module mypackage.constants;

GLfloat[] vertices =
[
      // Positions          // Texture Coords
      -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
       0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
                   .....
               (lots and lots of values)
                   .....
];
==================

file: main.d
==================
import mypackage.constants;

void main()
{
    auto v = vertices;
}
==================

Probably the key thing to remember is that when you compile your program,
all of the modules that are part of your program rather than a separate
library need to be compiled into it. Simply importing them isn't enough -
though using either rdmd or dub make that easier.

This is the official documentation's page on modules:

http://dlang.org/spec/module.html

This is the chapter from Al's book that covers modules:

http://ddili.org/ders/d.en/modules.html

And you'd almost certainly benefit from simply reading Ali's book as a
whole:

http://ddili.org/ders/d.en/index.html

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list