Forum > General

Please explain how this operator overloading works.

(1/5) > >>

Solecist:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode objfpc}{$asmmode intel}Program Nibble;Type        TNibble = Record _Nibble : Byte; End;Var        q : TNibble; Operator := (src : Byte) dst : TNibble;Begin        // writeln('??');        if src > 15 then src := src mod 16;End; //Operator := (src : TNibble) dst : Char;//Begin        // writeln('!!');//      dst := chr(src._Nibble);//End;  Begin        q := 1;        writeln(q._Nibble);        q := 23;        writeln(q._Nibble);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.

TRon:

--- Quote from: JustAQuestion on February 02, 2023, 08:13:57 am ---I feel like this should not work at all, but it does.

--- End quote ---
pure luck.


--- Quote ---Can someone explain why this works?

--- End quote ---
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" ?

--- End quote ---
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

Zvoni:

--- Quote from: JustAQuestion on February 02, 2023, 08:13:57 am ---
As is, the output is 1 and 7.
With the '??', the first result is seemingly random, the second is 7.
Untested

--- End quote ---
Your first call (q:=1) has no Result set in the Operator
Untested

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Operator := (src : Byte) : TNibble;Begin        // writeln('??');        if src > 15 then Result._Nibble:= src mod 16 Else Result._Nibble:=42;End;

Thaddy:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses   sysutils;var   b:Byte;begin  b.Nibbles[0] := 1;  b.Nibbles[1] := 1;  writeln(b);  //17end.
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++)

MarkMLl:

--- Quote from: Thaddy 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.

--- End quote ---

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

Navigation

[0] Message Index

[#] Next page

Go to full version