After an important rewrite, I have added and improved many features.
All objects treated as references
This basically means that an object is a space in memory, and your variable simply points to it. Why is this good? Well, it means you get
shallow copying, so, for example, if you pass an object as a parameter, it doesn't take a million cycles to copy all the data. The parameter simply points to the same place in memory.
Here's an example:
// Create a function that will take an object as a parameter
function myFunction( obj )
{
// Change the data of the original object passed to the function
obj.someField = 12;
}
// Starts here
function main()
{
// Create an object, and pass it to the function
var myobj = new Object();
myFunction( myObj );
// And, hey presto, this prints '12'
print( myobj.someField );
}
The good thing about this, from a game programming perspective, is, for example, say you want to update a player's state. Instead of having an array of players, and passing the index of the player you wish to update to the function, you can just pass the actual object player. This is faster than the method that DBPro users are used to.
Only issue left with this is that you have to manually delete memory, as such:
var obj = new Object();
// Object isn't deleted until this is called. Watch out for memory leaks
delete obj;
The useful thing about not scoping the objects however is that they can be passed out of functions. Take this for example:
function myFunction()
{
var vec = new Vector( 1, 2, 3 );
return vec;
}
// Starts here
function main()
{
print( myFunction().x );
}
Of course, that object isn't being deleted, so you'd actually have to assign it to a variable and then delete that.
Function overloading
The signature of a function in Eco isn't just it's name, but the name plus how many parameters it takes. You can call different functions that have the same name, but with different amounts of parameters.
For instance:
function myFunction()
{
print( "Function that takes no parameters" );
}
function myFunction( param )
{
print( param );
}
// Starts here
function main()
{
myFunction();
myFunction( "hello!" );
}
The output would be:
Function that takes no parameters
Hello!
Constructors
In the previous version, constructors weren't supported for objects. Now they are necessary. Using the fact that you can have function overloading, objects can be created in many different ways.
Here's an example of a vector:
var vec1 = new Vector(); // Start with blank data
var vec2 = new Vector( 1, 2, 3 ); // Initialize fields
Constructors are written in a class in the same way any other method is written. No special syntax is required. Only thing that is required is that even if you don't need a constructor, it must still be present. For the vector example, you would just do this:
public function Vector()
{
}
Recursive trees using classes
The old model for classes for Eco was basically a parent-child relationship. Each class had a parent (itself), and a child for each field. If the field was of another UDT, it too would be a parent, and have children. Needless to say, this meant that if you wanted to be recursive, you run out of memory.
Because objects are now treated as references, this isn't an issue. A class is basically just an identifier with a list of fields, which could be integers, floats, strings, or, other objects, even if they're of the same type of the class itself. Of course, the only way it would become recursive is using the constructors, because variables are dynamically typed.
Here's an example:
// Person - assume they always have 2 kids, lol
class Person
{
public var child1, child2;
public var name;
// Construct person
public function Person( name )
{
// RECURSIVE, YAY
this.child1 = new Person( name + "'s first child" );
this.child2 = new Person( name + "'s second child" );
}
}
Access modifiers
Access modifiers allow you to restrict usage of class member variables and methods to outside users. Essentially this means that they can't fiddle around with things they shouldn't be.
This feature is almost complete, except that currently (since it's implementation just today), even if you use a member inside a class, it will still throw an error. I will get this fixed, but not now because I'm about to go out for a drink.
Anyway, it still gives it a snazzy java-ish look. Here's a simple vector class:
// Vector class
class Vector
{
public var x, y, z;
// Blank constructor must always exist
public function Vector()
{
}
// Initialize data
public function Vector( x, y, z )
{
this.x = x;
this.y = y;
this.z = z;
}
// Add vectors
public function add( vec )
{
this.x = this.x + vec.x;
this.y = this.y + vec.y;
this.z = this.z + vec.z;
}
// Get values
public function getX()
{
return this.x;
}
public function getY()
{
return this.y;
}
public function getZ()
{
return this.z;
}
}
Plenty more features still to come. This is the result of many years of research and practice, so it would be nice to see some comments
"everyone forgets a semi-colon sometimes." - Phaelax