I was wondering what people's thoughts are on using ECMAScript style Module Import/Export System. This seems like a really good idea, and in general the straight forward to implement.
New Lexer Keywords: export, import, from, as.
Example Code (Script Side): (modified from Example squirrel code on Main web page...
Code File 1: Core/Entity.nut
Code:
// file: Core/Entity.nut
class Entity
{
constructor(etype,entityname)
{
name = entityname;
type = etype;
}
x = 0;
y = 0;
z = 0;
name = null;
type = null;
}
function Entity::MoveTo(newx,newy,newz)
{
x = newx;
y = newy;
z = newz;
}
// Place an entry EntityBase in export table, and have it reference the Entity class.
export Entity as EntityBase;
Code File 2: Core/Player.nut
Code:
// file: Core/Player.nut
// Import EntityBase from the export table, and reference it as Entity in the local file.
import EntityBase from 'Entity';
class Player extends EntityBase {
constructor(entityname)
{
base.constructor("Player",entityname)
}
function DoDomething()
{
::print("something");
}
}
// Straight Forward Player Export.
// Also note that the semicolon on the Export Statement Is Optional.
export Player
Code File 2: MyMathLibrary:
Code:
// Sample Exports:
export myPI = 3.14159
export const myConstantPI = 3.14159
export function myDegreetoRadeon(value)
{
return value / 3.14159;
}
export class myVectorClass {
constructor()
{
}
x = 0;
y = 0;
}
Code File 3: GameLogic.nut
Code:
// Rename Player Object MyPlayer for local namespace.
// Also, the semicolon at the end of the line for Imports is optional.
import { Player as MyPlayer} from 'Core/Player'
local newplayer = MyPlayer("da playar");
newplayer.MoveTo(100,200,300);
As a quick note, I know that I did not show Multiple Import/export, but in general the format would be...
Code:
// Multiple Export:
export {object1, object2, ... , objectN};
export {object1 as myobject1, object2 as myobject2, ..., objectN as myObjectN};
// multiple Import:
import {object1, object2, ... , objectN} from MyLibrary;
import {object1 as myobject1, object2 as myobject2, ..., objectN as myObjectN} from MyLibrary;
Binary Modules: To be determined, however I believe that a system similar to what SQRat does may be a good idea.