Author Topic: parent.typename like self.typename?  (Read 1797 times)

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1540
    • View Profile
parent.typename like self.typename?
« on: 2015-Dec-09 »
There's self. but how can I reference nested type referencing?   :-[
Logically, should there be parent. also?   :zzz:

Nested TYPE Example:
Code: GLBasic [Select]
TYPE _item
    id
    selected_id
    color
    size
    FUNCTION set:
         self.id = basket.item_id                    // absolute
//      self.id = parent.parent.item_id       // would prefer relative          
    ENDFUNCTION
ENDTYPE

TYPE _group
    apple AS _item
    banana AS _item
ENDTYPE

TYPE _base
    item_id
    fruit AS _group
ENDTYPE

GLOBAL basket as _base
basket.fruit.apple.set()
 

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10697
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: parent.typename like self.typename?
« Reply #1 on: 2015-Dec-10 »
The child has no knowledge about the parent. (Unlike in real life) ;)

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1540
    • View Profile
Re: parent.typename like self.typename?
« Reply #2 on: 2015-Dec-10 »
Ah. i was hoping it to be like Javascript, which is not real nor life either.  8)

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2613
    • View Profile
Re: parent.typename like self.typename?
« Reply #3 on: 2015-Dec-10 »
You could do it with a little ref passing workaround...

change...
   
Code: GLBasic [Select]
FUNCTION set:

to...
   
Code: GLBasic [Select]
FUNCTION set: parentRef as _base

then change...
   
Code: GLBasic [Select]
self.id = basket.item_id

   to...
Code: GLBasic [Select]
self.item_id = parentRef.item_id

Your call would need a change too...
   
Code: GLBasic [Select]
basket.fruit.apple.set(basket)

for another basket...
   
Code: GLBasic [Select]
basket2.fruit.apple.set(basket2)

Untested.
« Last Edit: 2015-Dec-10 by bigsofty »
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)

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1540
    • View Profile
Re: parent.typename like self.typename?
« Reply #4 on: 2015-Dec-11 »
I see what you mean bigS and I did have something like this. Pardon the complexity and I don't expect a complete solution, although the idea of any type parent was so that it could be generally called, well in this case the fruit may also be not in of basket type (eg. basket, box, bag have item_id).

Code: GLBasic [Select]

TYPE _container
    item_id
    fruit_sealed AS _group
    fruit_fresh AS _group
ENDTYPE

GLOBAL box as _container  // not the same as _base