Discussion:
variable type cast
Jay Michael
2013-05-12 23:12:45 UTC
Permalink
gpc version 20070904, based on gcc-3.4.5 (mingw special)
?
???? Given
????????? type?INT16? = INTEGER? attribute( size=16 ) ;
????????? type WORD16 = Cardinal attribute( size=16 ) ;
????????? var?i16 : INT16 ;
????????? var w16 : WORD16 ;
why does
????????? WORD16(I16) := W16 ;
produce "error: invalid lvalue in assignment"?

?
?????????
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.g-n-u.de/pipermail/gpc/attachments/20130512/ba3f9d8f/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: jsm5.pas
URL: <http://www.g-n-u.de/pipermail/gpc/attachments/20130512/ba3f9d8f/attachment.ksh>
Prof A Olowofoyeku (The African Chief)
2013-05-13 15:23:45 UTC
Permalink
Post by Jay Michael
gpc version 20070904, based on gcc-3.4.5 (mingw special)
?
???? Given
????????? type?INT16? = INTEGER? attribute( size=16 ) ;
????????? type WORD16 = Cardinal attribute( size=16 ) ;
????????? var?i16 : INT16 ;
????????? var w16 : WORD16 ;
why does
????????? WORD16(I16) := W16 ;
produce "error: invalid lvalue in assignment"?
?????????
int16 and word16 have different ranges, and that kind of type-casting
will not circumvent the fact that the compiler should stop you from
doing things like that unless you specifically instruct it to NOT do
so.

"i16 := Word16(w16)" should compile, as should "i16 := w16".
Preferably, all should be avoided, because, if the value of "w16" is
higher than the int16 range, the program may simply die with a runtime
error (at best), or, worse, you end up with an undefined value in
"i16", which may then lead to unpredictable behaviour in your program.

Imagine this;
w16 := 45000;
i16 := w16;
This should compile, but the assignment will generate a run-time error.

If you turn off range checking;
w16 := 45000;
{$R-}
i16 := w16;
{$R+}
then you will not get a runtime error, but what would the value of
"i16" be after the assignment? Would it be what you are expecting?

Best regards, The Chief
--------
Prof. Abimbola A. Olowofoyeku (The African Chief)
web: http://www.greatchief.plus.com/
Maurice Lombardi
2013-05-13 16:38:02 UTC
Permalink
-------- Message original --------
Sujet: Re: variable type cast
Date : Mon, 13 May 2013 09:11:11 +0200
De : Maurice Lombardi <Maurice.Lombardi at ujf-grenoble.fr>
Pour : Jay Michael <jmichael_ll at yahoo.com>
Post by Jay Michael
gpc version 20070904, based on gcc-3.4.5 (mingw special)
Given
type INT16 = INTEGER attribute( size=16 ) ;
type WORD16 = Cardinal attribute( size=16 ) ;
var i16 : INT16 ;
var w16 : WORD16 ;
why does
WORD16(I16) := W16 ;
produce "error: invalid lvalue in assignment"?
First notice tht the other way around

I16 := INT16(W16) ;

works.

Typecasts are C style unportable "extensions" which garble the
"write once, compile everywhere" philosophy behind the strong typing
principles of Pascal.
There is certainly nothing about it in the standards, and I do not know
at which point this "lvalue error" crept in first into C, and then into
Pascal.

Maurice
--
Maurice Lombardi
Laboratoire Interdisciplinaire de Physique,
(ex Spectrometrie Physique)
Universite Joseph Fourier de Grenoble, BP87
38402 Saint Martin d'Heres Cedex FRANCE
Tel: 33 (0)4 76 51 47 51
Fax: 33 (0)4 76 63 54 95
mailto:Maurice.Lombardi at ujf-grenoble.fr
Tom Verhoeff
2013-05-13 19:06:40 UTC
Permalink
Post by Jay Michael
gpc version 20070904, based on gcc-3.4.5 (mingw special)
?
???? Given
????????? type?INT16? = INTEGER? attribute( size=16 ) ;
????????? type WORD16 = Cardinal attribute( size=16 ) ;
????????? var?i16 : INT16 ;
????????? var w16 : WORD16 ;
why does
????????? WORD16(I16) := W16 ;
produce "error: invalid lvalue in assignment"?
I would think that the reason is that to the left of the assignment
operator you need a variable or equivalent, in C jargon also known
as an lvalue (i.e. something that has a persistent memory address).

Note that a variable by itself is both an lvalue and an expression (an
rvalue, though this term is little used), but not every expression is
an lvalue (e.g. v + 1 is not an lvalue; you cannot assign anything to it).

In particular, casting is an operator that yields an rvalue, but not
an lvalue. Casting needs to done to the right of the assignment operator.

(The previous warnings about casting still stand.)

Tom
--
E-MAIL: T.Verhoeff (a) TUE.NL | Dept. of Math. & Comp. Science
PHONE: +31 40 247 41 25 | Eindhoven University of Technology
FAX: +31 40 247 54 04 | PO Box 513, NL-5600 MB Eindhoven
http://www.win.tue.nl/~wstomv/ | The Netherlands
Adriaan van Os
2013-05-15 07:41:04 UTC
Permalink
Post by Tom Verhoeff
In particular, casting is an operator that yields an rvalue, but not
an lvalue. Casting needs to done to the right of the assignment operator.
Not really. A casting at the right of the assignment operator is called a value type cast, a
casting at the left of the assignment operator is called a variable type cast. The rules for both
are a bit different, e.g. a value type cast may allow a cast to a different size (e.g. to a
different size integer), a variable type cast is more strict.

Regards,

Adriaan van Os
Adriaan van Os
2013-05-14 08:58:59 UTC
Permalink
Post by Jay Michael
gpc version 20070904, based on gcc-3.4.5 (mingw special)
Given
type INT16 = INTEGER attribute( size=16 ) ;
type WORD16 = Cardinal attribute( size=16 ) ;
var i16 : INT16 ;
var w16 : WORD16 ;
why does
WORD16(I16) := W16 ;
produce "error: invalid lvalue in assignment"?
A bug in the compiler. GPC is too picky when it comes to typecasts.

Regards,

Adriaan van Os
Jay Michael
2013-05-17 00:36:36 UTC
Permalink
Post by Adriaan van Os
Post by Jay Michael
gpc version 20070904, based on gcc-3.4.5 (mingw special)
? ? ???Given
? ? ? ? ???type INT16? = INTEGER? attribute( size=16 ) ;
? ? ? ? ???type WORD16 = Cardinal attribute( size=16 ) ;
? ? ? ? ???var i16 : INT16 ;
? ? ? ? ???var w16 : WORD16 ;
why does
? ? ? ? ???WORD16(I16) := W16 ;
produce "error: invalid lvalue in assignment"?
A bug in the compiler. GPC is too picky when it comes to typecasts.
? ???Do you have any idea why this case is rejected?

? ???The compiler accepted

? ? ? ? ? type winbool = boolean attribute( size = 32 ) ;
? ? ? ? ? var i : integer ;
? ? ? ? ? var wb : winbool ;

? ? ? ? ? integer(wb) := 1024 ;
? ? ? ? ? integer(wb) := -1 ;
? ? ? ? ? integer(wb) := i ;

I thought BOOLEAN was an enumeration type.? I thought an enumeration type was a second cousin to an unsigned integer.? So I thought the only real difference in my case was that I was using 16-bit integers.

? ???I was prepared for the compiler to accept the code and insist on generating range checks that I don't want.? I'm just puzzled that it wouldn't even accept what looks to me to be just like examples I see posted of "variable type cast".
Continue reading on narkive:
Loading...