"new" ? not sure what it does

Monkey Forums/Monkey Beginners/"new" ? not sure what it does

dubbsta(Posted 2016) [#1]
can someone explain "new" to me. whats the difference between
method new ()

end
and
method something ()

end , or even a "new function()"

a few more questions, when should parameters be passed to a function or method if at all

and lastly out of curiosity why are some keywords such as push,pop, not highlighted. when looking at examples people make variable names and im not sure if they're keywords or made up, very confusing thanks in advance


Jesse(Posted 2016) [#2]
Hi dubbsta,
it seems you are new to programming. The best thing for you to do is go through some tutorials. I recommend you look at these tutorials:
http://www.monkey-x.com/Community/posts.php?topic=3318&page=first

it will help you get started.


Goodlookinguy(Posted 2016) [#3]
Edit: I was ninja'd. DX

It seems like you don't know about object-orientated programming (OOP for short) nor functions for that matter.

I'm going to recommend you go to invaderJim's tutorials to learn (I assume he explains it well, as I've never actually watched them but people always seem to recommend them for newcomers). Link here: http://www.monkey-x.com/Community/posts.php?topic=3318&page=1

To answer your questions directly, even though I'd strongly recommend checking out the videos, New is a constructor executed when an object is instantiated. Using Function in a class means that it is static (aka, not a part of the object instance). Methods are functionality or actions an instantiated object can do.


therevills(Posted 2016) [#4]
Welcome to Object-oriented (OO) programming :)

"New" is MonkeyX's Constructor (ctor), I tend to think of classes as blue prints and when you use the "New" method it creates an object of that blue print.

Class Player
   Field x:Float, y:Float
   Field score:Int

   Method New()
      x = 0
      y = 0
      score = 0  
   End

   Method New(posX:Float, posY:Float)
      x = posX
      y = posY
      score = 0
   End
End

So with the above example with a Player class, we have two constructors and can call them like this:
Local player1:Player = New Player()
Local player2:Player = New Player(100, 100)

Now player1 will be created with x as 0, y as 0 and with a score of 0 and player2 will be created with x and y as 100 and a score of 0.

You can of course change the parameters passed to New to fit your own needs.

Regarding the highlighting of words, Push, Pop are part of the module "Mojo" and are not keywords of Monkey.


dubbsta(Posted 2016) [#5]
thanks people, i seen jim's tutorials, maybe im slower, but hes not very detailed with his explanations. there good dont get me wrong but i feel like sometimes i still have to decipher what hes doing instead of getting a clear understanding of what hes talking about, maybe its me who knows. i guess im looking for more practical examples, anyway ill go over the videos again til i get it and will search oop development. if there are any books on oop that you guys recommend please refer them to me. and thanks therevillsed for the explanation.


dubbsta(Posted 2016) [#6]
so player2's (100,100) values are sent to the second method automatically, because they have parameters?


therevills(Posted 2016) [#7]
Player2's x and y are set in the second constructor as parameters.

Have a play with this example, change the ctor:



dubbsta(Posted 2016) [#8]
thanks, ok i get that, but i guess my first question was using your example, whats is the purpose for new. could you have done:

method pos(x,y) instead of method new(x,y)? what is the difference or benefit of the new keyword is it necessary hope its not a stupid question


therevills(Posted 2016) [#9]
Yes of course you could have a SetPosition method:

Class Player
	Field x:Float, y:Float
	
	Method SetPosition:Void(x:Float, y:Float)
		Self.x = x
		Self.y = y
	End	
End

But you still have to call New to create the object:
player1 = New Player()
player1.SetPosition(100,100)


All depends on how much you want to do in your constructors.


Gerry Quinn(Posted 2016) [#10]
Maybe I can explain it this way.

Suppose I have a class Point:

Class Point
	Field x:Int
	Field y:Int
End


...and I write:

Local pt:Point


What I've done is make a reference to a Point object called pt, but there isn't yet any such object. If I now make an assignment:

Local pt:Point
pt.x = 5


...I'll get a null object error or a memory access error when I run the program (not sure which offhand - it might depend on the target), because I'm writing to a non-existent object.

But if I write instead:

Local pt:Point = New Point()
pt.x = 5


...that works. The 'New' method (a.k.a. the constructor) assigned a little block of memory with room for two ints, and I set pt to point to it,

The constructor used was the default constructor, which just assigns a block of memory and leaves it to default values. Default value for an int is zero. So the point is now (x=5,y=0) as y was never changed.

You can make your own constructors for various purposes - for example:
Class Point
	Field x:Int
	Field y:Int
	
	Method New()
		x = -1
		y = -1
	End
	
	Method New( xVal:Int, yVal:Int )
		x = xVal
		y = yVal
	End
End


Now if you write:

Local pt:Point = New Point()


... the values will be (x=-1,y=-1), as you have replaced the default constructor with one of your own.

For a Point class you would rarely replace the default; it's just an example. But you would often make a constructor like the second one, so you could write, say:

Local pt:Point = New Point( 50, 100 )


...and so initialise points cleanly.

More useful than a default constructor would be a Set() function:

Class Point
	Field x:Int
	Field y:Int
	
	Method New( xVal:Int, yVal:Int )
		x = xVal
		y = yVal
	End
	
	Method Set:Void( xVal:Int, yVal:Int )
		x = xVal
		y = yVal
	End
End


You could use this to set new values to a point, whatever they were before.

Local pt:Point = New Point()
pt.Set( 50, 100 )


...gives the same result as previous. But you can set it again and again, if you want! If you wanted to use the Set function in the two-value constructor, you could do that too. Overkill for a Point class, but a pattern that you will see used for complicated classes.

Hope that helps answer some of your questions.


dubbsta(Posted 2016) [#11]
that was a great explantion Gerry, more my speed slowwww lol. im confused about one thing thought, you declared x and y instead of xval and yval, and why a negative 1
i did run the code, and ran fine, so if im right we are setting the value to xval and yval and it overrode the x -1 and y -1?


dubbsta(Posted 2016) [#12]
Gerry i ran your first example disabling the first " new method" with the x and y and it still ran good, so is the first method not needed since you also had x and y in the second method?


Gerry Quinn(Posted 2016) [#13]
The constructor that puts -1 in x and y was just an example of how you can create a default constructor of your own to replace the automatic default constructor (which would leave x and y at their default values of 0). If you don't declare a default constructor, the automatic one will get created anyway - that is why it still works in your second question.

As for xVal and yVal, I just like to give method parameters different names from field variables. Many people prefer to write instead:

Class Point
	Field x:Int
	Field y:Int
	
	Method New( x:Int, y:Int )
		Self.x = x
		Self.y = y
	End
End


They use Self.x so the method knows that the object field x is meant, rather than the method parameter that's also called x.


dubbsta(Posted 2016) [#14]
ok i thought it was just for example, just wanted to make sure, very clear now. thank you very good teacher. you read my mind on the self thing was wondering,thx