Multiple Methods in a Single line

BlitzMax Forums/BlitzMax Beginners Area/Multiple Methods in a Single line

Chroma(Posted 2005) [#1]
I'm wondering if anyone has been able to do something like this:

MyVar.Add( YourVar.Add(10) )


Where Add is just a Type Method that does a var = var + num.

Can do that in Blitz3D but it seems there's a Null Pointer returned when you try embed a couple of method operations.


Beaker(Posted 2005) [#2]
This works fine:
SuperStrict

Type Y
	Method Add%(f%)
		Return f+10
	End Method
End Type

Type M
	Method Add%(f%)
		Return f+100
	End Method
End Type

Local yourvar:Y = New Y
Local Myvar:M = New M

DebugLog MyVar.Add( YourVar.Add(10) )


Probably not what you meant.


Perturbatio(Posted 2005) [#3]
Type t
	Field Value:Int = 10
	
	Method add:Int(val:Int)
		Value:+Val
		Return Value
	End Method
End Type

Type t2
	Field Value:Int = 100
	
	Method Add:Int(val:Int)
		Value:+Val
		Return Value
	End Method
End Type



Global a:t = New t
Global b:t2 = New t2

b.Add(a.Add(10))

Print b.value




It's what I thought he meant too :)


Chroma(Posted 2005) [#4]
I'm using the Add Method inside the same type like so:

this:Hello = New Hello
that:Hello = New Hello

this.Add( that.Add(10) )

Type Hello
     Field x:int
     Method Add(num:int)
          x :+ num
     End Method
End Type



Perturbatio(Posted 2005) [#5]
this:Hello = New Hello
that:Hello = New Hello

this.Add( that.Add(10) )

Type Hello
     Field x:Int
     Method Add:Int(num:Int)
          x :+ num
		Return x
     End Method
End Type

Print this.x

You need to return something :)


Chroma(Posted 2005) [#6]
Hmm...still having some trouble. Will post back in a bit.


Chroma(Posted 2005) [#7]
Ahhhhhh.....

So I make all my Methods return a new type that way when I do calculations nothing is actually changed unless you do a "this =".

ie.

this = that.Add(5)


Which would return that + 5 and not actually change that at all. Here's a question tho:


Chroma(Posted 2005) [#8]
When the 'this' type got returned a new type...what happened to the old type it was pointed to? Did it get automatically deleted or am I causing a memory leak?


Chroma(Posted 2005) [#9]
Nevermind....checked it with GCMemalloc() and it stayed around 45-50k so all is good. I'm very impressed with how the vector lib is turning out. Can do some pretty long embedded equations like this. :)


rdodson41(Posted 2005) [#10]
Why not... this.x = that.x + 5? Then you can do really long equations!
this.x = that.x + 5 * ( those.y - 3 ) - 5555555555 * ( -1 + these.z )