SELF is driving me nuts!

Previous topic - Next topic

bigsofty

Hi,

While using the new TYPE system, I am finding the requirement of variables defined within the type having to have the "self." requirement is becoming a real chore and making the code look a mess to-boot.

Simple things like, "a=b+c", have to have "self.a=self.b+self.c"... my code is a mass of "self's". Honestly nearly every line in some unfortunate TYPEs have some "self"s on them, making the actual variables hard to read...

What I request is that if the variable is defined within a type and outside a sub or func, then its scope is, by default, restricted to the type... in other words, "Self." by default. There would be no need to type Self. all the time, making it optional.

Many thanks,


Ian



[attachment deleted by admin]
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Crivens

A WITH command, like VB.NET would be nice. So your code would look something like this:-
Code (glbasic) Select

WITH self
  INC .x //GLB supports inc doesn't it? Can't remember now...
  IF .x >= 8
    .x = 0
..etc..
  ENDIF
ENDWITH


Cheers
Current fave quote: Cause you like musicians and I like people with boobs.

hardyx

Another alternative to the self can be putting only a point "." before the fields, like in VB. This may be used in the type functions/subs only. No need to define a WITH command. The precompiler can replace each ".var" for "self.var".

This code:
Code (glbasic) Select
if self.x > self.y then inc self.x

Can be written like this:
Code (glbasic) Select
if .x > .y then inc .x


Kitty Hello

I like the ".var" way. Would you all agree?

bigsofty

That would be an improvement Gernot, thank you. Hopefully the coloured coding will be the same as the var coloured coding too, as your variable being split into two colours was off-putting too.

Ian
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

WPShadow

is it possible to make both versions? I mean:

- self.var
- .var

For me is self.var the better solution --> easier to debug for me
AMD X2 4600, 2 GB Ram, ATI X1950 XTX, XP PRO SP2: GLB Premium 10.beta_dingsi, <(´.´<) Kirby Dance (>`.`)>
http://lostrevenant.blogspot.com
alea iacta est

Kitty Hello

yes, I'd try to make the "self" optional.

Crivens

So if I had two arrays using the same type then how do you reference them? I mean lets say we have a simple type containing X, Y, and S (for sprite ID), then I apply this to my enemy and my hero, then how would it know in the same routine that .X, .Y, .S etc  is for the variable enemy and not hero without a WITH header?

Hmm, even with a WITH then how to handle more than one? Perhaps like this?:-
Code (glbasic) Select
WITH hero,enemy,friend
  .x=10 //Hero X coord set
  ..x=50 //Enemy X coord set
  ...x=100 //Friend X coord set
ENDWITH


Or maybe I am over complicating it?

Cheers
Current fave quote: Cause you like musicians and I like people with boobs.

bigsofty

WITH is not being used AFAIK, "Self." will now optionally become "."

So writing "self.x" can be written as ".x" as well.

Cheers,


Ian
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Kitty Hello

I was this --><-- close to "throw the towel" (=give up for good). when I saw that self.self.self.x would be ...x now. But then I found out that self.self is stupid and ...x makes no sense.

Phew! You really shocked me for a second.

Crivens

Hmm, sorry I haven't really used GLB for very long at all, and haven't even bought it yet (or have access to it at the moment). Main thing is I'm browsing the forum to see whats what before purchasing it really.

So is this "self" a way of attaching a trigger routine to a type or something? In which case obviously a .x notation is much better than self.x. But what of using variables setup against types in standard routines? For example if I have an array of enemies which use a type that holds their X and Y coords amongst other things, and another array for wall objects etc all using the same type, then code like IF enemy[enNum].x>wall[waNum].x THEN ...etc would be nice to become IF .x>wall[waNum].x THEN if it was wrapped by a WITH enemy[enNum] and ENDWITH or even better if can do IF .x>..x if wrapped by WITH enemy[enNum],wall[waNum].x if you see what I mean. Might just be a little too hard to read though...

Cheers
Current fave quote: Cause you like musicians and I like people with boobs.

hardyx

.var is good for me, thanks Gernot for trying.

@Crivens: Self represent the type (*this in c++ or Me in VB). I think your method is original, but very complex, and a hell for the compiler's programmer. The code may became difficult to maintain. GLBasic must be "BASIC", easy to read and intuitive language.

MrTAToad

Using . would be fine

Might be worth tightening up the need for self/. too - just to reduce any logic errors by programmers :)

Kitty Hello

I'm not updating that. It's too much work for me. Sorry.

Try this:
Code (glbasic) Select


TYPE TT

foo

FUNCTION bar:
ALIAS pFoo AS self.foo
pFoo = 15
ENDFUNCTION
ENDTYPE

LOCAL t AS TT
t.bar()
PRINT t.foo, 0,0
SHOWSCREEN
MOUSEWAIT


bigsofty

Argh! Does this mean I am stuck with a mass of SELFs or a pointer has to be created for every type var used within a type, defined seprately for each function?

Bummer.
 
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)