Combine `with` and `use` clauses

Hear me out, please, before you get the pitchforks ready. Testing the waters here. :slightly_smiling_face:

Now, I’m surely not the first to complain about the with ... and use ... code repetition at the top of a file. And introducing a new keyword like import, for example, is not an easy task considering backwards compatibility and such, but what if we resort to a simple lowering by re-using what we have at our disposal?

Given that we can already write with clauses like this:

with A, B, C;

What if we allowed the following?

with and use Ada.Text_IO, Ada.Strings.Fixed, Foo.Bar.Baz;

It could then be simply lowered to the same old:

with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Foo.Bar.Baz;       use Foo.Bar.Baz;

or to the equivalent:

with Ada.Text_IO, Ada.Strings.Fixed, Foo.Bar.Baz;
use  Ada.Text_IO, Ada.Strings.Fixed, Foo.Bar.Baz;

Would this trigger any major side-effects? It requires changes to the grammar, sure, but it doesn’t introduce any keywords and shouldn’t have any impact on existing code since with and use isn’t legal, as far as I know.

Is anyone aware of any AIs that have discussed and dismissed such ideas before?

This is already legal.
Example from Byron:

Pragma Ada_2012;
Pragma Assertion_Policy( Check );

with
System,
Interfaces,
Unchecked_Conversion,
Ada.Strings.Fixed,
Ada.Strings.Wide_Wide_Fixed,
Ada.Strings.Unbounded,
Ada.Strings.UTF_Encoding.Conversions,
Ada.Strings.UTF_Encoding.Wide_Wide_Strings,
Ada.Characters.Conversions,
ADA.IO_EXCEPTIONS;

Function Readington (File : Byron.Internals.Types.Stream_Class) return Wide_Wide_String is
--…

Granted, there’s no Use there, but it’d be similar.

Pitchfork ? How cute. You meant the heavy flamethrower
Seriously, Use is already something you should be wary of, and employed as locally as possible, so a systematic with and use would be against the spirit of the language / good practices, and of little use in practical code as far as I’ve read. What you do is shoehorning habits from other languages, instead of adopting Ada’s own.

1 Like

Yes, which is why I proposed lowering to that form.

But the use in with and use is the whole point of the above.

Just to clarify, I would like with and use ... to get lowered to the usual with ...; use ... clauses by the compiler. Let’s optimize for fun(O).

You can find ``Proposal for a “with and use” clause´´ in the Ada-Comment mailing list, from 2003 !..
There are also traces of that idea in comp.lang.ada: Clause "with and use" .
Back in the day the author (me) was used to the syntax of a famous Pascal dialect that uses “uses” with a similar meaning (but not the same exact semantics).
In the meantime, with growing projects and libraries, I appreciate the approach with only “with”, and using “use” 's the most locally possible.

4 Likes

A better proposal long ago was that use would simply imply with. So just:

use Ada.Text_IO;

You will find no support, since majority hate use clause being too lazy to design package specifications that would work with it. Then you cannot do it use-friendly with generics anyway.

I would rather have a renames included:

with SDL.Video as Video;

Or

with Video renames SDL.Video;
4 Likes

Funnily enough, the GNAT compiler’s sources have long lists of context clauses like this all over the place:

with X; use X;
with Y; use Y;
...

So at least that software would benefit from such a proposal :slight_smile: .

3 Likes