BASIC

Author Topic: Redeclaring ALIAS  (Read 2213 times)

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1542
    • View Profile
Redeclaring ALIAS
« 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
 

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Redeclaring ALIAS
« Reply #1 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.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1542
    • View Profile
Re: Redeclaring ALIAS
« Reply #2 on: 2012-May-09 »
So there's no way to delete or clear it for reuse outside a loop then.  ;/

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10708
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Redeclaring ALIAS
« Reply #3 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?

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Redeclaring ALIAS
« Reply #4 on: 2012-May-09 »
Im very sure this is wrong.
Quote
poo = 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!
« Last Edit: 2012-May-09 by Kitty Hello »
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1542
    • View Profile
Re: Redeclaring ALIAS
« Reply #5 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
 
« Last Edit: 2012-May-09 by matchy »

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10708
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Redeclaring ALIAS
« Reply #6 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.


Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Redeclaring ALIAS
« Reply #7 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.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1542
    • View Profile
Re: Redeclaring ALIAS
« Reply #8 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
 

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10708
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Redeclaring ALIAS
« Reply #9 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
 

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1542
    • View Profile
Re: Redeclaring ALIAS
« Reply #10 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
 

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10708
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Redeclaring ALIAS
« Reply #11 on: 2012-May-11 »
Ahhhh! I see!
You want to Re-alias. I never thought of this. I can make such a command.

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1542
    • View Profile
Re: Redeclaring ALIAS
« Reply #12 on: 2012-May-11 »
Are we witnessing the design birth of new command?

 :nw: REALIAS  :nw: