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.

3 Likes

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.

1 Like

I would rather have a renames included:

with SDL.Video as Video;

Or

with Video renames SDL.Video;
5 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

I’ve by passed many earlier versions of my project’s with and use, that had too many circular uses dependencies and instead have used child packages as much as possible. Not perfect as it can sort of cludge ‘global variables’ (to the package’s hierarchy tree) declarations. I will go and refine later; for now I am at IOC, initial operation capability, a fully functional version 1, with plenty of refactoring to do. But I feel an IOC is first step, pretty or not.

In December 2017 I submitted a proposal for a “use with” clause to the ARG, but it was denied. See http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/ai12-0245-1.txt?rev=1.3&raw=N

The AI is misnamed since I specifically proposed “use with”, not “with and use”.

A “use with ‘package’;” clause would be allowed anywhere that same line “with ‘package’; use ‘package’;” clauses are allowed. It would be “syntactic sugar” for the usual longer form.

I like “use with” instead of “with use” so that a vertical list of “with” and “use with” clauses on adjacent lines can be easily alphabetically sorted into “use with” and “with” groups.

It would allow easily activating/deactivating the use clause semantics by simply inserting/removing "use " before the corresponding with clause.

My original motivation for proposing this was annoyance at having to edit Ada “user code” in the primitive little edit boxes provided by a certain unit test tool.

I tend to agree with the ARG reasoning. I don’t use use clause in general. When I read code, I dislike having to question ‘from which package does this function or procedure comes from?’. I much prefer renames for long package names and use type.

For the proposed syntax, I would just say use at package level implies a with and not add a use with syntax. But that would add a lot of incoherences to the langage not worth the burden.

1 Like

I prefer using “use” in nested levels where I really want to avoid the package name. This way it’s easier to know where something came from and does not require an IDE. Some people point out that we have fancy navigation features in IDEs to help figure out where things are. However, code reviews are often done outside of the IDE (eg GitLab merge requests). Having less confusion at the code level makes it more convenient for the reviewer.

4 Likes

This would make more sense since we sometimes have to write something line

with Ada.Sequential_IO;

...
package IO is new Ada.Sequential_IO(Interfaces.Unsigned_8)

Which is a with without a use. So use would imply with but with wouldn’t imply use.