Recent

Author Topic: Please explain how this operator overloading works.  (Read 1716 times)

Solecist

  • Guest
Please explain how this operator overloading works.
« on: February 02, 2023, 08:13:57 am »
This really works and I'm not sure if it should. I am having a really hard time getting operators to overload, getting constant errors about how they can not be overloaded. At some point of random attempts I've found this, but I do not see why it works.

What is also interesting is that, when I remove the // from the first writeln, I get a random result at first, but the second result is correct anyway.

Edit: Actually I've realized that the second overload is not even required. I'll comment it out.

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$asmmode intel}
  3. Program Nibble;
  4. Type
  5.         TNibble = Record _Nibble : Byte; End;
  6. Var
  7.         q : TNibble;
  8.  
  9. Operator := (src : Byte) dst : TNibble;
  10. Begin
  11.         // writeln('??');
  12.         if src > 15 then src := src mod 16;
  13. End;
  14.  
  15. //Operator := (src : TNibble) dst : Char;
  16. //Begin
  17.         // writeln('!!');
  18. //      dst := chr(src._Nibble);
  19. //End;
  20.  
  21.  
  22. Begin
  23.         q := 1;
  24.         writeln(q._Nibble);
  25.         q := 23;
  26.         writeln(q._Nibble);
  27. End.

As is, the output is 1 and 7.
With the '??', the first result is seemingly random, the second is 7.
The '!!' does not change the result at all.

I feel like this should not work at all, but it does.

Can someone explain why this works?
Also, is there a way for me to get rid of the "_Nibble" ?

Thank you.
« Last Edit: February 02, 2023, 08:44:45 am by JustAQuestion »

TRon

  • Hero Member
  • *****
  • Posts: 2515
Re: Please explain how this operator overloading works.
« Reply #1 on: February 02, 2023, 08:49:25 am »
I feel like this should not work at all, but it does.
pure luck.

Quote
Can someone explain why this works?
There is no gain in explaining undefined compiler behaviour. It is undefined *period*

Quote
Also, is there a way for me to get rid of the "_Nibble" ?
Don't make it a record so that you do not need a fieldname ?

fwiw: records should use record operators: https://www.freepascal.org/docs-html/ref/refse63.html#x122-1460009.3
« Last Edit: February 02, 2023, 08:53:42 am by TRon »

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Please explain how this operator overloading works.
« Reply #2 on: February 02, 2023, 09:16:18 am »

As is, the output is 1 and 7.
With the '??', the first result is seemingly random, the second is 7.
Untested
Your first call (q:=1) has no Result set in the Operator
Untested
Code: Pascal  [Select][+][-]
  1. Operator := (src : Byte) : TNibble;
  2. Begin
  3.         // writeln('??');
  4.         if src > 15 then Result._Nibble:= src mod 16 Else Result._Nibble:=42;
  5. End;
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Please explain how this operator overloading works.
« Reply #3 on: February 02, 2023, 09:20:10 am »
First of all, nibbles are theoretically defined as half of any integer type, signed or not and including bytes and shortint.
Only a nibble of byte has size 4. BUT
Second, the type helpers in sysutils already contain nibble support defined as 4 bits for everything, which is technically incorrect, but what most people expect, e.g.
Code: Pascal  [Select][+][-]
  1. uses
  2.   sysutils;
  3. var
  4.   b:Byte;
  5. begin
  6.   b.Nibbles[0] := 1;
  7.   b.Nibbles[1] := 1;
  8.   writeln(b);  //17
  9. end.

Your code works because it is correctly implemented for your own nibble type, but quite frankly not necessary because of the bit and nibble implementations in sysutils. (see sourcecode in syshelph.inc )
That code is partially written by Avra and partially written by me.

Plz excuse us for having the theory wrong  ;D but many other languages make the same mistake if nibble is implemented. (C#, C++)
« Last Edit: February 02, 2023, 09:37:22 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Please explain how this operator overloading works.
« Reply #4 on: February 02, 2023, 09:38:03 am »
First of all, nibbles are theoretically defined as half of any integer type, signed or not and including bytes and shortint.

I have never come across any usage of the term other than to identify a half-byte, but I would suggest that pushing the point wouldn't contribute to OP's understanding.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Please explain how this operator overloading works.
« Reply #5 on: February 02, 2023, 09:45:47 am »
That is why we implemented it like you expect! But it is not correct.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Please explain how this operator overloading works.
« Reply #6 on: February 02, 2023, 09:47:17 am »
And my answer was more to his Question, why q:=1 results in a random value
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Please explain how this operator overloading works.
« Reply #7 on: February 02, 2023, 09:56:36 am »
Ah, just replace mod with a shr 4 will do. Probably sar for signed. Not tested.
« Last Edit: February 02, 2023, 09:58:32 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Please explain how this operator overloading works.
« Reply #8 on: February 02, 2023, 10:02:45 am »
@MarkMLI
Nibbles in computer science can be either 4 bits or half the size of any integer signed or not, depending on the context. As an example, in the Intel x86 architecture, nibbles are defined as 4 bits, while in the ARM architecture, nibbles are defined as half the size of any integer signed or not.

References:

1. Intel® 64 and IA-32 Architectures Software Developer's Manual Volume 1: Basic Architecture, Intel, 2017.
2. ARM Architecture Reference Manual, ARM, 2012.
3. The term itself was introduced in 1965 by John G. Kemeny in 1965, who was wrong because it already existed under different names all the way back to Boole himself.

Note that 2. is theoretically correct and how I learned it many moons ago. 1. is just how it is now defined in practice, but an error. I have implemented an intentional error in sysutils...
Mea Culpa.  :'(

Anyway: as long as there is a definition for a purpose it is legal. That does not mean it is logically correct of course.
« Last Edit: February 02, 2023, 10:21:46 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Solecist

  • Guest
Re: Please explain how this operator overloading works.
« Reply #9 on: February 02, 2023, 10:18:48 am »
And my answer was more to his Question, why q:=1 results in a random value

No, you've misunderstood.


As is, the output is 1 and 7.
With the '??', the first result is seemingly random, the second is 7.
Untested
Your first call (q:=1) has no Result set in the Operator
Untested
Code: Pascal  [Select][+][-]
  1. Operator := (src : Byte) : TNibble;
  2. Begin
  3.         // writeln('??');
  4.         if src > 15 then Result._Nibble:= src mod 16 Else Result._Nibble:=42;
  5. End;

Quote
What is also interesting is that, when I remove the // from the first writeln, I get a random result at first, but the second result is correct anyway.

It worked without setting the result, which doesn't seem to make sense.

The *WriteLn* caused the random result.
When I comment it away, the number is correct.


Do you have an explanation? Generally it seems that this should not work at all, yet it does.
« Last Edit: February 02, 2023, 10:25:41 am by JustAQuestion »

TRon

  • Hero Member
  • *****
  • Posts: 2515
Re: Please explain how this operator overloading works.
« Reply #10 on: February 02, 2023, 10:21:26 am »
Do you have an explanation?

There is no gain in explaining undefined compiler behaviour. It is undefined *period*

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Please explain how this operator overloading works.
« Reply #11 on: February 02, 2023, 10:44:36 am »
Do you have an explanation?

There is no gain in explaining undefined compiler behaviour. It is undefined *period*
Since the example uses globals, they are initialized memory. PascalDragon explained last week that it even makes no difference if you change memory manager. (What I assumed to be the case) Which makes it defined behavior.
« Last Edit: February 02, 2023, 10:47:04 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

TRon

  • Hero Member
  • *****
  • Posts: 2515
Re: Please explain how this operator overloading works.
« Reply #12 on: February 02, 2023, 10:56:32 am »
It does not really matter much Thaddy.

The code (as presented) is faulty, thus leaving the compiler in a undefined state. Somehow TS expect to explain why it works /for TS/.

I get totally different results because I ran the code on a not so common platform (on purpose).

Compile the code with other settings (for example other optimalization) and again the output will differ from previous results.

If TS truly wishes to know then output some assembler and TS can explain him/herself why it (does not) work.

Ergo: no gain whatsoever to try and explain the generated assembler that is based on faulty code.

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Please explain how this operator overloading works.
« Reply #13 on: February 02, 2023, 11:16:58 am »
Nibbles in computer science can be either 4 bits or half the size of any integer signed or not, depending on the context. As an example, in the Intel x86 architecture, nibbles are defined as 4 bits, while in the ARM architecture, nibbles are defined as half the size of any integer signed or not.
"Nibble" is not a scientific definition, but just a colloquial term and as such subject to change (like other words as well, e.g. the word "pudding" previously meant sausage, but today means something completely different). Similar here today the term nibble is pretty universally used for describing a half-byte. This is for example the meaning described by Wikipedia: https://en.wikipedia.org/wiki/Nibble

Personally I have learned the term nibble as "half-byte" at university, and have seen it being used like this in academic literature. In comparison I never have seen it being refered to something else, at least in recent history.

That said, if a nibble is a half-byte, what a byte is is then the question of the underlying archtiecture, but is generally 8 bit
« Last Edit: February 02, 2023, 11:19:52 am by Warfley »

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Please explain how this operator overloading works.
« Reply #14 on: February 02, 2023, 11:47:32 am »

"Nibble" is not a scientific definition, but just a colloquial term
No, it is - with different wording . It is just that in CS it is defined often as half a byte (4 bits) which is wrong since Leibnitz , not Boole, and even I Ching many moons before that, defined what we now call Boolean arithmatic and both explained the importance of a half value..
A colloquial term it is indeed, but not founded in science. (Although the ARM explanation is.... Old world vs New World for that matter Colour vs Color or the potato song...) It is not even colloquial , because mainstream implementations differ.

(The older I get, the more I appreciate logic, anyway colloquial: I implemented it wrong! Although - almost - everybody else say I implemented it right! Oh well... This is just for the record. In Popper's speak I only falsified myself...)
« Last Edit: February 02, 2023, 12:04:46 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018