comp.lang.ada
 help / color / mirror / Atom feed
From: Mart van de Wege <mvdwege@mail.com>
Subject: Re: Single element arrays as function parameters
Date: Sat, 11 Jun 2011 00:24:15 +0200
Date: 2011-06-11T00:24:15+02:00	[thread overview]
Message-ID: <8662oduxyo.fsf@gareth.avalon.lan> (raw)
In-Reply-To: 529356dd-fe4a-4dc0-9415-16ebdb5f611d@q12g2000prb.googlegroups.com

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.



  reply	other threads:[~2011-06-10 22:24 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2011-06-11 12:28 ` Peter C. Chapin
2011-06-13 14:49   ` Adam Beneschan
replies disabled

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