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]
A WITH command, like VB.NET would be nice. So your code would look something like this:-
WITH self
INC .x //GLB supports inc doesn't it? Can't remember now...
IF .x >= 8
.x = 0
..etc..
ENDIF
ENDWITH
Cheers
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:
if self.x > self.y then inc self.x
Can be written like this:
if .x > .y then inc .x
I like the ".var" way. Would you all agree?
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
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
yes, I'd try to make the "self" optional.
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?:-
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
WITH is not being used AFAIK, "Self." will now optionally become "."
So writing "self.x" can be written as ".x" as well.
Cheers,
Ian
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.
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
.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.
Using . would be fine
Might be worth tightening up the need for self/. too - just to reduce any logic errors by programmers :)
I'm not updating that. It's too much work for me. Sorry.
Try this:
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
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.
like in C++ ->this.<-
Yes... :)
"this." would be very very nice, because it´s used in many big languages!
For e.g. C++, C# and Java.
Cheers
I can't use "this", since it's a protected keyword. self has the same amount of characters, though. ;)
I don't think this drives me insane but there could be an easy work around. Maybe the editor can detect when you are editing within a TYPE ENDTYPE, scans what Variables are defined and then as you are typing in those variables it automatically adds the word self to them?
I can make a macro, when you type ".." it writes "self." - would that be cool?
What about s. for the Macro?
Or:
Bla as SELF
Bla.dosomething ?
personally i like self. I think it does what it says on the tin :whistle: