Recent

Author Topic: Has anyone converted the C librarys to FreePascal?  (Read 5433 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 673
  • Compiler Developer
Re: Has anyone converted the C librarys to FreePascal?
« Reply #75 on: October 14, 2019, 09:46:20 am »
This needs a compiler provided functionality (like the IfThen intrinsic that I had added a year or two back, but had to remove again after other devs complained).

Not sure I remember that. How did it work exactly? And what were the complaints?

Would it have allowed something similar to this, with constant record members, if combined with Ryan's generic constants patch?

Code: C  [Select]
  1. #include <iostream>
  2.  
  3. template <const auto A, const auto B>
  4. struct Min
  5. {
  6.   static constexpr auto Result = (A < B) ? A : B;
  7. };
  8.  
  9. int main()
  10. {
  11.   std::cout << Min<-9, 5>::Result;
  12. }

Seems like it would have been quite useful if so.

The fact that you can't really express that many things as compile-time constants is basically the one aspect of other languages that I find myself missing in Pascal.

Edit: Just went back and found the mailing list thread... it seems like the intrinsic was basically just an objectively better version of the normal function version?

There's various things you cannot use the normal function version for at all, like this:

Code: Pascal  [Select]
  1. program Example;
  2.  
  3. {$mode Delphi}
  4.  
  5. uses Classes, SysUtils;
  6.  
  7. const Value = (2 * 3) div 3;
  8.  
  9. begin
  10.   // ↓↓↓ Creates a TObject no matter what, and thus a memory leak!
  11.   // ↓↓↓ Why would anyone *want* it to work like that if it could be avoided with the intrinsic version?
  12.   with IfThen<TObject>(Value = 2, TPersistent.Create, TObject.Create) do begin
  13.     WriteLn(ClassName);
  14.     Free;
  15.   end;
  16. end.

The generic IfThen function came after this discussion.

And the intrinsic would have determined the result type of the IfThen expression based on the result of the if-clause. Same as the ternary operator does in C.

Thaddy

  • Hero Member
  • *****
  • Posts: 9183
Re: Has anyone converted the C librarys to FreePascal?
« Reply #76 on: October 14, 2019, 11:23:52 am »
Yes! (I wrote it..)
You can also have a specialization to TFunction, btw, so it can be used for code paths. (The docs for 3.2 contains an example)
The auto part is also covered partially by using smart pointers from other contributors (it is not in the rtl, but there are contributions)
« Last Edit: October 14, 2019, 11:32:19 am by Thaddy »
also related to equus asinus.

Akira1364

  • Hero Member
  • *****
  • Posts: 539
Re: Has anyone converted the C librarys to FreePascal?
« Reply #77 on: October 17, 2019, 09:34:54 pm »
This needs a compiler provided functionality (like the IfThen intrinsic that I had added a year or two back, but had to remove again after other devs complained).

Not sure I remember that. How did it work exactly? And what were the complaints?

Would it have allowed something similar to this, with constant record members, if combined with Ryan's generic constants patch?

Code: C  [Select]
  1. #include <iostream>
  2.  
  3. template <const auto A, const auto B>
  4. struct Min
  5. {
  6.   static constexpr auto Result = (A < B) ? A : B;
  7. };
  8.  
  9. int main()
  10. {
  11.   std::cout << Min<-9, 5>::Result;
  12. }

Seems like it would have been quite useful if so.

The fact that you can't really express that many things as compile-time constants is basically the one aspect of other languages that I find myself missing in Pascal.

Edit: Just went back and found the mailing list thread... it seems like the intrinsic was basically just an objectively better version of the normal function version?

There's various things you cannot use the normal function version for at all, like this:

Code: Pascal  [Select]
  1. program Example;
  2.  
  3. {$mode Delphi}
  4.  
  5. uses Classes, SysUtils;
  6.  
  7. const Value = (2 * 3) div 3;
  8.  
  9. begin
  10.   // ↓↓↓ Creates a TObject no matter what, and thus a memory leak!
  11.   // ↓↓↓ Why would anyone *want* it to work like that if it could be avoided with the intrinsic version?
  12.   with IfThen<TObject>(Value = 2, TPersistent.Create, TObject.Create) do begin
  13.     WriteLn(ClassName);
  14.     Free;
  15.   end;
  16. end.

The generic IfThen function came after this discussion.

And the intrinsic would have determined the result type of the IfThen expression based on the result of the if-clause. Same as the ternary operator does in C.

I found another patch you made around that time that implements it as a language construct along the lines of "variable := if boolean then a() else b()", and found that it still works fine with trunk FPC and indeed allows stuff not possible with IfThen as it exists now.

What was wrong with that version, exactly? Doesn't it solve the problems people had with the "function-like" implementation?

PascalDragon

  • Hero Member
  • *****
  • Posts: 673
  • Compiler Developer
Re: Has anyone converted the C librarys to FreePascal?
« Reply #78 on: October 18, 2019, 09:44:28 am »
I found another patch you made around that time that implements it as a language construct along the lines of "variable := if boolean then a() else b()", and found that it still works fine with trunk FPC and indeed allows stuff not possible with IfThen as it exists now.

What was wrong with that version, exactly? Doesn't it solve the problems people had with the "function-like" implementation?
There were core devs that disliked the use of the if-statement as an expression. :-X

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7503
Re: Has anyone converted the C librarys to FreePascal?
« Reply #79 on: October 18, 2019, 11:21:25 am »
What was wrong with that version, exactly? Doesn't it solve the problems people had with the "function-like" implementation?

Readability, more difficult to the point error generation (something that C/C++ obviously doesn't care about)

PascalDragon

  • Hero Member
  • *****
  • Posts: 673
  • Compiler Developer
Re: Has anyone converted the C librarys to FreePascal?
« Reply #80 on: October 19, 2019, 01:25:38 pm »
What was wrong with that version, exactly? Doesn't it solve the problems people had with the "function-like" implementation?

Readability, more difficult to the point error generation (something that C/C++ obviously doesn't care about)
What do you mean with "point error generation"?

Firewrath

  • New Member
  • *
  • Posts: 34
Re: Has anyone converted the C librarys to FreePascal?
« Reply #81 on: October 24, 2019, 12:22:48 am »
Not to step on the other interesting discussions going on in this thread, but...
I just want to say I never thought C was in anyways better then Pascal. I have been 'playing' at programming off and on for years and Lazarus/FreePascal has been the easiest language for me to grasp and the one I've done the most with, ...though it has so far just been a bunch of stuff for my personal use. :P

440bx hit the nail on the head, so to speak, as to why I was asking about converting C:
It is a fact that there are many more libraries written for one thing or another in C than in Pascal.
IE: more stuff to play with.
And I know people have done a lot of cool stuff with Pascal and there's a lot of packages in the package manager I can mess with, but my home PC, where I do my programming, lacks internet access, sooo.... yeah. :P


Other then that, I think my mistake was that I kept reading that Pascal is "C based" and thinking that conversion should be ...easier then it is.
So after this thread, I get it now, "C like" instead of "C based".  :D

Also...
The real decision is, how much programming do the two of you want to learn ?.  If the answer is "enough to write some capable programs" then, you'll do fine with knowing only Pascal.  If the answer is "some really sophisticated programs", you can also do it in Pascal but, you'll need to know C because many applicable examples/samples will likely only be available in that language.

Actually, I think I'm happy enough as a hobbyist programmer, I doubt I'll ever get the skills to be really good at it, muchless doing it for a job, but I'm good enough that it serves my purposes.
So Lazarus/FreePascal is great for that. ^-^

My friend though, I really don't know how far he wants to take it. I'll hopefully find out soon enough, seems I've been missing him the past few weeks with everything going on. He wasn't going to start learning till Dec. when he's taking time off work. So there's time to find out.

But Thanks for all the advice / opinions, plus all the other stuff, it's been helpful and interesting to read. ^-^
Sorry. I currently don't have Internet Access. So my replies might take a week. -_-

del

  • Full Member
  • ***
  • Posts: 113
Re: Has anyone converted the C librarys to FreePascal?
« Reply #82 on: October 25, 2019, 05:29:50 am »
The big draw IMHO is NOT Pascal (meh) but Lazarus. Lazarus is the kind of experience you want to have developing on Linux. However - the price you pay for this experience (and its dependency on Pascal) is that you eventually run into brick walls. And as impressive as the BGRABitmapPak is, there are times when you need more -

https://opencv.org/

Your options are to either acquaint yourself with C++, or one of the two languages that wrap it: Python or Java. IIRC Delphi made an attempt to wrap some of the old C based OpenCV stuff. But that's all majorly deprecated.  So if Python and Java can wrap OpenCV, why can't Pascal?

  • Small community.
  • Tiny subset of small community that knows C++.
  • Tiny subset of tiny subset of small community that is actually incentivized to explore this.

But anything is possible in the mysterious world of open source.

Thaddy

  • Hero Member
  • *****
  • Posts: 9183
Re: Has anyone converted the C librarys to FreePascal?
« Reply #83 on: October 25, 2019, 06:31:02 am »
There are multiple opencv bindings available. Which one are you referring to?
https://github.com/Laex/Delphi-OpenCV is the most prominent but there is also https://cviptools.siue.edu/downloads.php which has a COM interface layer that is accessible from FPC. The latter is based on opencv., afaik. I also saw some others.
« Last Edit: October 25, 2019, 06:53:10 am by Thaddy »
also related to equus asinus.

440bx

  • Hero Member
  • *****
  • Posts: 1199
Re: Has anyone converted the C librarys to FreePascal?
« Reply #84 on: October 25, 2019, 07:34:58 am »
Lazarus is the kind of experience you want to have developing on Linux. However - the price you pay for this experience (and its dependency on Pascal) is that you eventually run into brick walls.
A RAD environment isn't a magic lamp you click that makes a genie appear to write the code for you (and even if it were, legend goes you only get three (3) lines of code.)  In programming, walls aren't made of bricks, they are made of ignorance bound together by sticky lack of effort.

Your options are to either acquaint yourself with C++, or one of the two languages that wrap it: Python or Java.
The other option is, learn Pascal really well.  You can write Python and Java in Free Pascal (provided you know the language.)

why can't Pascal?

  • Small community.
  • Tiny subset of small community that knows C++.
  • Tiny subset of tiny subset of small community that is actually incentivized to explore this.
You forgot the most important reason and that is, because there are people who _expect_ other people to do the work for them instead of doing it themselves.  If you find the current binding to OpenCV lacking in some ways, you could make them everything they should be (in your opinion.)  The obvious problems are, 1. it requires work on your part and 2. it also requires you to learn Pascal well enough to do it.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

del

  • Full Member
  • ***
  • Posts: 113
Re: Has anyone converted the C librarys to FreePascal?
« Reply #85 on: October 25, 2019, 10:26:17 am »
There are multiple opencv bindings available. Which one are you referring to?
https://github.com/Laex/Delphi-OpenCV is the most prominent but there is also https://cviptools.siue.edu/downloads.php which has a COM interface layer that is accessible from FPC. The latter is based on opencv., afaik. I also saw some others.
IplImage is the old deprecated C interface. And we know how much you guys love C. Not that you like C++ any better, but those who do will benefit from virtually care-free memory management and the latest and greatest features / functionality that the OpenCV library has to offer.

del

  • Full Member
  • ***
  • Posts: 113
Re: Has anyone converted the C librarys to FreePascal?
« Reply #86 on: October 25, 2019, 10:34:26 am »
Lazarus is the kind of experience you want to have developing on Linux. However - the price you pay for this experience (and its dependency on Pascal) is that you eventually run into brick walls.
A RAD environment isn't a magic lamp you click that makes a genie appear to write the code for you (and even if it were, legend goes you only get three (3) lines of code.)  In programming, walls aren't made of bricks, they are made of ignorance bound together by sticky lack of effort.

Your options are to either acquaint yourself with C++, or one of the two languages that wrap it: Python or Java.
The other option is, learn Pascal really well.  You can write Python and Java in Free Pascal (provided you know the language.)

why can't Pascal?

  • Small community.
  • Tiny subset of small community that knows C++.
  • Tiny subset of tiny subset of small community that is actually incentivized to explore this.
You forgot the most important reason and that is, because there are people who _expect_ other people to do the work for them instead of doing it themselves.  If you find the current binding to OpenCV lacking in some ways, you could make them everything they should be (in your opinion.)  The obvious problems are, 1. it requires work on your part and 2. it also requires you to learn Pascal well enough to do it.

I get it. Lazarus is great, and if you want OpenCV (modern) you gotta role your own. Not a big shock - cuz I pretty much said the same thing. We agree to agree. Forget Python (a necessary evil where I work), and Java (never had any use for it, and probably never will).

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7503
Re: Has anyone converted the C librarys to FreePascal?
« Reply #87 on: October 25, 2019, 10:36:16 am »
What do you mean with "point error generation"?

I said "to the point error generation".   If you have an in-expression if, the whole errorhandling situation changes. The IF might suddenly associate with the next begin end block etc.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7503
Re: Has anyone converted the C librarys to FreePascal?
« Reply #88 on: October 25, 2019, 10:38:02 am »
Your options are to either acquaint yourself with C++, or one of the two languages that wrap it: Python or Java. IIRC Delphi made an attempt to wrap some of the old C based OpenCV stuff. But that's all majorly deprecated.  So if Python and Java can wrap OpenCV, why can't Pascal?

I do fulltime vision in Delphi, and I simply rarely need OpenCV (in 2010 iirc I looked at the KLT stuff, but that is about it).  Too generic (often first makes some extra datastructure like path vectors before doing the actual operation) , too slow specially for the base algorithms. Anything but base generally is too slow anyway.

It felt like targeted at e.g. PHD students to quickly get something done
« Last Edit: October 25, 2019, 10:40:43 am by marcov »

del

  • Full Member
  • ***
  • Posts: 113
Re: Has anyone converted the C librarys to FreePascal?
« Reply #89 on: October 25, 2019, 10:58:28 am »
Your options are to either acquaint yourself with C++, or one of the two languages that wrap it: Python or Java. IIRC Delphi made an attempt to wrap some of the old C based OpenCV stuff. But that's all majorly deprecated.  So if Python and Java can wrap OpenCV, why can't Pascal?

I do fulltime vision in Delphi, and I simply rarely need OpenCV (in 2010 iirc I looked at the KLT stuff, but that is about it).  Too generic (often first makes some extra datastructure like path vectors before doing the actual operation) , too slow specially for the base algorithms. Anything but base generally is too slow anyway.

It felt like targeted at e.g. PHD students to quickly get something done
That reminds me of the old joke: "IDL was invented so Forestry Majors could code". Today we replace "IDL" with "Python". What sold me on OpenCV was stuff like optical flow and the fact that it engaged OpenCL and you could literally watch the temperatures rise on your cores and GPU's, not to mention hearing the fans rev up to deal with all the extra heat. It's just cool stuff - but of course I'm not on a crusade to push it. YMMV.
 :)