This (Solved)

General FreeBASIC programming questions.
Post Reply
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

This (Solved)

Post by deltarho[1859] »

I have never understood the keyword This. I use it to be on the safe side but decided it was time to use it only when necessary.

In the manual and Wiki we are given an example where This is not needed. What I would have preferred is an example where This is needed.
The only times when you need to qualify member names with
This is when the member name is hidden, for example, by a local variable or parameter.
What does 'the member is hidden ' mean? Local variable? Local to what?

I suppose that I could simply never use 'This' and wait for the compiler to have a go at me, or is that asking for trouble
Last edited by deltarho[1859] on Sep 17, 2018 21:54, edited 1 time in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: This

Post by fxm »

Very obvious example:

Code: Select all

Type UDT
  Dim As Integer I
  Declare Sub init1 (Byval I As Integer)
  Declare Sub init2 (Byval I As Integer)
End Type

Sub UDT.init1 (Byval I As Integer)
  I = I
End Sub

Sub UDT.init2 (Byval I As Integer)
  This.I = I
End Sub


Dim As UDT u
u.init1(1234)
Print u.I
u.init2(1234)
Print u.I

Sleep
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: This

Post by badidea »

For me, "This" is just a way to prevent a conflict between member names and function parameter names:

Code: Select all

type vector
	dim as single x, y
	declare constructor()
	declare constructor(x as single, y as single)
	declare sub set(x_ as single, y_ as single)
	declare sub print()
end type

constructor vector()
	'nothing
end constructor

'this works :-)
constructor vector(x as single, y as single)
	this.x = x
	this.y = y
end constructor

'this sucks, but this works as well :-) 
sub vector.set(x_ as single, y_ as single)
	x = x_
	y = y_
end sub

sub vector.print()
	..print str(x)& "," & str(y)
	..print str(this.x) & "," & str(this.y) 'the same
end sub

var v1 = vector(10.0, 13.0)
v1.print
dim as vector v2
v2.set(3.0, 4.0)
v2.print
sleep
Too late :-)

In python "This" is called "self", and it is required to use. You can recognize a python coder's keyboard by these 4 worn out keys :-)

It can also be used on the members functions, like this bugged code (segfault):

Code: Select all

sub vector.print()
	this.print()
end sub
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: This

Post by deltarho[1859] »

Thanks guys.

For me, good examples bring home a textural definition. There are too many examples in the Manual and Wiki that just do not pass muster. Maybe it just me. <smile>
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: This (Solved)

Post by fxm »

Me, I always use 'This' explicitly when accessing a member field from a member procedure, because the code is so much more clear and symmetrical (highlighting access to a member field):
- access a member field ('xxx') of an object ('u') from outside its type: 'u.xxx'
- access a member field ('xxx') of an object from inside its type: 'this.xxx'
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: This

Post by fxm »

deltarho[1859] wrote:For me, good examples bring home a textural definition. There are too many examples in the Manual and Wiki that just do not pass muster. Maybe it just me. <smile>
For me, the first objective of an example is to illustrate how to use the syntax of the described keyword, but not necessary to present all possible cases of shortcuts.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: This (Solved)

Post by badidea »

The problem with manuals is often that one wants to describe everything related (use, function) as accurately as possible, resulting in abstract stuff that is hard to understand. The example code at the "this" wiki page leaves one guessing why the keyword exists.

Just wondering now, why they did not chose the plain "." (so ".x = x" and ".y = x) like used in the "with" block. Expect for the incompatibility with the "with" block.
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: This (Solved)

Post by deltarho[1859] »

fxm wrote:For me, the first objective of an example is to illustrate how to use the syntax of the described keyword
The manual does that but it is left wanting. It left me wanting to know what does 'the member is hidden ' mean?

Your first post, fxm, is much better; it illustrates how to use the syntax and does not leave me wanting. That should be put into the manual and WiKi.-
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: This (Solved)

Post by caseih »

badidea wrote:Just wondering now, why they did not chose the plain "." (so ".x = x" and ".y = x) like used in the "with" block. Expect for the incompatibility with the "with" block.
That's a good question. In the world of C++, this is actually a pointer, and gets used to pass the current object to some other external function. In FB it's not a pointer, near as I can tell, so it's just a lexical thing.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: This (Solved)

Post by Tourist Trap »

caseih wrote: In the world of C++, this is actually a pointer, and gets used to pass the current object to some other external function. In FB it's not a pointer, near as I can tell, so it's just a lexical thing.
Hello,
In VB.NET and VBA , THIS is named ME, and is used in a way similar to the FB's way.
The other major advantage of THIS (besides the clarity and reusability of the code because insertion of code with THIS is safe by copy/paste for example) is that an IDE can launch an autocomplete (relative to properties and methods of the object) when the editor sees THIS, at least in VB 's world this is a major feature.

@caseih, THIS->... (explicitely with -> arrow) is a pointer for sure. But I don't know C++ son maybe you mean something totally different.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: This (Solved)

Post by Tourist Trap »

(duplicate)
Last edited by Tourist Trap on Sep 20, 2018 6:58, edited 1 time in total.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: This (Solved)

Post by caseih »

Tourist Trap wrote:@caseih, THIS->... (explicitely with -> arrow) is a pointer for sure. But I don't know C++ son maybe you mean something totally different.
Doesn't look like it if I take fxm's example to be correct. Looks like a reference to me, not a pointer, which is really just a lexigraphical syntax thing (syntactical sugar). It may be a pointer in the generated C function, and indeed there is a hidden pointer passed to each member function of a class/type. But that's not the way FB presents it to the programmer.

A good IDE that can parse code doesn't need the "this" to determine a symbol is a member of a type; the scoping rules are clear and the IDE has already parsed all the symbols.
Post Reply