comp.lang.ada
 help / color / mirror / Atom feed
* Single element arrays as function parameters
@ 2011-06-10 20:11 Mart van de Wege
  2011-06-10 20:54 ` J-P. Rosen
  2011-06-11 12:28 ` Peter C. Chapin
  0 siblings, 2 replies; 7+ messages in thread
From: Mart van de Wege @ 2011-06-10 20:11 UTC (permalink / raw)


I have the following function:
    
    function Creature (Name	: in String;
		       Race	: in MM_Subtype;
		       Level	: in Positive;
		       Player	: in String)
		      return Character_Ptr is
    C : Character_Ptr;
    Temp : Integer;
    begin
       C := new Player_Character;
       C.Creature_Name := To_Unbounded_String(Name);
       C.Creature_Type := Humanoid;
       C.Creature_Subtype := new MM_Subtype'(Race);
       C.Character_Level := Class_Level(Level);
       C.Player := To_Unbounded_String(Player);
       return C;
    end Creature;
    
It will update the following record:

   type Creature is 
     tagged record
      Creature_Name : Unbounded_String;
      Creature_Type : MM_Type; 
      Creature_Subtype : access MM_Subtype; 
   end record;

(actually, it will update a derived type with a few extra fields, but
that is not important right now).

It works just fine when called like this:

M := Init.Creature( Name => "Duergar",
   		       Creature_Type => Humanoid,
   		       Creature_Subtype => (Dwarf,Psionic) );

But when called like this 

M := Init.Creature( Name => "Dwarf",
   		       Creature_Type => Humanoid,
   		       Creature_Subtype => (Dwarf) );

The above call fails to compile:

test_create.adb:11:13: no candidate interpretations match the actuals:
test_create.adb:11:13: missing argument for parameter "Race" in call to "Creature" declared at games-rpg-dnd-creatures.ads:47
test_create.adb:11:13: missing argument for parameter "Level" in call to "Creature" declared at games-rpg-dnd-creatures.ads:44
test_create.adb:13:45: positional aggregate cannot have one component
test_create.adb:13:45:   ==> in call to "Creature" at
games-rpg-dnd-creatures.ads:40

If I look at the language documentation, I'm guessing that this is
because a single element array does not get recognised as being of type
MM_Subtype, which is defined as 

type MM_Subtype is array(Natural range <>) of Possible_MM_Subtype;

What *is* the best way to store an attribute which may be either a
single enumeration element or a list of enumeration elements?

Mart

-- 
"We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.



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

* Re: Single element arrays as function parameters
  2011-06-10 20:11 Single element arrays as function parameters Mart van de Wege
@ 2011-06-10 20:54 ` J-P. Rosen
  2011-06-10 21:28   ` Mart van de Wege
  2011-06-11 12:28 ` Peter C. Chapin
  1 sibling, 1 reply; 7+ messages in thread
From: J-P. Rosen @ 2011-06-10 20:54 UTC (permalink / raw)


Le 10/06/2011 22:11, Mart van de Wege a �crit :

> M := Init.Creature( Name => "Dwarf",
>    		       Creature_Type => Humanoid,
>    		       Creature_Subtype => (Dwarf) );
> 
> The above call fails to compile:
> 
> test_create.adb:13:45: positional aggregate cannot have one component
This is the clue: it is interpreted as a parenthesized value, not as an
aggregate. Just write:

 M := Init.Creature( Name => "Dwarf",
    		       Creature_Type => Humanoid,
    		       Creature_Subtype => (1 => Dwarf) );
-- 
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Adalog a d�m�nag� / Adalog has moved:
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00



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

* Re: Single element arrays as function parameters
  2011-06-10 20:54 ` J-P. Rosen
@ 2011-06-10 21:28   ` Mart van de Wege
  2011-06-10 21:58     ` Adam Beneschan
  0 siblings, 1 reply; 7+ messages in thread
From: Mart van de Wege @ 2011-06-10 21:28 UTC (permalink / raw)


"J-P. Rosen" <rosen@adalog.fr> writes:

> Le 10/06/2011 22:11, Mart van de Wege a écrit :
>
>> M := Init.Creature( Name => "Dwarf",
>>    		       Creature_Type => Humanoid,
>>    		       Creature_Subtype => (Dwarf) );
>> 
>> The above call fails to compile:
>> 
>> test_create.adb:13:45: positional aggregate cannot have one component
> This is the clue: it is interpreted as a parenthesized value, not as an
> aggregate. Just write:
>
>  M := Init.Creature( Name => "Dwarf",
>     		       Creature_Type => Humanoid,
>     		       Creature_Subtype => (1 => Dwarf) );

Brrr. That works, but it looks a bit...ugly. Especially considering that
creatures only having one subtype[1] is the norm in Dungeons&Dragons
(the application in question is meant to help manage large amounts of
data to assist in running a campaign).

If there is no more elegant solution, I'll fix this by adding the
necessary remarks to the API description, in the unlikely case that
someone else except me is ever interested in this application.

Mart

[1] Side note: automating D&D in Ada is a pain, given the amount of
overlap between game terms and Ada keywords: Character, Class, Type,
Subtype...

-- 
"We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.



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

* Re: Single element arrays as function parameters
  2011-06-10 21:28   ` Mart van de Wege
@ 2011-06-10 21:58     ` Adam Beneschan
  2011-06-10 22:24       ` Mart van de Wege
  0 siblings, 1 reply; 7+ messages in thread
From: Adam Beneschan @ 2011-06-10 21:58 UTC (permalink / raw)


On Jun 10, 2:28 pm, Mart van de Wege <mvdw...@mail.com> wrote:
> "J-P. Rosen" <ro...@adalog.fr> writes:
> > Le 10/06/2011 22:11, Mart van de Wege a écrit :
>
> >> M := Init.Creature( Name => "Dwarf",
> >>                       Creature_Type => Humanoid,
> >>                       Creature_Subtype => (Dwarf) );
>
> >> The above call fails to compile:
>
> >> test_create.adb:13:45: positional aggregate cannot have one component
> > This is the clue: it is interpreted as a parenthesized value, not as an
> > aggregate. Just write:
>
> >  M := Init.Creature( Name => "Dwarf",
> >                           Creature_Type => Humanoid,
> >                           Creature_Subtype => (1 => Dwarf) );
>
> Brrr. That works, but it looks a bit...ugly. Especially considering that
> creatures only having one subtype[1] is the norm in Dungeons&Dragons
> (the application in question is meant to help manage large amounts of
> data to assist in running a campaign).
>
> If there is no more elegant solution, I'll fix this by adding the
> necessary remarks to the API description, in the unlikely case that
> someone else except me is ever interested in this application.

Your original post doesn't make sense; it defines a function Creature
with parameters Name, Race, Level, and Player, but then you're calling
a function Creature that has parameters Name, Creature_Type, and
Creature_Subtype.  Actually, though, this would explain some of the
error messages in your original post (such as missing "Race" and
"Level" arguments).

Assuming you meant something like

  Race => (Dwarf)

one solution is to define an overloaded Creature function that takes a
single element instead of an array.  Assuming MM_Subtype is an array
of Element_Type (since I don't know what the actual name is), you can
write something like

    function Creature (Name     : in String;
                       Race     : in Element_Type;
                       Level    : in Positive;
                       Player   : in String)
                      return Character_Ptr is
    begin
       return Creature (Name => Name, Race => (1 => Race), Level =>
Level, Player => Player);
    end Creature;

and now the rest of your program can call Creature with either a
single element or an array as the Race.  (And you wouldn't need to
parenthesize the single element when you call it, but you could.)

                             -- Adam



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

* Re: Single element arrays as function parameters
  2011-06-10 21:58     ` Adam Beneschan
@ 2011-06-10 22:24       ` Mart van de Wege
  0 siblings, 0 replies; 7+ messages in thread
From: Mart van de Wege @ 2011-06-10 22:24 UTC (permalink / raw)


Adam Beneschan <adam@irvine.com> writes:

> On Jun 10, 2:28 pm, Mart van de Wege <mvdw...@mail.com> wrote:
>> "J-P. Rosen" <ro...@adalog.fr> writes:
>> > Le 10/06/2011 22:11, Mart van de Wege a écrit :
>>
>> >> M := Init.Creature( Name => "Dwarf",
>> >>                       Creature_Type => Humanoid,
>> >>                       Creature_Subtype => (Dwarf) );
>>
>> >> The above call fails to compile:
>>
>> >> test_create.adb:13:45: positional aggregate cannot have one component
>> > This is the clue: it is interpreted as a parenthesized value, not as an
>> > aggregate. Just write:
>>
>> >  M := Init.Creature( Name => "Dwarf",
>> >                           Creature_Type => Humanoid,
>> >                           Creature_Subtype => (1 => Dwarf) );
>>
>> Brrr. That works, but it looks a bit...ugly. Especially considering that
>> creatures only having one subtype[1] is the norm in Dungeons&Dragons
>> (the application in question is meant to help manage large amounts of
>> data to assist in running a campaign).
>>
>> If there is no more elegant solution, I'll fix this by adding the
>> necessary remarks to the API description, in the unlikely case that
>> someone else except me is ever interested in this application.
>
> Your original post doesn't make sense; it defines a function Creature
> with parameters Name, Race, Level, and Player, but then you're calling
> a function Creature that has parameters Name, Creature_Type, and
> Creature_Subtype.  

Erm yes. That function initializes a Player_Character type, which is a
derived type of Creature, and thus has a few more fields.

In case of Player_Character the Creature_Type field is hardcoded to be
'Human', and the 'Race' parameter will initialize the Creature_Subtype
field inherited from the Creature (abstract) type.

> Actually, though, this would explain some of the error messages in
> your original post (such as missing "Race" and "Level" arguments).
>
The 'Creature' *function* is an overloaded function from a child package
(a constructor, basically) that initializes either a Monster, a
Non_Player_Character or a Player_Character type (which are all derived
from the Creature abstract type). The overloading works fine, as long as
I take care to pass a one-element 'Race' or 'Creature_Subtype' parameter
correctly.

If I don't, the overloading fails because there is no initialization
function with the right function profile.

I'm not happy with it right now, because it mingles a constructor with
an initialization function. I might refactor that, in which case the
rest of your post is very helpful.

> Assuming you meant something like
>
>   Race => (Dwarf)
>
> one solution is to define an overloaded Creature function that takes a
> single element instead of an array.  Assuming MM_Subtype is an array
> of Element_Type 

It's an array of Possible_MM_Subtype, which is an Enumeration. I like to
use Enumeration types for this as it will fail if I try to assign a
non-existant creature subtype.

> (since I don't know what the actual name is), you can
> write something like
>
>     function Creature (Name     : in String;
>                        Race     : in Element_Type;
>                        Level    : in Positive;
>                        Player   : in String)
>                       return Character_Ptr is
>     begin
>        return Creature (Name => Name, Race => (1 => Race), Level =>
> Level, Player => Player);
>     end Creature;
>
> and now the rest of your program can call Creature with either a
> single element or an array as the Race.  (And you wouldn't need to
> parenthesize the single element when you call it, but you could.)
>
>                              -- Adam

Neat, thanks. That's a different solution, but I might have a use for
that (see above).

Mart
-- 
"We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.



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

* Re: Single element arrays as function parameters
  2011-06-10 20:11 Single element arrays as function parameters Mart van de Wege
  2011-06-10 20:54 ` J-P. Rosen
@ 2011-06-11 12:28 ` Peter C. Chapin
  2011-06-13 14:49   ` Adam Beneschan
  1 sibling, 1 reply; 7+ messages in thread
From: Peter C. Chapin @ 2011-06-11 12:28 UTC (permalink / raw)


On Fri, 10 Jun 2011 22:11:49 +0200, Mart van de Wege wrote:

> If I look at the language documentation, I'm guessing that this is
> because a single element array does not get recognised as being of type
> MM_Subtype, which is defined as
> 
> type MM_Subtype is array(Natural range <>) of Possible_MM_Subtype;
> 
> What *is* the best way to store an attribute which may be either a
> single enumeration element or a list of enumeration elements?

What about qualifying the aggregate:

   MM_Subtype'(Dwarf)

Peter




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

* Re: Single element arrays as function parameters
  2011-06-11 12:28 ` Peter C. Chapin
@ 2011-06-13 14:49   ` Adam Beneschan
  0 siblings, 0 replies; 7+ messages in thread
From: Adam Beneschan @ 2011-06-13 14:49 UTC (permalink / raw)


On Jun 11, 5:28 am, "Peter C. Chapin" <PCha...@vtc.vsc.edu> wrote:
> On Fri, 10 Jun 2011 22:11:49 +0200, Mart van de Wege wrote:
>
> > If I look at the language documentation, I'm guessing that this is
> > because a single element array does not get recognised as being of type
> > MM_Subtype, which is defined as
>
> > type MM_Subtype is array(Natural range <>) of Possible_MM_Subtype;
>
> > What *is* the best way to store an attribute which may be either a
> > single enumeration element or a list of enumeration elements?
>
> What about qualifying the aggregate:
>
>    MM_Subtype'(Dwarf)

No, that's still illegal.  Since MM_Subtype is an array subtype,
(Dwarf) has to have the syntax of an aggregate, which means you still
need a named association:

    MM_Subtype'(1=>Dwarf)

                                -- Adam



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

end of thread, other threads:[~2011-06-13 14:49 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-10 20:11 Single element arrays as function parameters Mart van de Wege
2011-06-10 20:54 ` J-P. Rosen
2011-06-10 21:28   ` Mart van de Wege
2011-06-10 21:58     ` Adam Beneschan
2011-06-10 22:24       ` Mart van de Wege
2011-06-11 12:28 ` Peter C. Chapin
2011-06-13 14:49   ` Adam Beneschan

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