GLBasic forum

Main forum => GLBasic - en => Topic started by: matchy on 2012-May-09

Title: Redeclaring ALIAS
Post by: matchy on 2012-May-09
Perhaps there a way to clear the ALIAS for now. Maybe I'm doing it wrong but ALIAS redeclarations don't pass outside a for integer loop.

Code (glbasic) Select

alias_test()
FUNCTION alias_test:
LOCAL arr[], als[], id
DIM arr[2]
FOR id = 0 TO 1
ALIAS ar AS arr[id] // 'ar' can be redeclared and compiled in FOR integer loop :)
DEBUG "For " + id + " passed.\n"
NEXT
DIM als[2]
ALIAS ar AS als[0]
DEBUG "Alias 0 passed.\n"
ALIAS ar AS als[1] // but can't be re-declared for compiling outside loop :(
DEBUG "Alias 1 passed.\n"
ENDFUNCTION
Title: Re: Redeclaring ALIAS
Post by: kanonet on 2012-May-09
If you define a variable inside a loop (or an if) it just exist inside this loop. So if you reach NEXT your alias gets deleted, thats why you can define it again without problem in next loop or after the loop. Its the same for LOCAL and ALIAS.
Title: Re: Redeclaring ALIAS
Post by: matchy on 2012-May-09
So there's no way to delete or clear it for reuse outside a loop then.  ;/
Title: Re: Redeclaring ALIAS
Post by: Kitty Hello on 2012-May-09
uhm... it's a variable like any other. The only exception is, that it can't be unassigned.

Code (glbasic) Select

ALIAS poo AS hotdog
FOR i=0 to 5
   poo = hotdog_factory[i] // point to any in the factory
NEXT
// poo now points to the hotdog_factory[5]
poo = hotdog // point to the original thing again.


Or did I misunderstand your question?
Title: Re: Redeclaring ALIAS
Post by: kanonet on 2012-May-09
Im very sure this is wrong.
Quotepoo = hotdog_factory
This doesnt move the pointer to hotdog_factory, it just copies the value.
Or are im wrong with this?

[edit] WRONG - see later!
Title: Re: Redeclaring ALIAS
Post by: matchy on 2012-May-09
I not trying to understand how it works,  :o I'm trying to find an alternative to repeated setting that are not in a for loop and parameters can be copy and pasted across same aliases such as a simplified version of this where ar0, ar1, ar2 should use the alias name ar:  :whistle:

Code (glbasic) Select

ALIAS ar0 AS als[0]
al0.a = 1 // i can't quickly copy and paste interchanging with other ar. named aliases of the same type this way.
al0.b = 2
al0.c = 3
al0.x = 4
al0.y = 5
al0.z = 6

ALIAS ar1 AS als[1]
al1.a = 1
al1.b = 2
al1.c = 3
al1.x = 4
al1.y = 5
al1.z = 6

ALIAS ar2 AS als[2]
al2.a = 1
al2.b = 2
al2.c = 3
al2.x = 4
al2.y = 5
al2.z = 6
Title: Re: Redeclaring ALIAS
Post by: Kitty Hello on 2012-May-09
Do you want this?
Code (glbasic) Select

ALIAS prt_a AS als[0]
ptr_a.a = 1
ptr_a.b = 2

prt_a AS als[1]
ptr_a.a = 1
ptr_a.b = 2

FOREACH p in als[]
   DEBUG "a"+p.a+" b:"+p.b+"\n"
NEXT


@kanonet: The ALIAS command gives you a POINTER!! (reference to be precise) to anything. So if you change the alias'ed value, whatever it points to will be changed. This is the same with the FOREACH variables.
And this is why it's so important _NOT_ to REDIM, DIM, DIMPUSH, DIMDEL an array while you are using an ALIAS to any of its childs. If the array internally will reallocate, your ALIAS pointer will screw up and you will be happy if the program only crashes. Mostly it will overwrite random memory.

Title: Re: Redeclaring ALIAS
Post by: kanonet on 2012-May-09
@kitty i know. Thats why i said 'poo = hotdog_factory' in your example does not create a new pointer, it just copys the value from hotdog_factory to the location where the pointer points at (In this case hotdog).
Or to make it more clear:
Code (glbasic) Select
LOCAL a, b
ALIAS c AS a
a = b

is not the same like
Code (glbasic) Select
local a, b
ALIAS c AS a
ALIAS c AS b

And of cause its good, that its not the same. But would be nice, if the 2nd one would be possible and not give an compiler error.
Title: Re: Redeclaring ALIAS
Post by: matchy on 2012-May-09
Yes, that's what I was hoping for. Again, it's really just about the editing. You know, I could just use individual dummy loops then as this works:  :P

Code (glbasic) Select

ALIAS ar AS als[0]
DEBUG "Alias 0 passed.\n"

FOR id = 1 TO 1
ALIAS ar AS als[id]
DEBUG "Alias 1 passed.\n"
NEXT

FOR id = 2 TO 2
ALIAS ar AS als[id]
DEBUG "Alias 2 passed.\n"
NEXT

Title: Re: Redeclaring ALIAS
Post by: Kitty Hello on 2012-May-10
I really don't understand your problem, matchy.

What's wrong with:
Code (glbasic) Select

FOR id = 1 TO 2
   ALIAS ar AS als[id]
   DEBUG "ALias "+id+" passes\n"
NEXT

Title: Re: Redeclaring ALIAS
Post by: matchy on 2012-May-10
In an absolute sense, an example command would be like SET ar = NOTHING, but it not a problem as much as I use it.  :zzz:

There are cases where I need to set values for a type array that are different for each index, thus not done in a loop. When I have many of these, like over ten I am aliasing ar1, ar2, etc... and it seems pointless. The second example shows probably what I should be doing.  :-[

Code (glbasic) Select

ALIAS ar1 as arr[0]
ar1.x = 1
ar1.y = 2
ar1.z = 3
... // type could have over ten ar.w ar.h ar.l etc...
ALIAS ar2 AS arr[9]
ar2.z = 4
ar2rgb = 0xffff
...




Code (glbasic) Select

FOR i = 0 to 9
ALIAS ar AS arr[i]
SELECT i
  CASE 0
   ar1.x = 1
   ar1.y = 2
   ar1.z = 3
   ...
  CASE 9
   ar2.z = 4
   ar2rgb = 0xffff
ENDSELECT
NEXT
Title: Redeclaring ALIAS
Post by: Kitty Hello on 2012-May-11
Ahhhh! I see!
You want to Re-alias. I never thought of this. I can make such a command.
Title: Re: Redeclaring ALIAS
Post by: matchy on 2012-May-11
Are we witnessing the design birth of new command?

:nw: REALIAS  :nw: