Public and static methods?

I wish to write a vector class from a javascript book in codea, but I have a limited understanding of public and static methods. I assume it is much like local and global variables?

I wonder how important the distinction is in terms of functioning of the code, if I were to accidently write a static method as a public one for example.

The js functions of the vector2d object are defined as follows in the book

function Vector2d(x,y)
{
   this.x = x
   this.y = y
}

// public methods
Vector2d.prototype = 
{
   length: function() { return math.sqrt(this.lengthSquared() }
   normalize: function() { etc...}
   etc...
}

// static methods
Vector2d.distance = function(vec1,vec2)
{
   return ( vec1.subtract(vec2) ).length()
}

In Codea I would probably write it something like this

Vector2d = class()
function Vector2d:init(x,y)
   self.x = x
   self.y = y
end

-- public? Or static?
function Vector2d:length()
   return math.sqrt( self:lengthSquared() )
end

However I am unsure if the above codea Vector2d:length() function is public or static, what that difference means exactly. And how I would write the other type of function in codea.

I have a javascript/actionscript background so I have a basic understanding of most of what goes on, but I’m trying to improve my understanding of OO programming, in general and in Codea.

Any light on the difference between public and static functions, when to use which and how to define them in codea would be very welcome and apreciated!

You’re much better off using the built in vec2 classes in Codea. They have length, normalise, dot, cross methods etc. They’ll be faster than anything you can write in Lua.

There’s no exact equivalent to public/ static.

If you make a function local, only code within that tab (ie that Lua file), that comes below the locally defined function, will be able to call it. They can be fiddly to use, because of the requirement that the code calling the locally-defined function has to come below the function. So if you want two local functions to be able to call each other, the only way is to nest one inside the other. So you’d use it for private, internal methods within a library that you don’t want the rest of your project to be able to access.

Or, if you using a function/ command extensively in a certain sequence of your code, you can setup a temporary local pointer/ alias. Eg a trigonometry intensive chunk of code might benefit from local sin = math.sin etc.

The public-facing methods that the rest of the project can call have to be global.

Thanks yojimbo!

When reading back my post, I realised there is already vec2 object in codea, but I didn’t realise it also has all the vector operations all ready to go. Thats quite useful! Although another reason I wanted to do this, was to learn more about vector math itself.

I suggest that instead of writing basic vector math, you do something more useful with it.

As an example, I built my own basic physics library and wrote some posts about it (link). This knowledge has been very useful in several projects, because it taught me how to use acceleration, velocity and directions.

Another example is learning about movement and rotation in 2D and 3D space, which is all about vector math. The dot product and cross product, which completely baffled me at first, are extremely useful tools of vector math.

So there is no shortage of things to do with vector math.

Haha, yes, I know. I do a lot of that stuff too kirl.nl, but not using the vector aproach, which can simplify some operations quite a bit. Thats why I planned on writing a vector class to use, until I found out codea already has a complete vector class. :slight_smile:

Great, I look forward to seeing what you can produce in Codea…