Assign this to something

BlitzMax Forums/BlitzMax Programming/Assign this to something

skn3(Posted 2008) [#1]
Does anyone else think it would be useful to be able to assign "this" to something.

EG
this = myobject
this.mymethod

Naturally this is just for cosmetic reasons in code, but functions within a type, this would be good for keeping code uniform.


boomboom(Posted 2008) [#2]
arn't you just using the word 'this' as a variable? Why can't you use it like that now?

Sound1:TSound = Loadsound...etc
Image1:TImage = Loadimage...


This:TSound = Sound1

*do stuff on 'this'

This:TImage = Image1

*Do stuff on 'this'*


When u reassign 'this' it won't cause anything to be GC'ed becuase it still exists in the original Sound1 and Image1 variable.


GfK(Posted 2008) [#3]
I think he's asking for something along the lines of VB-style 'With' blocks.

i.e.
myFirstType.mySecondType.myFirstMethod()
myFirstType.mySecondType.mySecondMethod()
myFirstType.mySecondType.myThirdMethod()
mrFirstType.mySecondType.x:+1

...would become...
this = myFirstType.mySecondType
this.myFirstMethod()
this.mySecondMethod()
this.myThirdMethod()
this.x:+1

The VB equivalent would be something like With myFirstType.mySecondType ; do stuff ; EndWith...all of which would be very useful, if you could have a single keyword which could be assigned to any type of object.


boomboom(Posted 2008) [#4]
'Type Setup
Type Type2
	Field Name:String
End Type

Type Type1
	Field T2:Type2 = New Type2
End Type

'Create New Type1
bob:Type1 = New Type1

'Set Name
bob.T2.Name = "Dave"

'Print Name the long way round to test its in there.
Print bob.T2.Name

'Now direct 'this' to be the second type inside bob, change name and print
this:Type2 = bob.t2
this.name = "Bill"
Print This.name

'Retest the long way round to make sure 'this' worked
Print bob.T2.Name

WaitKey()


Maybe I am still not understanding, but wouldn't the example above be it? Of course it would be nice not to have to assign the type of type to 'this'. but its not to much of an arse, plus it would probably break strict/superstrict.


ziggy(Posted 2008) [#5]
Yes. this is called late binding and in almost all languages it is implemented at runtime, so it has a deep impact on performance, unless a whole LinQ-like compile-time system is implemented. Additionally code like this is a bit harder to mantain.


grable(Posted 2008) [#6]

this is called late binding and in almost all languages it is implemented at runtime


Thats not necessarily true. a good optimizing compiler should already convert this:
first.second.third.method1()
first.second.third.method2()
first.second.third.method3()

into this
Local third = first.second.third
third.method1()
third.method2()
third.method3()


Or even better, by keeping third in a register during the block.


skn3(Posted 2008) [#7]
It would be entirely for assigning this to anything at runtime.
type mytype
    function blah:int(pobject:object)
        this = mytype(pobject)
        this.weee()
    end function

    method weee:int()
    end method
end type


I did not know you could assign "this", i just assumed you couldn't as blitz is very picky about reusing keywords in variables. Will try your example boomboom.

A use block would also be very useful, I use this feature ALOT in javascript!


boomboom(Posted 2008) [#8]
It should be fine as long as you redefine 'this' using local all the time.


skn3(Posted 2008) [#9]
Oh I think i see where teh confusion lies!

let me rephrase...
---

Does anyone else think it would be useful to be able to assign "self" to something.

EG
self = myobject
self.mymethod

Naturally this is just for cosmetic reasons in code, but functions within a type, this would be good for keeping code uniform.


tonyg(Posted 2008) [#10]
I think Self outside the confines of a method would be less understandable than a meaningful object variable name :
e.g.
self.print vs highest_score.print
If you're creating highest_score and then assigning it to self it would be even more bizarre imo.


boomboom(Posted 2008) [#11]
yea....it wouldn't really work outside of a method I don't think. The fact its inside a method gives it relevance, otherwise you will have to going through your code all the time to work out what 'self' is all the time.


skn3(Posted 2008) [#12]
True! I guess Im just to intwined with my day job of javascript! With that you can call functions and have a local context assigned per call.

myfunc.call(myobject,param1,param2)

function myfunc(param1,param2)
    self.dosomething(param1,param2)
end function



Arowx(Posted 2008) [#13]
I'm confused but if you write object or type based code and then can re-assign Self to another object ....


skn3(Posted 2008) [#14]
Type testtype
	Function static_function:Int(ninstance:Object,param1:Int)
		Self = testtype(ninstance)
		'--------------------------------
		
		Self.myvar = param1
	End Function
	
	Method nonstatic_function:Int(param1:Int)
		Self.myvar = param1
	End Method
End Type

Local mytype:testtype = New testtype

mytype.static_function(mytype,1234)
mytype.nonstatic_function(1234)


In this instance it is purely a cosmetic thing, but if you were introduce calling a function as if it were belonging to an object.

Function my_penknife_function:Int(param1:Int)
	Self.counter = param1
End Function

Type type1
	Field counter:Int
End Type

Type type2
	Field counter:Int
End Type

Local mytype1:type1 = New type1
Local mytype2:type2 = New type2

my_penknife_function(12345){mtype1}
my_penknife_function(12345){mytype2}
'function name     params      related object


Naturally the same could be done by passing the object as a parameter, but this is just a pondering thread is it not...