comp.lang.ada
 help / color / mirror / Atom feed
* Doubts on Function Overloading
@ 2019-06-17 22:31 Ricardo Brandão
  2019-06-17 23:15 ` Keith Thompson
  0 siblings, 1 reply; 3+ messages in thread
From: Ricardo Brandão @ 2019-06-17 22:31 UTC (permalink / raw)


Hi,

In the very first program I tried after finish the tutorial from Learn.Adacore.com I got some compile errors related to Overloading.

My idea was verify how Ada deal with JSON. So, I found out this program [1].

I created a project on GPS, included GnatCool as dependence, and when I tried to compile, I got the error:

λ gprbuild.exe main.adb
using project file adajson.gpr
Compile
   [Ada]          main.adb
main.adb:16:11: ambiguous call to "Set_Field"
main.adb:16:11: possible interpretation at gnatcoll-json.ads:317
main.adb:16:11: possible interpretation at gnatcoll-json.ads:323
main.adb:30:11: ambiguous call to "Set_Field"
main.adb:30:11: possible interpretation at gnatcoll-json.ads:317
main.adb:30:11: possible interpretation at gnatcoll-json.ads:323
gprbuild: *** compilation phase failed

The source code is:

13   Penguin.Set_Field (Field_Name => "name", 
14                      Field      => "Linux");
15 
16   Penguin.Set_Field (Field_Name => "born",
17                      Field      => 1992);


28   Penguin := Read (JSON_String, "json.errors");
29 
30   Penguin.Set_Field (Field_Name => "born",
31                      Field      => 1986);

The source code of gnatcool-json.ads cited on error:

317   procedure Set_Field
318     (Val        : JSON_Value;
319      Field_Name : UTF8_String;
320      Field      : Integer)
321      with Pre => Val.Kind = JSON_Object_Type;
322
323   procedure Set_Field
324     (Val        : JSON_Value;
325      Field_Name : UTF8_String;
326      Field      : Long_Integer)
327      with Pre => Val.Kind = JSON_Object_Type;


If I changed 1992 to "1992" on line 16, and 1986 to "1986" on line 30, the program compile ok.

Why compile didn't resolve correctly, as stated here [2]?

Thanks


[1] https://rosettacode.org/wiki/JSON#Ada
[2] https://learn.adacore.com/books/Ada_For_The_CPP_Java_Developer/chapters/06_Functions_and_Procedures.html?#overloading

--
Ricardo Brandão


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

* Re: Doubts on Function Overloading
  2019-06-17 22:31 Doubts on Function Overloading Ricardo Brandão
@ 2019-06-17 23:15 ` Keith Thompson
  2019-06-18  1:03   ` Ricardo Brandão
  0 siblings, 1 reply; 3+ messages in thread
From: Keith Thompson @ 2019-06-17 23:15 UTC (permalink / raw)


Ricardo Brandão <rbrandao.br@gmail.com> writes:
> In the very first program I tried after finish the tutorial from
> Learn.Adacore.com I got some compile errors related to Overloading.
>
> My idea was verify how Ada deal with JSON. So, I found out this
> program [1].
>
> I created a project on GPS, included GnatCool as dependence, and when
> I tried to compile, I got the error:
>
> λ gprbuild.exe main.adb
> using project file adajson.gpr
> Compile
>    [Ada]          main.adb
> main.adb:16:11: ambiguous call to "Set_Field"
> main.adb:16:11: possible interpretation at gnatcoll-json.ads:317
> main.adb:16:11: possible interpretation at gnatcoll-json.ads:323
> main.adb:30:11: ambiguous call to "Set_Field"
> main.adb:30:11: possible interpretation at gnatcoll-json.ads:317
> main.adb:30:11: possible interpretation at gnatcoll-json.ads:323
> gprbuild: *** compilation phase failed
>
> The source code is:
>
> 13   Penguin.Set_Field (Field_Name => "name", 
> 14                      Field      => "Linux");
> 15 
> 16   Penguin.Set_Field (Field_Name => "born",
> 17                      Field      => 1992);
>
>
> 28   Penguin := Read (JSON_String, "json.errors");
> 29 
> 30   Penguin.Set_Field (Field_Name => "born",
> 31                      Field      => 1986);
>
> The source code of gnatcool-json.ads cited on error:
>
> 317   procedure Set_Field
> 318     (Val        : JSON_Value;
> 319      Field_Name : UTF8_String;
> 320      Field      : Integer)
> 321      with Pre => Val.Kind = JSON_Object_Type;
> 322
> 323   procedure Set_Field
> 324     (Val        : JSON_Value;
> 325      Field_Name : UTF8_String;
> 326      Field      : Long_Integer)
> 327      with Pre => Val.Kind = JSON_Object_Type;
>
>
> If I changed 1992 to "1992" on line 16, and 1986 to "1986" on line 30, the program compile ok.

I presume there's an overload of Set_Field that has Field as a String
(or UTF8_String).

> Why compile didn't resolve correctly, as stated here [2]?

Because the literal 1992 doesn't distinguish between Integer and
Long_Integer.  Integer literals are of type *universal_integer*,
which can be implicitly converted to any integer type.
(The asterisks denote italics; the name "universal_integer" is
not visible.)

Try this:

    Penguin.Set_Field (Field_Name => "born",
                       Field      => Integer'(1992));

so the argument is visibly of type Integer, not univeral_integer.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

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

* Re: Doubts on Function Overloading
  2019-06-17 23:15 ` Keith Thompson
@ 2019-06-18  1:03   ` Ricardo Brandão
  0 siblings, 0 replies; 3+ messages in thread
From: Ricardo Brandão @ 2019-06-18  1:03 UTC (permalink / raw)


Hi Keith

> Try this:
> 
>     Penguin.Set_Field (Field_Name => "born",
>                        Field      => Integer'(1992));
> 
> so the argument is visibly of type Integer, not univeral_integer.
> 

This worked fine! 

Thanks

--
Ricardo Brandão


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

end of thread, other threads:[~2019-06-18  1:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-17 22:31 Doubts on Function Overloading Ricardo Brandão
2019-06-17 23:15 ` Keith Thompson
2019-06-18  1:03   ` Ricardo Brandão

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