-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assignment of a string constant to a dynamic array fails #69
Comments
I agree, that it looks strange.
I believe this is in accordance with the report, but right now won't explain. Let us read and find the explanation. |
Regarding the assignment, I believe, you can use COPY and Strings.Assign. |
checked: COPY works. |
Yes, I can use COPY, but the version of the Oberon report that introduced string assignment no longer has a COPY function, so using COPY is using a deprecated feature to work around a bug. Wirth's 2013 Oberon report states:
Notice that it does not say that the left hand side must be a variable of type ARRAY OF CHAR, just that it must be an ARRAY OF CHAR. In my example above, |
voc is Oberon-2 compiler. 2013's report is not applicable. |
so far what i found: 6.4.
means that pstr2^is not an 'array n of char'. 9.1
and Appendix A.
in our case it is not clear that pstr2^ can be treated as an array n of char. so I guess we have two questions
|
also checked how oo2c behaves.
|
to sum up: both versions of OP2 - from V4 and S3 systems prohibit this. |
It would be interesting to determine if this behaviour is intended. In my mind, the specification is ambiguous in this regard. Which version of the report are you using? In my day job, I'm actively involved in the standardization of another (very different) language, and I can tell you that just because N implementations with completely different codebases all agree, doesn't mean they are right. :-) |
@svorkoetter There is no ambiguity here, the term arrays doesn't include open arrays. Since the days of Modula-2 open arrays were meant to be accessed only element-wise. No assignment was permitted and this has been retained in Oberon. In the type system they are anonymous and incompatible with the rest of the world. Strings are a special case of structured literals that have been made compatible with arrays of characters (we are talking here about arrays, not open arrays) and open arrays only in case of formal parameters. Once the assignment is done, conventional rules apply. COPY() has been created to make life easier, but not specifically for open arrays. Once a literal string is assigned to an array v2, the v1 := v2 operation copies the whole source array v2 to the destination array v1 (assuming they have the same type), while COPY() stops at the null character (and it allows for string truncation). Oberon-07 and Component Pascal allow a bit more flexibility but be aware that this flexibility is susceptible of runtime errors. |
Stefan, |
Diego, COPY() implemented in Ofront/voc silently truncates the string literal to the size of the target string without any warning. In terms of reliability, this is very bad, because it distorts the data in a string. Which would you prefer? Distort data or issue a warning about the insufficient size of the target string as a runtime error? My choice is quite obvious. I remembered how I recently worked on a project being developed simultaneously in Oberon-2 and Component Pascal. The Oberon-2 version, of course, used COPY. There was a pointer to a small string that I, not knowing its actual size, tried to assign a literal that was larger than the size. And Oberon-2 with its COPY did not help me to find this problem - the string literal was truncated quietly, whereas Component Pascal showed me what's really going on. So the problem was detected instantly. This moment, in the end, determined my choice. |
@diegosardina Thanks, that makes sense. The last time I did much programming in Oberon was in 1990 or so, |
@Oleg-N-Cher I agree that a silent truncation is not the best idea. It's still better than C's |
My intention wasn't to say that something is better than another one, but to warn that an assignment to an open array is susceptible of runtime errors and it requires proper checks in the code (the same, of course, holds for COPY). However COPY causes truncation when two different (-> different length) arrays of characters are used, of course you should expected something like this, it's an utility to break the rules of the type system. Open arrays instead may be used without care. I agree that in general runtime checks are better than unexpected results due to a silent truncation. For this reason I gave much more relevance to the first. |
In the following, voc allows the assignment to
ptr
, but not the assignment toptr2
. I believe this to be a bug:The text was updated successfully, but these errors were encountered: