comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Instantiations of a generic with often encountered arguments
Date: Mon, 27 Nov 2017 08:05:43 -0800 (PST)
Date: 2017-11-27T08:05:43-08:00	[thread overview]
Message-ID: <8c831928-0a22-4d86-b848-007164754a7f@googlegroups.com> (raw)
In-Reply-To: <ovfrna$1hkd$1@gioia.aioe.org>

On Sunday, November 26, 2017 at 7:02:23 PM UTC-7, Victor Porton wrote:
> 
> I am now in analysis-paralysis of whether to define instantiations for often 
> encountered types in advance (globally) or immediately before use.

Ok, I completely understand that problem -- the right way will undoubtedly influence your codebase and you don't want to have to rewrite everything.

Unfortunately I can't give you a hard rule on how you should proceed because, like so many other things, it depends on what you're doing. (e.g. do you want to make SOME_TYPE limited & private, just private, just limited, or neither? It really depends, if you're passing around a handle where there's some sort of reference-counting going on, you'll want limited private to ensure that you're not accidentally mucking about on the internal count. OTOH, if you're implementing a parallel solver, you probably want your data non-limited so you can copy it and feed these copies to your worker-tasks freely.)

For generics, I've found that making a "global instantiation" generic is best when you're either (a) working on an entire [sub-]system that is generic [e.g. a parser that you're going to use multiple times with different syntax-sets] or (b) when the functions themselves are anticipated to be used widely in your code [e.g. the Ada.Numerics.Elementary_Functions example].

One last option is leveraging Ada's visibility/scoping rules to "mix and match" your options; example you have a generic package GENERIC_AUDIO_OPERATIONS containing subprograms that are going to be used all over the audio portion of an application:

With
Application.Generics.GENERIC_AUDIO_OPERATIONS;

Package Application.Subsystems.Audio is

Private
  Package Operations is New GENERIC_AUDIO_OPERATIONS( [[parameters]] );
  Use Operations;
  -- The instantiation is now directly visible/usable to the private and
  -- (IIRC) implementation portions of child packages.
End Application.Subsystems.Audio;

      parent reply	other threads:[~2017-11-27 16:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-26  0:04 Instantiations of a generic with often encountered arguments Victor Porton
2017-11-26  2:24 ` gautier_niouzes
2017-11-26  8:52 ` Dmitry A. Kazakov
2017-11-27  2:02   ` Victor Porton
2017-11-27  8:00     ` Simon Wright
2017-11-27  8:41     ` Dmitry A. Kazakov
2017-11-27 16:05     ` Shark8 [this message]
replies disabled

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