Do I get it right, that a construction like
record
Something: byte;
case Something of
1: (x,y: word);
2: (z: longword);
end;
Is impossible in FPC?Mailing lists / fpc-pascal / 2006-09
variant part of a record
24 messages · 6 people · –
- ППётр Косаревский 10
- JJonas Maebe 8
- VVinzent Hoefler 3
- MMarco van de Voort 1
- VVincent Snijders 1
- VVinzent Höfler 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Пётр Косаревский wrote 8 lines
You have to write
record
case Something: byte of
1: (x,y: word);
2: (z: longword);
end;Jonas
Do I get it right, that a construction like record Something: byte; case Something of 1:…
Пётр Косаревский wrote 8 lines
Yes, you do. :)
Nevertheless
some_type = record
case Something : byte of
1: (x,y: word);
2: (z: longword);
end;would be the correct Pascal syntax and - surprise, surprise - is accepted by fpc. ;)
Vinzent.
Show quoted text 7 lines
Thank you, Jonas and Vincent, but I was vague: what I want is blockread'ing, so I have to use
record
Something: byte;
case byte of
1: (x,y: word);
2: (z: longword);
end;Or use classes/properties (virtualization of structure: reading into memory, processing there, checking in run-time that data is accessed legally [if needed]). Should I? Is there less abstracted approach?
Пётр Косаревский wrote 15 lines
No, AFAIK you don't have to, it's the same, just a different syntax. Your "Something" byte discrimant will be there. Or where else do you think it is stored within the structure?
Probably you should used a "packed record", though. And it's in no way portable, of course.
Vinzent.
Vinzent Höfler wrote 9 lines
Correct.
Probably you should used a "packed record", though. And it's in no way portable, of course.
Also correct.
Jonas
Vinzent H?fler> Probably you should used a "packed record", though. And it's in no Vinzent H?fler> way portable, of course. Also correct. Jonas
I use
{$A-}
abc= record
something: byte;
case byte of
1: (a,b: byte);
2: (c: word);
end;
{$A+} // or even a: byte; rsrvd1: array[1..3] of byte; b: word; ...
{$IF sizeof(abc)1234}
{$FATAL Неверный размер записи для заголовка (sizeof(abc)1234).} // I think, there is no need to translate:)
{$ENDIF}because I read binaries generated by other programs. So it's mostly portable: if it was supposed to be really portable, I'd add endianness checking.
What am I thinking of is a construction like
xyz= object
raw: abc;
function GetA: byte;
function GetB: byte;
function GetC: word;
property something: byte READ raw.something;
property a: byte READ GetA;
property b: byte READ GetB;
property c: word READ GetC;
procedure GetData(...); // obtain data for the first time, writes to the 'raw'
end;
function xyz.GetA: byte;
begin if something=1 then GetA:= raw.a else Show_Fatal_Error_Message(Fatal_Error_ID); end;
function xyz.GetB: byte;
begin if something=1 then GetB:= raw.b else Show_Fatal_Error_Message(Fatal_Error_ID); end;
function xyz.GetC: word;
begin if something=2 then GetC:= raw.c else Show_Fatal_Error_Message(Fatal_Error_ID); end;But it's somewhat bulky (in the case of many types), so I hoped there is a language feature for my convenience (I don't know pascal very well and didn't understand corresponding documentation in the one way).
Пётр Косаревский wrote 17 lines
You should also realign the data to their native boundaries then. :) Some processors just don't like data being accessed the wrong way, so either you trust fpc to workaround for you (and this could be rather slow) or you accept that your program may raise SIGBUS or similar processor exceptions on certain architectures.
Show quoted text 10 lines
I don't know if it is possible to define a property accessing only a part of a structure, and I'm too lazy to check it, so maybe you have to use a function like "GetSomething" instead. But apart from that I don't understand your problem with the Pascal syntax of discrimants in a variant record. It would work precisely as you expect, even if the syntax is a little bit different than your expectation about it are.
But it's somewhat bulky (in the case of many types), so I hoped there is a language feature for my convenience (I don't know pascal very well and didn't understand corresponding documentation in the one way).
Yes, there is a language feature called object orientation. But still that means you would have to translate the binary structure into a class instance somehow.
Vinzent.
Yes, there is a language feature called object orientation. But still that means you would have to translate the binary structure into a class instance somehow. Vinzent.
The object I presented compiles well, but there is almost no gain in OO approach: I still have to nest structures and write lots of properties, inheritance does not help.
I'm vague: for the first time I hoped that when you access the variant part, if the "case" variable was named, program checks it run-time. Hoping that it was implemented this way, I asked about control over the relative place in the memory of that named variable. Oh, well, there are no run-time checks, so that's all...
Пётр Косаревский wrote 5 lines
It's on my todo list to implement such checks (when enabled by a command line switch, of course). It's not that easy as it may look at first sight though.
Jonas
Jonas Maebe wrote 10 lines
Maybe you should take a look at the GNAT source code, Ada Compiler's do that sort of checks since 1983. ;)
Vinzent.
Vinzent Hoefler wroteMaybe you should take a look at the GNAT source code, Ada Compiler's do that sort of checks since 1983. ;)
I doubt GNAT's symtable implementation is even remotely similar to FPC's symtable implementation...
Jonas
Maybe you should take a look at the GNAT source code, Ada Compiler's do that sort of checks since 1983. ;) Vinzent.
Uh. See also intel 432 processor (1981, smalltalk and ada were main languages for it later, if I get it right)...
It's on my todo list to implement such checks (when enabled by a command line switch, of course). It's not that easy as it may look at first sight though. Jonas
It's great news!
However, in the case of such checks, ability to explicitly place tag variable in the record structure or to place something after the variant part (assuming it has the size of the biggest branch) would be great too.
Implementing "else" seems very logical (does modula-2 perform this checks?).
I wish you all the best.
Пётр Косаревский wrote 11 lines
To place something after the variant part, simply use nested records:
type
tr1 = record
a, b: longint;
case d: byte of
1: (s: string);
2: (c: char);
end;
tr2 = record
l: longint;
r: tr1;
l2: longint;
end;Hacking the parser to support this directly is a waste of time imho (not to mention that it would break the code's compatibility with every other Pascal compiler out there afaik).
Implementing "else" seems very logical (does modula-2 perform this checks?).
That would be more easily doable than the above, though again it would break compatibility with other Pascal compilers afaik.
Jonas
That would be more easily doable than the above, though again it would break compatibility with other Pascal compilers afaik. Jonas
I thought so.
But FPC isn't very compatible as it is: if you don't care for compatibility much, Delphi won't compile a program. Of course, TP will fail too.
It may be active in ObjFPC mode only.
Hacking the parser to support this directly is a waste of time imho (not to mention that it would break the code's compatibility with every other Pascal compiler out there afaik).
More specifically, it adds feature and does not break backward compatibility. Otherwise, the same as above. As you saw, nested records and properties already solve the problem, but with bulky and not very internal albeit transparent methods (pun).
In any way, it's you (one of those), who exert influence upon the FPC, not me.
Пётр Косаревский wrote 8 lines
Yes, but we try to minimise that sort of stuff in general (at least I do).
It may be active in ObjFPC mode only.
If it were implemented, it would definitely be supported only in FPC and ObjFPC modes.
In any way, it's you (one of those), who exert influence upon the FPC, not me.
It's not because you don't pay me, or because you cannot demand anything from me or anyone else working on FPC, that you do not have influence on the project or even on what I do (both positively and negatively).
Jonas
Show quoted text 5 lines
If I get it right, I don't have the influence, mainly because I don't contribute. Are there other reasons?
Пётр Косаревский wrote> It's not because you don't pay me, or because you cannot demand > anything from me or anyone else working on FPC, that you do not have > influence on the project or even on what I do (both positively and > negatively).
That was a difficult sentence. I think I understood it after reading three or four times.
If I get it right, I don't have the influence, mainly because I don't contribute. Are there other reasons?
I *think* all he said was:
Even if you don't pay me, even if you cannot demand anything from me, you still can have an influence on the project or on what I do.
Vincent
Vincent Snijders wroteI *think* all he said was: Even if you don't pay me, even if you cannot demand anything from me, you still can have an influence on the project or on what I do.
Yes.
Jonas
About contributing: is it useful to comment some parts of documentation ("html with comments") with improvements, corrections or compilable examples?
Show quoted text 11 lines
Well, I think, that the part "It's not because you don't pay me ..., that you do not have influence on the project ..." means, that I don't have direct influence on the project (it's right: I did not submit a bug for much time, and the last thing I did was writing some list of problems with 2.0.4rc1). Of course, it's not a robot of Jonas, who writes to the maillist, so I have some indirect influence (at least by entertaining, boring or wasting time of someone; I hope, that Florian reads maillist only when someone tells him, that there is something important there).
Пётр Косаревский wroteWell, I think, that the part "It's not because you don't pay me ..., that you do not have influence on the project ..." means, that I don't have direct influence on the project
No. It was simply your "but I don't have anything to say anyway"-type sentence that annoyed me. Your whole attitude seems to be based around "I'll be ignored anyway", "you'll do what you want regardless of what I say anyway so sorry for saying anything" etc.
That's everything but motivating to even bother replying the next time you post something, since all one gets back is mails in which you basically say "but I'm just wasting your time and you don't really care about what I say anyway".
(at least by entertaining, boring or wasting time of someone; I hope, that Florian reads maillist only when someone tells him, that there is something important there).
And this is another fine example of the above.
Jonas
About contributing: is it useful to comment some parts of documentation ("html with comments") with improvements, corrections or compilable examples?
For example, would it be meaningful to add an example on the discussed matter with a comment stating, that there are no such checks tonow?
-----------------------------
Show quoted text 6 lines
Maybe it's better to reply only on the meaningful part? You may regard my moaning as a cookie or an origin.
To place something after the variant part, simply use nested records: type tr1 = record…
Show quoted text 5 lines
Actually,�I wished for this one time or another. Usually when translating c code, and having to fix all references to insert another level in the reference.
