arclight
arclight

Reputation: 1608

How to unambiguously specify a character literal

As I've been working through "Rendezvous with Ada 95", I've run into what seems like a common Ada83 / Ada95 incompatibility: the dreaded ambiguous character literal error in the line:

Put ( Disk < Screen and 'P' < 'B' );

I'm new to Ada but not to strongly-typed languages and I understand the issue: there's no way to determine if a literal is of type Character or of Wide_Character and that ambiguity results in a compile-time error.

My question is, how would one change the statement above to specify a character literal with a given type (e.g. as a Character or Wide_Character)? I've read over similar questions (What caused this Ada compilation error "ambiguous character literal"?) and they don't provide any guidance beyond RTFM. Other references (https://gcc.gnu.org/onlinedocs/gnat_rm/Legal-Ada-83-programs-that-are-illegal-in-Ada-95.html) show workarounds for character ranges but not for single literals.

In a different language, I'd expect to cast the literals to a given type but my impression of Ada is that weaseling around type-checking would (should?) be discouraged. I know I could just explicitly define the literals as constants of a given type but that seems to defeat the purpose of allowing literals in the body of the code.

I imagine this is a very common issue when modernizing Ada83 code; what is the best practice for disambiguating ambiguous character literals inline?

Upvotes: 2

Views: 280

Answers (1)

trashgod
trashgod

Reputation: 205785

Use a qualified expression to identify the type explicitly:

Put ( Disk < Screen and Character'('P') < Character'('B') );

Upvotes: 4

Related Questions