New Method

BlitzMax Forums/BlitzMax Programming/New Method

Sean Doherty(Posted 2006) [#1]
I noticed the the following is allowed:

Method New(iTest:Int)
End Method


So if it compile, why can't I invoke it?

test = New Square(42)

Thanks


Ash_UK(Posted 2006) [#2]
Hmm, aren't you meant to use methods within types? Is that the only code you have, or have you got more to show us? It's hard to analyze anything with such little code.


Sean Doherty(Posted 2006) [#3]
I didn't bother putting the type. Asssume it is within type square. New is a special operator and I don't think you can invoke it using a parameter. At least not the normal way.


Ash_UK(Posted 2006) [#4]
Ahh, I see what you mean. My appologies Sean :o) Sorry, I don't have the answer to this one.


Cajun17(Posted 2006) [#5]
I didn't know that would compile. New isn't supposed to have arguments so I'm guessing the parser just doesn't check for that.


Sean Doherty(Posted 2006) [#6]
Funny thing is that if you change new to a function, it will tell you it must be a method?


H&K(Posted 2006) [#7]
Funny thing is that if you change new to a function, it will tell you it must be a method?

Thats because the method "NEW" exists as a method of the base object, (This may not be correct, cos intuitivly you would think it would be a function)

What you are doing is creating a method that is called after the base "New"
SuperStrict

Type MyType
	
	Field MyField:Int
	
	Method New:MyType()
		MyField=10
		Print "Hello"
	End Method

EndType

Local Variable:MyType = New MyType
Print Variable.MyField
I am very supprised to hear that it excepts parameters, cos I dont think it should.

You do this, when you want to add each instance to a list or such, or you need a more dynamic creation. (Well realy I dont think you should ever do it, and think MyType.Create:MyType(Params) is better)


Defoc8(Posted 2006) [#8]
perhaps im mistaken - but i thought bmax methods
in user defined classes override the same methods in parent
classes? - surely this means that the base method is not
called..as this wouldnt be desired in most cases.
[new() may well be an exception to the rule..]

Its stated in the docs that the overriden
methods must have the same signature!..so the param list
must surely have to match up correctly?..hmmm, perhaps
theres an issue with methods that take no params..

anyway - new() is not supposed to accept params, bmax
does not support overloaded methods..perhaps this will
come in the future, but its probably been left out to
keep things simple.


Cajun17(Posted 2006) [#9]
All New methods in a chain of inherited types are called starting with Object and finishing with the New method that was originally called.


Defoc8(Posted 2006) [#10]
thats how i expected it would work..lol, sorry if i confused
anyone..jst wasnt sure how this stuff worked under bmax..
havent used it for a while..atleast not in an oopy way..

thanks for clearing that up cajun17 - i tried a simple test
in bmax to confirm it....wasnt sure if parents new() method
had to be called explicitly, which would have been a pain...
anyway, good know.


Grey Alien(Posted 2006) [#11]
so you don't have to do Super.New() weird, but OK.


Red Ocktober(Posted 2006) [#12]
so, is the NEW method is a defacto constructor then?

--Mike


Jim Teeuwen(Posted 2006) [#13]
Indeed it is.

As will the following be the standard Destructor.

Method Delete()
End Method