Is Static Function or Method possible in MONKEY?

Monkey Forums/Monkey Programming/Is Static Function or Method possible in MONKEY?

APC(Posted 2014) [#1]
Hello Monkey community!
Does MONKEY supports static function or method ?
I am converting some code from javascript and the original code is as follows:
static Vector sub(Vector v1, Vector v2) {
Vector v3 = new Vector(v1.x - v2.x, v1.y - v2.y);
return v3;
}

What I am trying to accomplish is the following:
Local force:Vector = New Vector.sub(location, m.location)
It creates the new vector and do the subtraction in one line, off course it generates an error when I try to compile.

I can accomplish it by doing two line of codes:
Local force:Vector = New Vector(0, 0) ' Creates the new Vector
force = force.sub(location, m.location)
' Subtract the vectors 1 and 2 and return the result in the new vector.


Nobuyuki(Posted 2014) [#2]
yes, statics are possible using the Function and Global statements. Here's what you're looking for:

Function Sub(A:Vector, B:Vector)
  Return New Vector(A.x - B.x, A.y - B.y)
End Function


You would then call this based on which namespace the function was scoped at. For example, if you declared that function within the Vector class itself, you would call Vector.Sub(). So in your example, the code would be:

Local force:Vector = Vector.Sub(location, m.location)



Jesse(Posted 2014) [#3]
or maybe this:
Class Vector
	Field x:Float,y:Float
	
	Method New(x:Float,y:Float)
		Self.x = x
		Self.y = y
	End
     
	Method New(v:Vector)
		Self.x = v.x
		Self.y = v.y
	End
 
	Method Sub:Vector(v:Vector)
		x -= v.x
		y -= v.y
		Return Self
	End 

End

v3 = New Vector(v1).Sub(v2)



Gerry Quinn(Posted 2014) [#4]
Jesse's example works, but since it uses methods you need to have an instance of the class to work with. The point of static functions is that they are just functions that do not require a method instance. They can be put inside the class, and then they will also require a class scope but still not a class instance.


ziggy(Posted 2014) [#5]
To sumarize,(maybe it is helpfu). This is a small list of typical "what's this and that" when you come from another language to Monkey:

Namespaces:
There are no "namespaces" in Monkey but the name of the file where a class or function is defined acts like a namespace and can be used to prefix the class or funcition (or global, conts, etc...) name to disambiguate:
As instance, you can call:
graphics.DrawImage(myImage, 100,100)

This calls the DrawImage function, defined into graphics.monkey (which is just a file inside mojo).

Static classes:
There are no static classes into Monkey. You can make any class static by defining only static members on it and making it final.

Static subroutines or Procedures into classes:
Static subroutines into Monkey classes are fined as "Function" and are usually accessed from outside the Class by sing the name of the class as a prefix.
MyClass
    Function ThisIsStatic:Int(parameter1, parameter2)
      ...
   End
End

Function Main()
   MyFunction.ThisIsStatic(100,200)
end


For obvious reasons, a static subroutine does not require any class instance, so it won't be able to access any non shared member directly.

Static atributes
Static atributes on a class are fined as Global or Const into the class, and can be refered by using the class name as a prefix. Constant atributes can't be modified (obviously).

Delegates
Monkey does not support or have any delegates implementation. If you need this functionality, you can get very close to them by using reflection, but you'll have to live with run-time check of type safeness, which is not ideal, but workable

Function pointers
There are no function pointers in Monkey. The way to deal with the need of function pointers is usually done by using Interfaces.

malloc, free and memory management
Monkey us fully Garbage Collected, so you do not need to handle this manually. It's a lot like Java or C# in this regard.

Not sure if anything else can be handy?


Jesse(Posted 2014) [#6]
@Gerry
That is correct but works just as well for what he is doing. Just an alternative and is basically the same thing. And since it needs a new instance to create v3...


APC(Posted 2014) [#7]
Guys thank you I will try it.


APC(Posted 2014) [#8]
Nobuyuki

I tried your suggestion, first I had to add a return type to the function other wise Monkey would complain it is an Illegal type Expression
Class Vector
    Field x:Float
    Field y:Float
	
    Method New(x_:Float = 0, y_:Float = 0)
        x = x_
        y = y_
    End
    Function Sub:Vector(A:Vector, B:Vector)
        Return New Vector(A.x - B.x, A.y - B.y)
    End Function
End

Then when I call it from the main code
	Local force:PVector = PVector.sub(location, m.location)

it gives me the following error:
Method 'sub' cannot be accessed from here.

Jesse

Your suggestion works fine thank you!

ziggy

I have Jungle IDE Pro and I would like to know if there are samples that use static Function in the Jungle samples?


muddy_shoes(Posted 2014) [#9]
"PVector" isn't "Vector" and "sub" isn't "Sub".


APC(Posted 2014) [#10]
muddy_shoes

You are correct! my apologies to Nobuyuki, I fixed the typos and it worked fine!!!


NoOdle(Posted 2014) [#11]
I'm a bit late to the party but..

[bbcode]
Class Vector

Field x : Int
Field y : Int

Method New( x : Int = 0, y : Int = 0 )
Self.x = x
Self.y = y
End Method

Method Subtract : Vector( v : Vector )
Self.x = Self.x - v.x
Self.y = Self.y - v.y
Return Self
End Method

Method ToString : String()
Return Self.x + "," + Self.y
End Method

Function Sub : Vector( v1 : Vector, v2 : Vector )
Return New Vector( v1.x, v1.y ).Subtract( v2 )
End Function

End Class


Function Main : Int()

Local v1 : Vector = New Vector( 12, 2 )
Local v2 : Vector = New Vector( 1, 4 )

Local v : Vector = Vector.Sub( v1, v2 )
Print v.ToString()

Return 0
End Function
[/bbcode]


NoOdle(Posted 2014) [#12]
or alternatively you could modify it slightly to allow a result vector to be passed into the function. You can then store a temp vector somewhere to avoid allocation if it is not needed.

[bbcode]
Class Vector

Field x : Int
Field y : Int

Method New( x : Int = 0, y : Int = 0 )
Self.x = x
Self.y = y
End Method

Method Subtract : Vector( v : Vector )
Self.x = Self.x - v.x
Self.y = Self.y - v.y
Return Self
End Method

Method ToString : String()
Return Self.x + "," + Self.y
End Method

Method Copy : Vector( v : Vector )
Self.x = v.x
Self.y = v.y
Return Self
End Method

Function Sub : Void( v1 : Vector, v2 : Vector, result : Vector )
result.Copy( v1 ).Subtract( v2 )
End Function

End Class


Function Main : Int()

Local v1 : Vector = New Vector( 12, 2 )
Local v2 : Vector = New Vector( 1, 4 )

Local tmp : Vector = New Vector()
Vector.Sub( v1, v2, tmp )

Print tmp.ToString()

Return 0
End Function

[/bbcode]