The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

HP Prime - local variables retain their initial type
Message #1 Posted by BruceH on 9 Nov 2013, 6:15 p.m.

Local variables retain the type that is first assigned. For example:

EXPORT t2()
BEGIN
  LOCAL aa;
  aa := [1,2,3];
  print(aa);
  aa := 3;
  print(aa);
END;
returns
[1,2,3]
[[3]]
which is bound to catch people out.
      
Re: HP Prime - local variables retain their initial type
Message #2 Posted by Joseph Ec on 9 Nov 2013, 10:04 p.m.,
in response to message #1 by BruceH

Hi Bruce,

In page (512 foot print) 518 / 616 of user guide says:

"Although the system will allow you to store different types in the same local variable, this is poor programming practice and should be avoided."

Of course, thanks for your findings and it let us to remember to avoid as user guide indicates.

Best regards!!

joseph

            
Re: HP Prime - local variables retain their initial type
Message #3 Posted by David Hayden on 10 Nov 2013, 10:46 a.m.,
in response to message #2 by Joseph Ec

But it isn't storing a different type. It's converting the different type into the original type. I think the text is misleading. It should say that local variables maintain their type and if you try to store a different type into them, then the expression is converted into the local's type.

Dave

                  
Re: HP Prime - local variables retain their initial type
Message #4 Posted by Tim Wessman on 10 Nov 2013, 12:14 p.m.,
in response to message #3 by David Hayden

Indeed. As a side note, it works fine for me here....

TW

                  
Re: HP Prime - local variables retain their initial type
Message #5 Posted by Michael de Estrada on 10 Nov 2013, 12:42 p.m.,
in response to message #3 by David Hayden

Yes, that's exactly what is happening and it's consistent with practice in other programming languages. I used to do a lot of programming in FORTRAN, where you could explicitly declare the type of a variable. You could declare A to be Real and B to be Integer, such that if you stored 3 in A it became 3.00 and if you stored 3.14 in B it became 3. I agree that this practice in PPL is correct.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall