comp.lang.ada
 help / color / mirror / Atom feed
* Rosetta Code task Proper divisors fails to compile
@ 2023-07-26  4:49 Kenneth Wolcott
  2023-07-26  8:36 ` Jeffrey R.Carter
  0 siblings, 1 reply; 5+ messages in thread
From: Kenneth Wolcott @ 2023-07-26  4:49 UTC (permalink / raw)


Hi;

  Trying to understand (and use) a Rosetta Code task (Proper divisors)...

https://rosettacode.org/wiki/Proper_divisors#Ada

  it fails to compile

gnatmake -vh ./proper_divisors.adb

GNATMAKE 13.1.0
Copyright (C) 1992-2023, Free Software Foundation, Inc.
  "proper_divisors.ali" being checked ...
  -> "proper_divisors.ali" missing.
gcc -c -I./ -I- ./proper_divisors.adb
generic_divisors.ads:11:08: error: (Ada 2005) cannot copy object of a limited type (RM-2005 6.5(5.5/2))
generic_divisors.ads:11:08: error: return by reference not permitted in Ada 2005
End of compilation
gnatmake: "./proper_divisors.adb" compilation error

Why does this work for the the submitter of the Rosetta Code task and not for me?

Thanks,
Ken Wolcott

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

* Re: Rosetta Code task Proper divisors fails to compile
  2023-07-26  4:49 Rosetta Code task Proper divisors fails to compile Kenneth Wolcott
@ 2023-07-26  8:36 ` Jeffrey R.Carter
  2023-07-26 18:30   ` Kenneth Wolcott
  0 siblings, 1 reply; 5+ messages in thread
From: Jeffrey R.Carter @ 2023-07-26  8:36 UTC (permalink / raw)


On 2023-07-26 06:49, Kenneth Wolcott wrote:
> 
> https://rosettacode.org/wiki/Proper_divisors#Ada
> 
> gnatmake -vh ./proper_divisors.adb
> 
> generic_divisors.ads:11:08: error: (Ada 2005) cannot copy object of a limited type (RM-2005 6.5(5.5/2))
> generic_divisors.ads:11:08: error: return by reference not permitted in Ada 2005
> 
> Why does this work for the the submitter of the Rosetta Code task and not for me?

For some reason your gnatmake seems to be defaulting to -gnat05 mode. This code 
has an expression function, which is Ada 12, so try adding -gnat12 to the command.

You also should not need to put "./" in front of the file name, though I don't 
see how that would make a difference.

-- 
Jeff Carter
"I'm a lumberjack and I'm OK."
Monty Python's Flying Circus
54

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

* Re: Rosetta Code task Proper divisors fails to compile
  2023-07-26  8:36 ` Jeffrey R.Carter
@ 2023-07-26 18:30   ` Kenneth Wolcott
  2023-07-26 19:57     ` Jeffrey R.Carter
  0 siblings, 1 reply; 5+ messages in thread
From: Kenneth Wolcott @ 2023-07-26 18:30 UTC (permalink / raw)


On Wednesday, July 26, 2023 at 1:36:15 AM UTC-7, Jeffrey R.Carter wrote:
> For some reason your gnatmake seems to be defaulting to -gnat05 mode. This code 
> has an expression function, which is Ada 12, so try adding -gnat12 to the command. 
> 
> You also should not need to put "./" in front of the file name, though I don't 
> see how that would make a difference. 

Hi Jeff;

  Thank you for your suggestion.  Doesn't seem to have any effect.

(*SIGH*)

gnatmake -vh -gnat2012 proper_divisors.adb

GNATMAKE 13.1.0
Copyright (C) 1992-2023, Free Software Foundation, Inc.
  "proper_divisors.ali" being checked ...
  -> "proper_divisors.ali" missing.
gcc -c -gnat2012 proper_divisors.adb
generic_divisors.ads:11:08: error: (Ada 2005) cannot copy object of a limited type (RM-2005 6.5(5.5/2))
generic_divisors.ads:11:08: error: return by reference not permitted in Ada 2005
End of compilation
gnatmake: "proper_divisors.adb" compilation error

Ken

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

* Re: Rosetta Code task Proper divisors fails to compile
  2023-07-26 18:30   ` Kenneth Wolcott
@ 2023-07-26 19:57     ` Jeffrey R.Carter
  2023-07-26 22:26       ` Kenneth Wolcott
  0 siblings, 1 reply; 5+ messages in thread
From: Jeffrey R.Carter @ 2023-07-26 19:57 UTC (permalink / raw)


On 2023-07-26 20:30, Kenneth Wolcott wrote:
> 
>    Thank you for your suggestion.  Doesn't seem to have any effect.

Interesting. I get the same results with GNAT 12.

Looking more closely at the code, I think the error, while its msg is 
misleading, is correct. A function that returns a limited type can only return 
an aggregate, a function call, or an object declared by an extended return 
statement. The generic formal object None is none of these.

Changing the generic parameter to a function

    with function None return Result_Type;

makes the code correct. You need to change the definition of Empty in 
Proper_Divisors

       function Empty return Pos_Arr is (1 .. 0 => <>);

and create a function to supply for the 2nd instantiation

       function None return Natural is (0);
...
       package Divisor_Count is new Generic_Divisors
	(Result_Type => Natural, None => None, One => Cnt, Add =>  "+");

and then it compiles and runs.

Another possibility is to make Result_Type simply private, though that is 
slightly less general. That may be how the OP got the code to compile and run. 
It might be that limited was added later because the OP saw that it could be, 
and never tested the change.

-- 
Jeff Carter
"I'm a lumberjack and I'm OK."
Monty Python's Flying Circus
54

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

* Re: Rosetta Code task Proper divisors fails to compile
  2023-07-26 19:57     ` Jeffrey R.Carter
@ 2023-07-26 22:26       ` Kenneth Wolcott
  0 siblings, 0 replies; 5+ messages in thread
From: Kenneth Wolcott @ 2023-07-26 22:26 UTC (permalink / raw)


On Wednesday, July 26, 2023 at 12:57:40 PM UTC-7, Jeffrey R.Carter wrote:
> On 2023-07-26 20:30, Kenneth Wolcott wrote: 
> > 
> > Thank you for your suggestion. Doesn't seem to have any effect.
> Interesting. I get the same results with GNAT 12. 
> 
> Looking more closely at the code, I think the error, while its msg is 
> misleading, is correct. A function that returns a limited type can only return 
> an aggregate, a function call, or an object declared by an extended return 
> statement. The generic formal object None is none of these. 
> 
> Changing the generic parameter to a function 
> 
> with function None return Result_Type; 
> 
> makes the code correct. You need to change the definition of Empty in 
> Proper_Divisors 
> 
> function Empty return Pos_Arr is (1 .. 0 => <>); 
> 
> and create a function to supply for the 2nd instantiation 
> 
> function None return Natural is (0); 
> ... 
> package Divisor_Count is new Generic_Divisors 
> (Result_Type => Natural, None => None, One => Cnt, Add => "+"); 
> 
> and then it compiles and runs. 
> 
> Another possibility is to make Result_Type simply private, though that is 
> slightly less general. That may be how the OP got the code to compile and run. 
> It might be that limited was added later because the OP saw that it could be, 
> and never tested the change.

Hi Jeff;

  Thank you for your reply.

  I went with the latter suggestion for my local use.

Ken

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

end of thread, other threads:[~2023-07-26 22:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-26  4:49 Rosetta Code task Proper divisors fails to compile Kenneth Wolcott
2023-07-26  8:36 ` Jeffrey R.Carter
2023-07-26 18:30   ` Kenneth Wolcott
2023-07-26 19:57     ` Jeffrey R.Carter
2023-07-26 22:26       ` Kenneth Wolcott

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