From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ae138939b724a2de X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,UTF8 Path: g2news1.google.com!news3.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Mart van de Wege Newsgroups: comp.lang.ada Subject: Re: Single element arrays as function parameters Date: Sat, 11 Jun 2011 00:24:15 +0200 Message-ID: <8662oduxyo.fsf@gareth.avalon.lan> References: <86ei31v43e.fsf@gareth.avalon.lan> <86aadpv0jp.fsf@gareth.avalon.lan> <529356dd-fe4a-4dc0-9415-16ebdb5f611d@q12g2000prb.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: individual.net OS1fRAmdxit8mxJ5uNYHXwXn3FjKDzFqLDGvwnDkt9BC2wWwQ/ X-Orig-Path: gareth.avalon.lan!not-for-mail Cancel-Lock: sha1:4TujkO4aAKdwLs/Vq9vL/CIEWtU= sha1:As2Lgj+RCdcyejjs1V7jsCtzsvg= User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) Xref: g2news1.google.com comp.lang.ada:19741 Date: 2011-06-11T00:24:15+02:00 List-Id: Adam Beneschan writes: > On Jun 10, 2:28 pm, Mart van de Wege wrote: >> "J-P. Rosen" 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.