Howto:Start using vectors and hashes in Nasal: Difference between revisions

Jump to navigation Jump to search
m
m (no more stub)
Line 523: Line 523:


This will simply create a function named "new" which accepts an implicit list of arguments (in the implicit "args" vector). The function returns a hash whose parents field is set to the args vector, so this method could even be used for multiple inheritance. Note that the Position3D hash no longer has its own constructor, it's really just a simple hash in this case.  
This will simply create a function named "new" which accepts an implicit list of arguments (in the implicit "args" vector). The function returns a hash whose parents field is set to the args vector, so this method could even be used for multiple inheritance. Note that the Position3D hash no longer has its own constructor, it's really just a simple hash in this case.  
To make this look a little nicer, we can also rename the default argument vector (i.e. args) to something more informative, like "classes":
<syntaxhighlight lang="php">
var new = func(classes...) {
return {parents:classes};
}
var Position3D = {x:0.0, y:0.0, z:0.0};
var p = new( Position3D );
</syntaxhighlight>


Using this approach, you could even make the "new operator" call custom constructors automatically for each created object.
Using this approach, you could even make the "new operator" call custom constructors automatically for each created object.

Navigation menu