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.
{$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.