comp.lang.ada
 help / color / mirror / Atom feed
* compiler confusion, overloading and "subtype mark required in this context"
@ 2021-02-07  9:15 Mehdi Saada
  2021-02-07 10:59 ` AdaMagica
  0 siblings, 1 reply; 4+ messages in thread
From: Mehdi Saada @ 2021-02-07  9:15 UTC (permalink / raw)


In an exo to build a rational integers package, I'm having the surprise to get a heapload of "context requires function call, found procedure name". While there is no procedure of these names.

   function "+" (R1: T_rational; R2: T_rational) return T_rational is
     (if (denom(R1) = denom(R2)) then (ENUM(R1) + ENUM(r2))/ denom(R1)
      else (DENOM(R2)*ENUM(R1) + denom(r1)*ENUM(r2))/DENOM(R1)*denom(R2));

   function denom (fraction: T_rational) return Positive is (fraction.denom);
   function enum (fraction: T_rational) return Natural is (fraction.ENUM);

with usually a "no candidate interpretations match the actuals:" for the same stuff. There are several overloaded operators, but each time with different profiles so I'm very surprised it could cause trouble.

Another kind of oddity: several
 function "+" (R1: T_rational; R2: T_rational) return T_rational is
     (if (denom(R1) = denom(R2)) then (ENUM(R1) + ENUM(r2))/ denom(R1)
      else (DENOM(R2)*ENUM(R1) + denom(r1)*ENUM(r2))/DENOM(R1)*denom(R2));
cause "ambiguous expression (cannot resolve "+")" with "possible interpretation". -- here it picks
function "+" (R1: T_rational; R2: T_rational) return T_rational;
as an interpretation, which is bullocks since obviously I'm calling on Integer operations, between enum() and denom(), integer and natural, then calling on the constructor "/" who is the only "/" with the specific profile
   function "/" (N: Integer; D: Integer) return T_rational;

additional questions. I already looked up the rm. is there an attribute to 'min/'max integer without the sign (I "abs"ed them) ?

Last one:

The commented lines:
 function Get_Line return T_rational is
      use ada.Integer_Text_IO;
   begin
      loop
         declare
            S: string := Get_Line;
            Enume: integer;
            Denomi: Positive; ---- ##########
            Ind : Natural range 0..1 := 0;
            sign : T_SIGN := Positive;
            Last: Positive;
         begin
            if S(1) = '-' then SIGN := negative; ind := 0; end if;
            Get(S(s'First+ind..s'Last), Enume, Last);
            if last = S'Last then return (IF SIGN = NEGATIVE => -Enume/1 else ENUMe/1); ----------######

Both last ones are "subtype mark required in this context" while he's refering to:    type T_SIGN is (positive,negative);
But how can there be confusion ?

Thanks. this exo is suprisingly taxing... the numbers of details that go wrong !

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: compiler confusion, overloading and "subtype mark required in this context"
  2021-02-07  9:15 compiler confusion, overloading and "subtype mark required in this context" Mehdi Saada
@ 2021-02-07 10:59 ` AdaMagica
  2021-02-07 12:56   ` Mehdi Saada
  0 siblings, 1 reply; 4+ messages in thread
From: AdaMagica @ 2021-02-07 10:59 UTC (permalink / raw)


Without the full code, it's difficult to answer.

function "+" (R1: T_rational; R2: T_rational) return T_rational is
(if (denom(R1) = denom(R2)) then (ENUM(R1) + ENUM(r2))/ denom(R1)
else (DENOM(R2)*ENUM(R1) + denom(r1)*ENUM(r2))/DENOM(R1)*denom(R2)); 
If I count corretctly, looks like a closing ) is missing at the end. Thus in the else part, you have
Rational*Integer.

sign : T_SIGN := Positive;
Last: Positive; 
Here you have Positive as a type name and as an enumeration literal.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: compiler confusion, overloading and "subtype mark required in this context"
  2021-02-07 10:59 ` AdaMagica
@ 2021-02-07 12:56   ` Mehdi Saada
  2021-02-11  3:03     ` Randy Brukardt
  0 siblings, 1 reply; 4+ messages in thread
From: Mehdi Saada @ 2021-02-07 12:56 UTC (permalink / raw)


> If I count corretctly, looks like a closing ) is missing at the end. Thus in the else part, you have 
> Rational*Integer.
Ahhhh... I'm not used to these kinds of error messages ! I kinda understand what happened later in functions then... just kind :-)
Experiences makes good eyes... well I see myself getting better at it too.
> Here you have Positive as a type name and as an enumeration literal.
I saw, but it seemed to me the context was clear enough, I'm surprised gnat doesn't agree with me.

I'll say "thanks" when I'll have get rid of this:
[2021-02-07 12:47:18] Could not convert compiler output to UTF8
I did see a post about it, somewhat, but... way too long, so is there a quick fix to add project properties or whatnot ?
I think it happened by copying from a pdf.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: compiler confusion, overloading and "subtype mark required in this context"
  2021-02-07 12:56   ` Mehdi Saada
@ 2021-02-11  3:03     ` Randy Brukardt
  0 siblings, 0 replies; 4+ messages in thread
From: Randy Brukardt @ 2021-02-11  3:03 UTC (permalink / raw)


"Mehdi Saada" <00120260a@gmail.com> wrote in message 
news:4360fbab-d22f-4a21-bc9b-aebe6b3c1faan@googlegroups.com...
>> If I count corretctly, looks like a closing ) is missing at the end. Thus 
>> in the else part, you have
>> Rational*Integer.
> Ahhhh... I'm not used to these kinds of error messages ! I kinda 
> understand what happened later in functions then... just kind :-)
> Experiences makes good eyes... well I see myself getting better at it too.
>> Here you have Positive as a type name and as an enumeration literal.
> I saw, but it seemed to me the context was clear enough, I'm surprised 
> gnat doesn't agree with me.

Type names don't allow overloading. So if you have both an enumeration 
literal and a type name, you can only get one or the other (or neither in 
some cases). If you're using both, something has to be illegal. Only 
subprograms can be overloaded (enumeration literals are technically 
functions).

                               Randy.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-02-11  3:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-07  9:15 compiler confusion, overloading and "subtype mark required in this context" Mehdi Saada
2021-02-07 10:59 ` AdaMagica
2021-02-07 12:56   ` Mehdi Saada
2021-02-11  3:03     ` Randy Brukardt

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