Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-01

Checking assignment compatibility

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mark Morgan Lloyd

Simulating a C-style conditional, I can do this

function tf(const q: qword; const qt, qf: TObject): TObject; inline;
begin
   Assert(TypeOf(qt) = TypeOf(result));

which- as I understand it- checks that the types are strictly the same. How do I relax this to only ensure that the parameter is assignment-compatible with the result? I presume that it's an is relationship but so far the syntax eludes me.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Michael Van Canneyt
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 6 lines
Simulating a C-style conditional, I can do this function tf(const q: qword; const qt, qf: TObject): TObject; inline; begin Assert(TypeOf(qt) = TypeOf(result));

This is always true. Everything descends from TOBject.

But assuming the result is not TObject, it would be

Assert((qt=Nil) or (qt.Inheritsfrom(TheResultClass))

Although that is still radically different from what the C style macro does.

Michael.

Mark Morgan Lloyd
to Michael Van Canneyt
Michael Van Canneyt wrote 14 lines
On Sun, 5 Jan 2014, Mark Morgan Lloyd wrote: > Simulating a C-style conditional, I can do this > > function tf(const q: qword; const qt, qf: TObject): TObject; inline; > > begin > Assert(TypeOf(qt) = TypeOf(result)); This is always true. Everything descends from TOBject. But assuming the result is not TObject, it would be Assert((qt=Nil) or (qt.Inheritsfrom(TheResultClass))

So again skipping the nil check for simplicity, this compiles but are the semantics right?

Assert(qt.ClassType.InheritsFrom(result.ClassType));
Although that is still radically different from what the C style macro does.

Yes, understood and being watched out for.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Michael Van Canneyt
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 20 lines
Michael Van Canneyt wrote: > On Sun, 5 Jan 2014, Mark Morgan Lloyd wrote: > >> Simulating a C-style conditional, I can do this >> >> function tf(const q: qword; const qt, qf: TObject): TObject; inline; >> >> begin >> Assert(TypeOf(qt) = TypeOf(result)); > > This is always true. Everything descends from TOBject. > > But assuming the result is not TObject, it would be > > Assert((qt=Nil) or (qt.Inheritsfrom(TheResultClass)) So again skipping the nil check for simplicity, this compiles but are the semantics right? Assert(qt.ClassType.InheritsFrom(result.ClassType));

The classtype is not needed.

The semantics are not right IMHO, but I'd have to see the original C stuff to be able to say with certainty.

Michael.

Jonas Maebe
to Michael Van Canneyt
Michael Van Canneyt wrote 19 lines
On Mon, 6 Jan 2014, Mark Morgan Lloyd wrote: > Michael Van Canneyt wrote: >> On Sun, 5 Jan 2014, Mark Morgan Lloyd wrote: >>> Simulating a C-style conditional, I can do this >>> function tf(const q: qword; const qt, qf: TObject): TObject; >>> inline; >>> begin >>> Assert(TypeOf(qt) = TypeOf(result)); >> This is always true. Everything descends from TOBject. >> But assuming the result is not TObject, it would be >> Assert((qt=Nil) or (qt.Inheritsfrom(TheResultClass)) > > So again skipping the nil check for simplicity, this compiles but > are the semantics right? > > Assert(qt.ClassType.InheritsFrom(result.ClassType)); The classtype is not needed.

The first one, at least.

The semantics are not right IMHO, but I'd have to see the original C stuff to be able to say with certainty.

Regardless of what the original C stuff was, the above is wrong because "result" is uninitialised. Inside the function you cannot know the type of whatever the result will be assigned to on the caller side. Maybe instead of "result" it should be "qf".

Jonas

Mark Morgan Lloyd
to Michael Van Canneyt: The classtype is not needed. The semantics are not right IMHO, but I'd have to see the…
Michael Van Canneyt wrote 27 lines
On Mon, 6 Jan 2014, Mark Morgan Lloyd wrote: > Michael Van Canneyt wrote: >> On Sun, 5 Jan 2014, Mark Morgan Lloyd wrote: >> >>> Simulating a C-style conditional, I can do this >>> >>> function tf(const q: qword; const qt, qf: TObject): TObject; inline; >>> >>> begin >>> Assert(TypeOf(qt) = TypeOf(result)); >> >> This is always true. Everything descends from TOBject. >> >> But assuming the result is not TObject, it would be >> >> Assert((qt=Nil) or (qt.Inheritsfrom(TheResultClass)) > > So again skipping the nil check for simplicity, this compiles but are > the semantics right? > > Assert(qt.ClassType.InheritsFrom(result.ClassType)); The classtype is not needed. The semantics are not right IMHO, but I'd have to see the original C stuff to be able to say with certainty.

It's not C, it's Javascript :-(

     this.P1 = (this.PB1L ? this.PB : this.PA);
     this.P2 = (this.PB1L ? this.PA : this.PB);

where PB1L is 0 or 1, PA and PB are objects representing emulated CPUs (one or more defined), P1 indicates the first (master) CPU and P2 indicates the second (slave). PB1L was- on the original system- a switch on the engineer's panel indicating which of the processors was to run as the master (i.e. it handled the MCP, all interrupts and so on) and which as the slave.

I thought it would be good practice to abstract the conditional operator to a function- noting the unavoidable problem that both parameters will be evaluated, which is not really an issue when they're simple object references- but it's beginning to look as though it would be far simpler to have a simple if statement inline.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Mark Morgan Lloyd
to Jonas Maebe: The first one, at least. Regardless of what the original C stuff was, the above is wrong…
Jonas Maebe wrote 21 lines
>>>> Assert(TypeOf(qt) = TypeOf(result)); >>> This is always true. Everything descends from TOBject. >>> But assuming the result is not TObject, it would be >>> Assert((qt=Nil) or (qt.Inheritsfrom(TheResultClass)) >> >> So again skipping the nil check for simplicity, this compiles but are >> the semantics right? >> >> Assert(qt.ClassType.InheritsFrom(result.ClassType)); > > The classtype is not needed. The first one, at least. > The semantics are not right IMHO, but I'd have to see the original C > stuff to be able to say with certainty. Regardless of what the original C stuff was, the above is wrong because "result" is uninitialised. Inside the function you cannot know the type of whatever the result will be assigned to on the caller side. Maybe instead of "result" it should be "qf".

I see. So I could possibly do it with a third parameter provided that this was initialised to a dummy object, but otherwise the only possible check is that the two source objects are of the same type. In practice it's far better done with an inline if statement.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]