Peter N Lewis wrote:
(For callbacks etc., once we have an accepted mechanism, such as `Discard', one can just as well add it immediately.)
One thing that would be good, and generally is not done, is to generate a hint/warning/error in the case where you specify that a parameter is unused, but then in fact use it (since this likely indicates either an error or at the least obscure code indicating one thing and doing another).
I agree that it can be useful. (Though, unfortunately, this would complicate the implementation again. Currently `unused' attributes are handled by just marking the thing as used. For this warning we'd need another flag and extra code to handle the two flags. It's all more work than I'd like to spend on the matter, at least for now ...)
To my mind, the cleanest way in GPC is attribute (unused) on the formal parameter, but one down side of this is I don't believe GPC allows you to specify that in only the implementation (for example, if I have a unit:
interface function doit( a: Integer; b: Integer ): Integer; implementation function doit( a: Integer; b: Integer attribute (unused) ): Integer; begin return a; end;
That would not be possible in GPC would it?
That's up to the (yet to be done) implementation. I think it would be possible to allow it in the implementation. We don't allow routine attributes there, but we do that on purposes, because (at least some of the) attributes there would lead to nonsense, such as specifying a different linker name or calling convention after the routine has been exported and perhaps called from within the current module.
Provided it's clear that parameter attributes can never influence the interface, it should be possible to allow them in the implementation.
But now that you wrote the example, I see a syntactic problem. We already have the syntax `<type> <attribute>' for type attributes. Though we don't allow type attributes in parameters (more or less in the spirit of Pascal's forbidding new structured types there), I wouldn't like to have the same syntax mean something different here.
For variable's attributes, we have `var <name> = <type>; <attribute>'. But wouldn't this look strange in a parameter list?
function doit( a: Integer; b: Integer; attribute (unused) ): Integer;
This would at first glance look like another parameter (though syntactically invalid, even with procedural parameters and all the extensions we have, AFAICS, so maybe not a conflict, but still strange) ...
A related question is if we should simply allow `unused' (and perhaps more) as type attributes. I.e., we'd create a variant of a type which is generally unused. This sounds strange, of course. I'm reminded of this, because I've once wondered if we should have `static' as a type attribute (though I'm not sure yet if I'd really need it, probably not) ...
Obviously, the easiest thing for me would be just to implement the {$unused(b)} directive,
I think this would be quite a bit more difficult, and raises some new questions, since directives are outside of the syntax rules. E.g.:
procedure foo (a: Integer);
{$unused(a)}
procedure bar (a: Integer); begin end;
begin end;
Which `a' is meant here? The outer one? What if you move the directive after the word `procedure', is it the inner one now? If not, it would seem confusing. If so, it would need to reference an identifier before it's declared, also bad ...
Frank