comp.lang.ada
 help / color / mirror / Atom feed
* How to build Objects containing itself?
@ 2008-06-20 22:06 snoopysalive
  2008-06-20 22:42 ` george.priv
  0 siblings, 1 reply; 3+ messages in thread
From: snoopysalive @ 2008-06-20 22:06 UTC (permalink / raw)


Hi!

So, here's my next question about object orientation in Ada.

In languages like Ruby, C++ or Java, the following construct is
possible (here in Java):

class State {
    HashMap<char, State> transitions = new HashMap<char, State>();
}

Perhaps you're wondering, what this code is meant to be. Some time ago
I programmed a stack-structure simulating a trie as part of a
incremental search-algorithm. The idea was to have a state-object
containing a mapping of characters pointing to state-objects again.
So, the result is a data-structure containing instances of its own
data type again.

As object orientation is a bit different in Ada, the previous code
example would be something like this in Ada:

package State is

    type State is tagged
        record
            Transitions : Transition_Maps.Map;
        end record;

    package Transition_Maps is new Ada.Containers.Ordered_Maps
        (Key_Type => Character,
         Element_Type => State,
         "<" => "<",
         "=" => "=");
    use Transition_Maps;

end State;


As you can see, we have a "What had been there first? The hen or the
egg?"-problem: The State-record requires an instance of
Transition_Maps.Map which is declared after the State-record's
definition. But because Transition_Maps.Map needs a State as
Element_Type changing the order ends in the same problem.

So, has anybody an idea, how to solve this paradox?

Thanks,
Matthias



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to build Objects containing itself?
  2008-06-20 22:06 How to build Objects containing itself? snoopysalive
@ 2008-06-20 22:42 ` george.priv
  2008-06-21  5:57   ` Ivan Levashew
  0 siblings, 1 reply; 3+ messages in thread
From: george.priv @ 2008-06-20 22:42 UTC (permalink / raw)


On Jun 20, 6:06 pm, "snoopysal...@googlemail.com"
<snoopysal...@googlemail.com> wrote:
> Hi!
>
> So, here's my next question about object orientation in Ada.
>
> In languages like Ruby, C++ or Java, the following construct is
> possible (here in Java):
>
> class State {
>     HashMap<char, State> transitions = new HashMap<char, State>();
>
> }
>
> Perhaps you're wondering, what this code is meant to be. Some time ago
> I programmed a stack-structure simulating a trie as part of a
> incremental search-algorithm. The idea was to have a state-object
> containing a mapping of characters pointing to state-objects again.
> So, the result is a data-structure containing instances of its own
> data type again.
>
> As object orientation is a bit different in Ada, the previous code
> example would be something like this in Ada:
>
> package State is
>
>     type State is tagged
>         record
>             Transitions : Transition_Maps.Map;
>         end record;
>
>     package Transition_Maps is new Ada.Containers.Ordered_Maps
>         (Key_Type => Character,
>          Element_Type => State,
>          "<" => "<",
>          "=" => "=");
>     use Transition_Maps;
>
> end State;
>
> As you can see, we have a "What had been there first? The hen or the
> egg?"-problem: The State-record requires an instance of
> Transition_Maps.Map which is declared after the State-record's
> definition. But because Transition_Maps.Map needs a State as
> Element_Type changing the order ends in the same problem.
>
> So, has anybody an idea, how to solve this paradox?
>
> Thanks,
> Matthias

I am not sure what do you mean by including C++ there, but in Java all
objects are dynamic references.  So in order to make your type
equivalent to Java one should use 1) references when defining the map
and 2) Advance declaration of types (that will be also true for C++).
Otherwise you will have infinitely recursive type declaration that
compiler guards against.

   type State;
   type State_Access is access all State;

     package Transition_Maps is new Ada.Containers.Ordered_Maps
        (Key_Type => Character,
         Element_Type => State_Access,
         "<" => "<",
         "=" => "=");
   use Transition_Maps;

   type State is tagged
        record
            Transitions : Transition_Maps.Map;
        end record;



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to build Objects containing itself?
  2008-06-20 22:42 ` george.priv
@ 2008-06-21  5:57   ` Ivan Levashew
  0 siblings, 0 replies; 3+ messages in thread
From: Ivan Levashew @ 2008-06-21  5:57 UTC (permalink / raw)


snoopysalive@googlemail.com пишет:

>> As object orientation is a bit different in Ada, the previous code 
>> example would be something like this in Ada:

This has nothing to do with OO. Its a mere typesystem.


george.priv@gmail.com пишет:

> I am not sure what do you mean by including C++ there, but in Java all
> objects are dynamic references.

That's right. References, not instances here.

>> The idea was to have a state-object containing a mapping of 
>> characters *pointing* to state-objects again. So, the result is a 
>> data-structure /containing/ instances of its own data type again.

-- 
If you want to get to the top, you have to start at the bottom



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-06-21  5:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-20 22:06 How to build Objects containing itself? snoopysalive
2008-06-20 22:42 ` george.priv
2008-06-21  5:57   ` Ivan Levashew

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox