Recent

Author Topic: overloading operators in fpc?  (Read 4220 times)

alsor

  • New Member
  • *
  • Posts: 47
overloading operators in fpc?
« on: March 22, 2021, 06:49:11 pm »
Is it possible to define operators in the fpc, like in the c++?

for example:

object = xyz
x,y, z : double;

operator * (a,b)...

end;



then using something like this:
xyz a, b, c;

c = a + b ;

?



Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9869
  • Debugger - SynEdit - and more
    • wiki

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: overloading operators in fpc?
« Reply #2 on: March 22, 2021, 08:06:14 pm »
Is it possible to define operators in the fpc, like in the c++?

I take it you did not like C++?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: overloading operators in fpc?
« Reply #3 on: March 23, 2021, 09:45:23 am »
https://www.freepascal.org/docs-html/ref/refch15.html#x212-23400015

I think it's not explicitly mentioned in the documentation, but only operators that don't have an existing built-in operation can be overload (so you can't overload e.g. Integer * Integer while Integer >< Integer (symmetric difference, not a mistyped <>) is fine).

alsor

  • New Member
  • *
  • Posts: 47
Re: overloading operators in fpc?
« Reply #4 on: March 23, 2021, 04:22:36 pm »
Is it possible to define operators in the fpc, like in the c++?

I take it you did not like C++?

no, i'm just total expert in the c++ programming.

inline operator + (int a, int b) { return a * b; }

can You rewrite this code to the free-pascal?

inline operator + (T3DPoint a, T3DPoint b) { return T3DPoint(a.x+a.x, a.y+b.y, a.z+b.z); }

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: overloading operators in fpc?
« Reply #5 on: March 23, 2021, 04:58:28 pm »
Is it possible to define operators in the fpc, like in the c++?

I take it you did not like C++?

no, i'm just total expert in the c++ programming.

Why would you bother with Pascal If you are an expert in C++?  :)
« Last Edit: March 23, 2021, 05:16:12 pm by engkin »

ccrause

  • Hero Member
  • *****
  • Posts: 856
Re: overloading operators in fpc?
« Reply #6 on: March 23, 2021, 05:11:11 pm »
inline operator + (int a, int b) { return a * b; }

can You rewrite this code to the free-pascal?

inline operator + (T3DPoint a, T3DPoint b) { return T3DPoint(a.x+a.x, a.y+b.y, a.z+b.z); }
It is clear you didn't read the link to the relevant FPC documentation that Martin posted...

alsor

  • New Member
  • *
  • Posts: 47
Re: overloading operators in fpc?
« Reply #7 on: March 23, 2021, 05:26:06 pm »
Is it possible to define operators in the fpc, like in the c++?

I take it you did not like C++?

no, i'm just total expert in the c++ programming.

Why would you bother with Pascal If you are an expert in C++?  :)

I have some very precious pascal coded application from very old version of Windows yet...
and now I try to recode it, but with the minimal cost of coding - direct recoding this code to c++ code would cost me a tremendous coding times.


And additionally: I begin with pascal long time ago, thus I'm an indisputable pascal expert too.
This is a primitive tool in fact, but I can still code everything using it.
« Last Edit: March 23, 2021, 05:37:46 pm by alsor »

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: overloading operators in fpc?
« Reply #8 on: March 23, 2021, 05:34:17 pm »
No, you should not do it.

I don't know much about C++, I'm just a hobbyist that can use Pascal and BASIC to write some simple games. But I heard many people said proper written C++ code is faster than those in Pascal. Also a lot of programmers think Pascal now is dying.

Would you put your precious applications in Pascal? That clearly is a bad idea.
« Last Edit: March 23, 2021, 05:37:16 pm by Handoko »

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: overloading operators in fpc?
« Reply #9 on: March 23, 2021, 05:39:59 pm »
Too much noise for such a simple question (and I'm contributing to it).

alsor

  • New Member
  • *
  • Posts: 47
Re: overloading operators in fpc?
« Reply #10 on: March 23, 2021, 05:44:26 pm »
I don't think... the programing language is illusion only: any code is applicable in any other language; the key is just a mastering of programing in general;
any coding language is the same in fact (with some exclusions of course).

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: overloading operators in fpc?
« Reply #11 on: March 23, 2021, 05:46:56 pm »
Interesting. You really said something that differs from what most people said. I'm starting to like you.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: overloading operators in fpc?
« Reply #12 on: March 23, 2021, 06:16:36 pm »
@Handoko, don't get deceived, "talk is cheap," ask him to show you the code. He is expert in this and expert in that. And no, languages are not equal. Some languages would help you shoot yourself in the foot, every day.



"very precious pascal coded application"
Pure B.S. but entertaining  :)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: overloading operators in fpc?
« Reply #13 on: March 23, 2021, 07:21:45 pm »
inline operator + (int a, int b) { return a * b; }

Note PascalDragon's point from earlier: you can't redefine operators on basic types.

Also you can only redefine operators which already exist, and even that excludes things like ^

Quote
can You rewrite this code to the free-pascal?

inline operator + (T3DPoint a, T3DPoint b) { return T3DPoint(a.x+a.x, a.y+b.y, a.z+b.z); }

Yes. Read the links you've been given.

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

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: overloading operators in fpc?
« Reply #14 on: March 23, 2021, 07:25:39 pm »
And additionally: I begin with pascal long time ago, thus I'm an indisputable pascal expert too.
This is a primitive tool in fact, but I can still code everything using it.

"In short in matters vegetable, animal and mineral,
"he is the very model of a modern major-general."

And pleasingly modest to boot :-/

FPC has various modes to tune it to Delphi etc. But I'm sure you knew that from your perusal of the documentation.

https://www.freepascal.org/docs.html

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

 

TinyPortal © 2005-2018