* On functions and access types.
@ 2002-06-26 22:57 Caffeine Junky
2002-06-27 0:25 ` Robert A Duff
2002-06-27 1:29 ` SteveD
0 siblings, 2 replies; 5+ messages in thread
From: Caffeine Junky @ 2002-06-26 22:57 UTC (permalink / raw)
In a related post where I asked for help with Generics I posted this spec
file...
generic
Max : Positive;
type Item is private;
package genstack is
type Stack is limited private;
procedure Push(X : in Item; S : in out Stack);
function Pop(S: in Stack) return Item;
function Is_Empty(S : in Stack) return Boolean;
private
type Cell;
type Stack is access Cell;
type Cell is
record
Value: Item;
Next: Stack;
end record;
-- This package will be expanded to include other forms of stacks --
-- as time permits. --
end genstack;
Now, function Pop was written thusley...
function Pop(S : in Stack) return Item is
X : Item;
begin
X := S.Value
S := S.Next;
return X;
end Pop;
Now the compiler tells me that I cannot assign to an 'in' parameter.
However, as I understand it, a function can only take an 'in' parameter.
I can copy S to a stack which is local to the function, but results in
the function only returning the last item in the stack.
If I use a procedure, it works fine. But I run into problems whenever I
try to use similiar acess types into a function.
I'm definitely overlooking something here. Probably something obvious.
I'll continue to search through my book for an answer, but any pointers
or references would be appreciated.
Now, eventually I changed this to a procedure for the sake of expediancy.
But I'm continuing to work on the problem.
Thanks for your patience.
Staple
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: On functions and access types.
2002-06-26 22:57 On functions and access types Caffeine Junky
@ 2002-06-27 0:25 ` Robert A Duff
2002-06-27 2:33 ` Ted Dennison
2002-06-27 1:29 ` SteveD
1 sibling, 1 reply; 5+ messages in thread
From: Robert A Duff @ 2002-06-27 0:25 UTC (permalink / raw)
Caffeine Junky <nospam@hotmail.com> writes:
> Now the compiler tells me that I cannot assign to an 'in' parameter.
> However, as I understand it, a function can only take an 'in' parameter.
That's right. If you want a function to modify its argument,
you can use an access parameter, like "S: access Stack;",
and call it with Some_Stack'Access.
But in this case, it's better to use a procedure.
> Now, eventually I changed this to a procedure for the sake of expediancy.
That's the right solution.
- Bob
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: On functions and access types.
2002-06-27 0:25 ` Robert A Duff
@ 2002-06-27 2:33 ` Ted Dennison
0 siblings, 0 replies; 5+ messages in thread
From: Ted Dennison @ 2002-06-27 2:33 UTC (permalink / raw)
Robert A Duff wrote:
> Caffeine Junky <nospam@hotmail.com> writes:
>
>
>>Now the compiler tells me that I cannot assign to an 'in' parameter.
>>However, as I understand it, a function can only take an 'in' parameter.
>
> That's right. If you want a function to modify its argument,
> you can use an access parameter, like "S: access Stack;",
> and call it with Some_Stack'Access.
..or use a pointer. C also allows only "in" parameters to functions, and
passing pointers is the standard work-around there. (C++ allows
reference parameters though).
> But in this case, it's better to use a procedure.
It almost always is.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: On functions and access types.
2002-06-26 22:57 On functions and access types Caffeine Junky
2002-06-27 0:25 ` Robert A Duff
@ 2002-06-27 1:29 ` SteveD
1 sibling, 0 replies; 5+ messages in thread
From: SteveD @ 2002-06-27 1:29 UTC (permalink / raw)
"Caffeine Junky" <nospam@hotmail.com> wrote in message
news:eFrS8.4786$Uu2.750@sccrnsc03...
[snip]
> Now the compiler tells me that I cannot assign to an 'in' parameter.
> However, as I understand it, a function can only take an 'in' parameter.
> I can copy S to a stack which is local to the function, but results in
> the function only returning the last item in the stack.
...
> If I use a procedure, it works fine. But I run into problems whenever I
> try to use similiar acess types into a function.
>
> I'm definitely overlooking something here. Probably something obvious.
> I'll continue to search through my book for an answer, but any pointers
> or references would be appreciated.
The only thing you're overlooking is the philosophy that functions should
not change the state of the system. By modifying their parameters for
example.
The concept of not permitting "out" or "in out" parameters to functions has
been debated in this group many times.
>
> Now, eventually I changed this to a procedure for the sake of expediancy.
> But I'm continuing to work on the problem.
>
That's the right answer anyway.
SteveD
>
> Thanks for your patience.
>
> Staple
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: On functions and access types.
@ 2002-06-27 7:36 Grein, Christoph
0 siblings, 0 replies; 5+ messages in thread
From: Grein, Christoph @ 2002-06-27 7:36 UTC (permalink / raw)
> function Pop(S : in Stack) return Item is
> X : Item;
> begin
> X := S.Value
> S := S.Next;
> return X;
> end Pop;
>
> Now the compiler tells me that I cannot assign to an 'in' parameter.
Others have commented on 'in'. So if you change this into a procedure, watch out
for garbage you're going to produce:
S := S.Next;
Hint: Unchecked_Deallocation
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2002-06-27 7:36 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-06-26 22:57 On functions and access types Caffeine Junky
2002-06-27 0:25 ` Robert A Duff
2002-06-27 2:33 ` Ted Dennison
2002-06-27 1:29 ` SteveD
-- strict thread matches above, loose matches on Subject: below --
2002-06-27 7:36 Grein, Christoph
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox