Recent

Author Topic: [SOLVED] Operators overriding and use of PChars in code for AVRs  (Read 2835 times)

TRon

  • Hero Member
  • *****
  • Posts: 3956
Re: Operators overriding and use of PChars in code for AVRs
« Reply #30 on: January 16, 2025, 07:37:24 pm »
A more explanatory read then: https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Type_Compatibility_and_Identity_(Delphi)

... and then you'll run into a tiny small issue with the compiler... type aliasing behaviour is described differently (and imho is actually a bug, reported as such as well).
I do not have to remember anything anymore thanks to total-recall.

TRon

  • Hero Member
  • *****
  • Posts: 3956
Re: Operators overriding and use of PChars in code for AVRs
« Reply #31 on: January 16, 2025, 07:44:48 pm »
OK got it  .. so RTFM error  :-[ and apologies  :(

Sorry for the noise ...
No need to be sorry/make apologies.

What I do expect is at least a little incentive. Asking for examples on a self defined type is rather pointless because you would need to decide how to implement it. It can be done in multiple different ways and what way is the right one depends solely on the person implementing and that combined with current implementation and (future) wishes.

If someone has never worked with a particular construct then it would be beneficial to at least have a read through the documentation so that it is possible to ask specific questions in relation to the particular use case.
I do not have to remember anything anymore thanks to total-recall.

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #32 on: January 16, 2025, 07:46:27 pm »
A more explanatory read then: https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Type_Compatibility_and_Identity_(Delphi)

... and then you'll run into a tiny small issue with the compiler... type aliasing behaviour is described differently (and imho is actually a bug, reported as such as well).

@Tron, thanks for link. If I understand correctly, this code should work with operators overriding, but it doesn't:

Code: Pascal  [Select][+][-]
  1. type
  2. //  TFloat32=array[0..3] of UInt8;// UInt32;
  3.   TFloat32=type UInt32;
  4.  

d.ioannidis

  • Full Member
  • ***
  • Posts: 229
    • Nephelae
Re: Operators overriding and use of PChars in code for AVRs
« Reply #33 on: January 16, 2025, 07:54:57 pm »
Hi,

<snip>
If someone has never worked with a particular construct then it would be beneficial to at least have a read through the documentation so that it is possible to ask specific questions in relation to the particular use case.

I always read the documentation, but this time I read it too quick and I missed that part. Hence the acknowledgement of a RTFM error from my side.

regards,

TRon

  • Hero Member
  • *****
  • Posts: 3956
Re: Operators overriding and use of PChars in code for AVRs
« Reply #34 on: January 16, 2025, 07:55:31 pm »
@Tron, thanks for link. If I understand correctly, this code should work with operators overriding, but it doesn't:
You understood correctly. I have no idea why defining your own type currently does not work. As said, it is reported so we'll have to wait and see what happens.

For the time being (yes, inconvenient):
Code: Pascal  [Select][+][-]
  1. TFloat32 =
  2. record
  3.   value: UInt32;
  4. end;
  5.  
... and then see also record operators.

I always read the documentation, but this time I read it too quick and I missed that part. Hence the acknowledgement of a RTFM error from my side.
Ah, ok my bad. All good and no problem. Indirectly we did tell to read the manual but at least I was not aware you already did.
« Last Edit: January 16, 2025, 07:59:48 pm by TRon »
I do not have to remember anything anymore thanks to total-recall.

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #35 on: January 16, 2025, 08:03:03 pm »
(...)
What I do expect is at least a little incentive. Asking for examples on a self defined type is rather pointless because you would need to decide how to implement it. It can be done in multiple different ways and what way is the right one depends solely on the person implementing and that combined with current implementation and (future) wishes.

If someone has never worked with a particular construct then it would be beneficial to at least have a read through the documentation so that it is possible to ask specific questions in relation to the particular use case.
@Tron, It really was a question about operators overriding of simple UInt32 type. TFloat32 is only my alias name. I am a bit surprised that operators does not work even redefining it as
Code: Pascal  [Select][+][-]
  1. type
  2.   TFloat32=type UInt32;  
  3.  

Thaddy

  • Hero Member
  • *****
  • Posts: 16523
  • Kallstadt seems a good place to evict Trump to.
Re: Operators overriding and use of PChars in code for AVRs
« Reply #36 on: January 16, 2025, 08:08:07 pm »
Now I am completely lost....
What exactly, precisely do you mean?
All proper answers are given...... >:D
There is something in your head that doesn't belong there.
But I am sure they don't want the Trumps back...

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #37 on: January 16, 2025, 08:10:29 pm »
Now I am completely lost....
What exactly, precisely do you mean?
All proper answers are given...... >:D
There is something in your head that doesn't belong there.
Please provide more constructive comments.

Thaddy

  • Hero Member
  • *****
  • Posts: 16523
  • Kallstadt seems a good place to evict Trump to.
Re: Operators overriding and use of PChars in code for AVRs
« Reply #38 on: January 16, 2025, 08:12:39 pm »
Well,I already did, like a few more members. You play to be stupid on purpose?
 >:( >:( >:( >:( >:(
I will exit this discussion, because I hate the pointless/clueless.
But I am sure they don't want the Trumps back...

TRon

  • Hero Member
  • *****
  • Posts: 3956
Re: Operators overriding and use of PChars in code for AVRs
« Reply #39 on: January 16, 2025, 08:14:44 pm »
@Tron, It really was a question about operators overriding of simple UInt32 type. TFloat32 is only my alias name. I am a bit surprised that operators does not work even redefining it as
I more or less agree with you but the devil is probably in the details on that same type compatibility page. imho some of the things written there can be interpreted ambiguously.
I do not have to remember anything anymore thanks to total-recall.

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #40 on: January 16, 2025, 08:21:49 pm »
@Tron, It really was a question about operators overriding of simple UInt32 type. TFloat32 is only my alias name. I am a bit surprised that operators does not work even redefining it as
I more or less agree with you but the devil is probably in the details on that same type compatibility page. imho some of the things written there can be interpreted ambiguously.

@Tron, thanks again for your comment. I think I'll stop trying to override operators for now, because constructions based on arrays or records require a lot of redesigning of the code that's already written.

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #41 on: January 16, 2025, 08:28:13 pm »
Well,I already did, like a few more members. You play to be stupid on purpose?
 >:( >:( >:( >:( >:(
I will exit this discussion, because I hate the pointless/clueless.
@Thaddy I mean your emotional statements. All your substantive comments I respect very much!

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: [SOLVED] Operators overriding and use of PChars in code for AVRs
« Reply #42 on: January 16, 2025, 09:32:13 pm »
I think this is one of possible ways to override operators for my simple type TFloat32=UInt32:
Code: Pascal  [Select][+][-]
  1. program Operators;
  2.  
  3. {$mode objfpc}
  4.  
  5. type
  6.   TFloat32=array[0..3] of UInt8; // = UInt32 in size;
  7.  
  8. operator * (const f1, f2: TFloat32): TFloat32;
  9. var
  10.   if1: UInt32 absolute f1;
  11.   if2: UInt32 absolute f2;
  12.   ir: UInt32 absolute result;
  13. begin
  14.   ir:=if1*if2; //or whatever, only for test
  15. end;
  16.  
  17. var
  18.   f1, f2, f3: TFloat32;
  19. begin
  20.   f3:=f1 * f2; //only test
  21. end.
  22.  
« Last Edit: January 16, 2025, 09:34:24 pm by ackarwow »

TRon

  • Hero Member
  • *****
  • Posts: 3956
Re: [SOLVED] Operators overriding and use of PChars in code for AVRs
« Reply #43 on: January 16, 2025, 09:51:56 pm »
I think this is one of possible ways to override operators for my simple type TFloat32=UInt32:
Sure, in case that works for you then go ahead.

But... I do not see the benefit over:
Code: Pascal  [Select][+][-]
  1. type
  2.   TFloat32 = record
  3.     value: uint32;
  4.   end;
  5.  
  6. {
  7.   or in case array is required:
  8.   TFloat32 = record
  9.     case byte of
  10.     0 : (value: uint32);
  11.     1 : (bytes: packed array[0..3] of byte);
  12.   end;
  13.  e.g. best of 2 worlds :-)
  14. }
  15.  
  16.  
  17. operator * (const f1, f2: TFloat32): TFloat32;
  18. begin
  19.   result.value := f1.value * f2.value;
  20. end;
  21.  
  22. var
  23.   f1, f2, f3: TFloat32;
  24. begin
  25.   f3:=f1 * f2; //only test
  26. end.
  27.  
To me that looks cleaner and and less laborious.
« Last Edit: January 16, 2025, 10:02:46 pm by TRon »
I do not have to remember anything anymore thanks to total-recall.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5858
  • Compiler Developer
Re: [SOLVED] Operators overriding and use of PChars in code for AVRs
« Reply #44 on: January 16, 2025, 09:57:37 pm »
1. Is it possible to override operators such as '+', '-', '/', '*' using dedicated functions? As mentioned elsewhere we (me and @Dzandaa) are working on a software implementation of floating point numbers (as type TFloat32=UInt32). In this case using functions as equivalent for operators (for example Float32Mul for multiplication) is not very comfortable, simple operators could be much easier...

Operator overloading is independent of the used platform. However you can only overload operators that are not already defined internally by the compiler. These operators are also defined when you declare a unique type alias (using type TypeName), because after all a unique type alias of e.g. LongInt is still a LongInt.

2. What is your opinion about PChars (=arrays[0..n] of char) in code for AVRs? Is this feature fully supported by the compiler? In the wiki page https://wiki.freepascal.org/AVR_Programming there is no mention about PChars, but I think it was written at  one point that PChars work. If yes - what is better to use in practice: strings[n] or arrays[0..n] of char?

Please note that PChar is not a array[0..n] of Char, but ^Char. So while you can pass a pointer to the first element of an array[0..n] of Char to a PChar it can also point to some memory. The important point is that it needs to end with a #0 as that is used to determine the length.

 

TinyPortal © 2005-2018