top | item 34160097

A guided intro to the Free Pascal language

178 points| sysrpl | 3 years ago |getlazarus.org

100 comments

order

Peter5|3 years ago

Note for people who don't know much about FreePascal. It is a full-featured and very fast compiler. The resulting program is a rival for the best output of C/CPP compilers. It can be used in the style of simpler languages like Go and is almost as safe as Rust in a much faster manner. It has a great but old-looking IDE, Lazarus. It has been under active development for decades and is used for proper projects like:

https://dadroit.com/ https://peazip.github.io/ https://cudatext.github.io/ https://lazpaint.github.io/

As far as I know, there is no toolkit out there that lets you make fine looking applications for multiple platforms with proper speed.

The old Pascal you may know is not the new Pascal. The development of Pascal is mostly focused on ease of development while maintaining low-level programming and backward compatibility. It looks old on its face, but it is young at heart.

I recommend starting with Lazarus, https://www.lazarus-ide.org, a much lighter IDE compared to so-called light projects like VSCode, with many more features and components to play with.

Friendly word: don't let the comments with outdated information make you miss a great and fun tool.

jchw|3 years ago

I don't believe you regarding "as safe as Rust." It's been a minute since I've messed with FreePascal, but as far as I know and can ascertain at a glance, it lacks any true modelling of memory safety to even match Go, and certainly not memory ownership to match Rust. It is surely possible to write correct programs in Pascal, and maybe even easier than C, but it would not be accurate to describe it as "as safe as Rust." It's not really even close...

Don't get me wrong, though. I do think Lazarus/FreePascal are underrated, but some of these claims are exaggerated. FPC generates good code, but on par with GCC/LLVM? I'd guess it's probably sitting closer to Go in terms of performance characteristics. More than good enough, but surely a ways away from the ridiculously complicated optimization systems in LLVM and GCC which aggressively vectorize, constant-fold, DCE, inline calls, interchange loops, and so forth.

jksmith|3 years ago

Yeah, but it's not hip, and it's too wordy with BEGIN/END, and the data structures are too restrictive, and the compiler nags you, and the like. And you can write an 8M line codebase in it, touched in all kinds of foul ways, and it still works. That's the biggest codebase I've ever worked on at least.

Take that as a small (subjective) clue. Anything that is great about Rust or Nim or <..> could also have extended Pascal or M2 for the same result, and much sooner. Especially goes for M2, because the grammar was so small, thus avoiding the old PL1 traps from an overloaded parser.

FPC needs smart macros, needs to backport M2 interfaces, and the solid green thread spec* backported from M2, but it really is in the ballpark. You want gc? Well, fair enough, but you can write a pretty good mark/release* facsimile with RTTI. Or how about Eiffel simulated contracts with open arrays?

And Lazarus, good grief what a 30 year old premiere open source project. No point in even talking about how awesome it is.

Co-Pascal has been doing co-routines for 45 years.

In turbo pascal, you could create mini-heaps that could be dumped most anytime. They weren't smart, but still a cool feature.

andai|3 years ago

You mention that Pascal is almost as safe as Rust. This comes as a great surprise to me, because I was under the impression that Pascal and C are very similar.

(Though I hear the strings, at least, know their own size!)

ptx|3 years ago

> It can be used in the style of simpler languages like Go and is almost as safe as Rust in a much faster manner.

What does this mean, exactly? Usage in the style of Go requires garbage collection and being safe like Rust (without GC) requires a borrow-checker, and as far as I know Free Pascal doesn't have either.

chinabot|3 years ago

Should add the freepascal community is much more forgiving of noobs and generally well behaved (trolls dont like quiche I guess!)

ThinkBeat|3 years ago

Pascal gets an underserved bad reputation. I am showing my age but Turbo Pascal was (is) great. Extensions from Turbo and Delphi Object Pascal is great as well.

I hate the insane pricing Embarcado has imposed on Delphi. They have had cross platform GUIs in Delphi for a long time. Something for some reason other tools struggle and mostly fail to do.

I have not been able to "sell" it to clients mostly due to price.

klodolph|3 years ago

I think this was a reason for C’s success back in the day. If C compilers were a little cheaper and easier to get, then they’d completely take over, long-term.

I remember that Pascal was the norm for Mac programming for a time during the late 1980s or so. You could use THINK Pascal (later Symantec) or MPW Pascal. Pretty soon everyone was programming in C and compiling their programs with Metrowerks CodeWarrior.

ak39|3 years ago

Embarcadero insane pricing of Delphi is the reason I haven't continued beyond my old version Xe2. I guess they are making hay while the sun shines with the locked-in customers.

pkphilip|3 years ago

Well said. If not for the insane pricing and the fact that Delphi does not run on non-Windows platforms, it is a great product with a basically unmatched GUI app development capability

squarefoot|3 years ago

A peculiar aspect of Lazarus (Free Pascal's Delphi-like IDE) is that it either cross-compiles for other architectures or can run itself on them, therefore generating native GUI software that runs immediately on the target platform. That is, you can develop ARM executables on a Raspberry PI using Lazarus running on the Pi itself. I would also suggest to take a look at FPCUpDeluxe, an installer that does all the tedious job of dealing with dependencies for installing FPC, Lazarus and a great number of modules and components.

https://wiki.freepascal.org/Lazarus_on_Raspberry_Pi

https://wiki.lazarus.freepascal.org/fpcupdeluxe

A personal dream: having something like Lazarus that works with other languages too; that would likely become a game changer for desktop apps development.

terminalcommand|3 years ago

I think the popularity of pascal has something to with Modula-2 (the successor of Pascal invented by Wirth) having been recently included as an officially supported language in gcc alongside Rust. I tried lazarus again, I was amazed. It is as simple as using VB6 for creating quick GUIs that run everywhere. Turbopascal is blazingly fast, produces efficient and small binaries.

Pascal also has libraries for nearly everything, check awesome pascal for a list of actively maintained third party libraries: https://github.com/Fr0sT-Brutal/awesome-pascal

I feel like pascal is a hidden gem.

speed_spread|3 years ago

I used to be a hard Delphi/Pascal guy and still think it is much better than C for it's strong typing and overall "squareness". But I'd have a hard time going back to it nowadays. The 'var' section, magic "Result" variable and begin/end pairs are just from another age.

Programming languages have advanced _a lot_ since Pascal was created. It's core principles remain valid (safety by default, readability, flexibility). But the feedback loop between language design and actual usage is much tighter thanks to the Internet and open source.

Some things I still miss from my Delphi days are the language-level enumsets, near-instant compilation and RAD tooling (GUI builder).

5e92cb50239222b|3 years ago

I pretty much started programming in Delphi (which was very popular in this part of the world, and still is to some extent).

After using VCL for a year or two, I started branching to other, much more popular programming environments, and was shocked about how bad they were for GUI development. Why would anyone put themselves through the pain of writing interfaces manually using programming languages not designed for it (like C) when you could design everything in a convenient GUI builder?

It's sad they decided to focus on the "enterprise" market and buried themselves in the long term. Writing GUI stuff in Delphi was (probably is) a pleasure: they compiled quickly, looked native, and ran fast on my shitty hardware.

open-source-ux|3 years ago

"The 'var' section, magic "Result" variable and begin/end pairs are just from another age...Programming languages have advanced _a lot_ since Pascal was created."

I understand why some programmers dislike Pascal's syntax, but C and C++ are also old and still popular (including for new projects). Syntax and semantics are closely entwined but C and C++ syntax still influence "modern" languages.

revanx_|3 years ago

nothing magic about Result keyword, it's just an implicit pointer, you can avoid it completely by using explicit pointers.

tifadg1|3 years ago

Pascal killed my interest in programming for a while as it was force fed in both high school and earlier collage years. Reflecting back, it was the pessimistic approach to teaching of just pushing math problems via programming.

alexvoda|3 years ago

I also experienced mandatory Pascal classes in high school and similarly hated it, especially because I already knew other programming languages.

Much later however, I started really appreciating Pascal, both as a teaching language and as a multiplatform language.

PeaZip, CudaText, Transmission Remote GUI, UltraStar Deluxe and plenty others are written in FreePascal.

projektfu|3 years ago

Ca. 1994 we had Pascal as the language for AP computer science. Later, they started adding other languages. In my school the options were Basic (TrueBasic) which was offered as a stepping stone to APCS and pascal. I also had the opportunity to study Scheme through CTY's excellent summer class.

At the time, I remember pascal feeling like "real programming". The programs were compiled and they operated at various levels of abstraction, from pointers up through advanced data structures and interfaces/ADTs. I didn't see Scheme as more than a toy, which was partly because the MIT Scheme implementation was just an interpreter. And I hated Basic and still do.

Pascal was great because it was pretty easy to learn and somewhat consistent. Its most annoying feature, semicolon-as-separator, was easily handled by the terrific Think pascal editor.

In its time, Pascal was a great choice. There were real-world things that you could do immediately using Think Pascal and Turbo Pascal. Making the switch to C and Unix wasn't simple but probably easier than starting with Basic. However, I was left with a bias towards wordy languages like Pascal, Ada and Modula-3 that took a while to get over :)

sysrpl|3 years ago

Free Pascal was originally derived from Object Pascal and later Delphi, both of which added more object oriented programming features to Pascal. Additionally, it supports some of the more advanced features introduced to Pascal with later releases of Delphi including managed types, interface types, operator overloading, generics, implicit and explicit conversions, extension properties and methods, as well and user defined initializers and finalizers for your custom types, and more.

I could write an article about each of these features, but in summary they each are powerful and help users write more useful programming code when leveraged correctly.

JanisErdmanis|3 years ago

I also had mandatory pascal during my high school years. Although I knew a little of C, C++ and python at that time and felt a bit obscured about learning pascal, today, I deeply admire my teacher's choice of pascal. Pascal has a nice beginner terminal IDE with a debugger and breakpoints; it has types, stack allocation and memory planning before the program is being run; recursion is possible, and it does not have many obscure features like pointers and OOP obscurities. Today, perhaps I would suggest Julia as the first language to teach, but pascal is still at the top.

FpUser|3 years ago

Luckily for me I've never received any formal training in programming back in school. My introduction to programming happened in University and research labs in a very simple way - I needed to process and interpret some experimental data live and the only way I could do it was a computer. So I got me a user guide for particular one and bunch of books. In a few days I was already coding away. In exactly the same way I was introduced to electronics. Had to make some equipment for my research as you could not by one. Started with machine codes but over the time I've used many languages including Pascal from Borland. Programming was far from my main job but at some point I switched from science to creating commercial products as I was good at it. Some products I own and some I develop for clients. Most of products are ether pure software or contain some good chunk of it so I am still happily coding away even though I am 60 already.

coliveira|3 years ago

Pascal is great as a teaching language because of its simple syntax. It is also a pre-OO language, so you don't need to spend time explaining what classes and objects are. For an introductory programming course it is a very good language.

analog31|3 years ago

This was all people knew. Most people saw programming as a branch of math. When a high school offered a programming class, it was often taught by a math teacher. The smaller colleges in my state, that were beginning to add computer science, did so by combining it with the math department.

My mom taught programming in the early 80s. She took a course at the nearby community college, and a year later, was teaching the course. Her background was... high school math teacher. She said: "Programming is just math, a program is like a proof." Fortunately I loved math and proofs, so that way of thinking wasn't an obstacle for me. Clearly we know differently now.

tanakian|3 years ago

they just didn't know how to teach back then.

they could approach the same math from the other side, by starting to write games. and gradually come to game math. that would be more interesting, and then it would become obvious why one may need some math while programming.

markus_zhang|3 years ago

Pascal aside, back when I was 10 my father also tried to force math programming to me when I showed interest in games. Apparently it didn't work.

tristanbvk|3 years ago

Personally I read the programmer manual for FP. You can do both Object Pascal OOP and Delphi OOP.

Wild. Also nice units for doing mmap and all libc stuff. I really ought to finish learning Pascal and actually write something in it. Not a bad language.

tanakian|3 years ago

not only libc. what is very cool is that most of the parts of very rich fpc library has no libc dependency.

so necessary parts get linked statically to the resulting binaries, and those binaries do not require libc. most of the library goes with roots to kernel calls, bypassing libc.

once you start using threads or link to other c libraries (like gtk) then libc becomes a dependency.

kqr|3 years ago

When I looked into learning Free Pascal (not that many years ago) it seemed to me like Ada was the more well-developed, actively used language of that sort of heritage.

Have any of you used both languages? What made you stick with Free Pascal instead?

(To be clear, I'm interested in a comparison that goes beyond the price point of the commercial compilers, which is a tired subject at this point.)

pjmlp|3 years ago

I guess if you are into GUI applications, that isn't something that Ada is that great about, as most vendors target headless deployments.

Akira1364|3 years ago

Don't download the bundled Free Pascal and Lazarus distributions from this site, they're very outdated. Just get the normal releases from the actual Free Pascal and Lazarus sites which are linked at the top of the page. Lazarus comes with Free Pascal bundled by default anyways, so you don't need to grab the compiler separately if using Lazarus.

user3939382|3 years ago

I downloaded Lazarus from its release page on Github and after installing it didn’t work, saying it needed extra binaries that it couldn’t find. Now I have to see what it’s talking about, what to install, etc. Kind of a hassle.

themodelplumber|3 years ago

Some recent personal projects with laz and fp got me pretty interested in it.

I have nothing impressive to show for my work except for a lot of retro-pride when I type in e.g. the snippet which triggers the Pascal program which tells me how old I am. With some bells and maybe a couple whistles.

I was surprised to learn that there was Pascal for the C64, which I thought was more of a "BASIC or bare metal" system.

I too was a victim of the great AP Pascal force-feed in my HS comp. sci. class...but fortunately at the same time there were demoscene programmers around the world blowing my mind with Pascal programs and it seemed like there were new projects to appreciate online (BBS) every day after school.

To whit: Someday I would like to write a Pascal raytracer that tells me how old I am, with emoji output, since my other ride is a text editor. (And if you don't like my driving, get off the sidewalk!)

bigpeopleareold|3 years ago

Heh - I didn't do great in AP with Pascal either. I remember though trying to make it worth buying CodeWarrior for Mac OS (oh how the Internet changed everything ... :D ), which had a Pascal compiler, but I got overwhelmed looking at the Mac Toolbox docs and couldn't make much sense of it. (I know there was also MPW, but I didn't know how to get my hands on it.) The school computers ran Turbo Pascal on MS-DOS. I vaguely remember having to write programs on paper without a lot of access to those computers.

But, I tried FP last year. I wrote a program to mimic a small tool in the company I worked for. It was fun actually. At the time, I sort of wanted a lot to write stuff in Emacs. There is an LSP server for FP but it's not really complete.

mikewarot|3 years ago

There are some things that Lazarus/Free Pascal does right:

- It has the most sane strings you'll ever see, you don't have to allocate them, and they can hold gigabytes of text.

- You'll never confuse equality tests = with assignment :=

- It's case insensitive

- It doesn't do macros

- It is one of the fastest compilers available

- It is one of the best GUI builders available

Things it doesn't do right

- Begin/End seems verbose to some people

- Other "standard" pascal systems gave it a bad name

- Historically, Borland et al, priced people out of their products and killed off a loyal use base.

boobybrown25|3 years ago

Lazarus has, imo, the best wysiwyg GUI editor you can get. Enjoy it a lot more than fighting with qt.

SomeHacker44|3 years ago

Back when I learned computing, I had an old Apple ][+ with a Z80 card. There were only a few choices to program it. Apple and Z80 BASIC, Apple Logo, 6502 assembly (I did not have access to a Z80 assembler) and UCSD Pascal.

Pascal was awesome. So much better than the others. It really launched my interest in coding once it clicked.

I remember discovering that the early Apple game Wizardry was written in it one day when I was hacking around with it.

spapas82|3 years ago

I still remember that the thesis of a fellow student back in the university 20 years ago was to create an educational version of Pascal called... "Paschool".

I laugh out loud when I remember it, definitely the programming language with the most funny/catchy name ever!

grumblepeet|3 years ago

Coincidentally i tried to install Lazarus yesterday and hit issue after issue, two being some missing dependencies, which were items that I found in the documentation to be downloads as well. It then complained about lack of gdb, which I do understand (on an older Macos here) but it did occur to me, if you know it is needed, why not include it in the download?

Cut a long story short, Ive installed Dr Racket today and working through the tutorials.

xchip|3 years ago

With all my loving(it was the first language I learnt), I don't think we need yet another language. We need to fix the ones we have.

shimonabi|3 years ago

Is there a way to use the Lazarus libraries, but write code in C++, like with C++ Builder?

signaru|3 years ago

Qt and wxWidgets are also worth considering assuming you would just be "writing" the code and not taking advantage of the visual designers. These do have visual designers, but Lazarus if far easier to use, IMHO, following the footsteps of Delphi and VB classic.

lelanthran|3 years ago

> Is there a way to use the Lazarus libraries, but write code in C++, like with C++ Builder?

I write lazarus programs, and link to my lib**.so files which are all written in C. Works very well.

tanakian|3 years ago

yes, i believe delphi rad studio has c++ compiler and the libraries are written in pascal, it is a lineage from c++ builder.

badsectoracula|3 years ago

You can have your C/C++ code exposed to Free Pascal via a C API loadable as a shared library / DLL, but beyond that there isn't much you can do (Free Pascal comes with a `h2pas` tool to help convert C headers to Free Pascal units for such use).

This is of course far from having something like C++ Builder.

To have something like C++ Builder it'd need a lot of things to work together:

1. A C++ compiler will need to be extended so it can use Free Pascal classes (in terms of memory layout) and add any necessary language functionality for "published" sections (RTTI data that allows FPC objects to describe themselves and be streamed/serialized), metaclasses (classes in FPC are objects and can have their own virtual methods, including virtual constructors), delegates (basically "fat" pointers) as well as data types like sets, strings, etc that FPC has. LCL (Lazarus' framework) relies heavily on pretty much all of FPC features.

2. FPC will need to somehow be able to import classes defined on the C++ side too so that components written in C++ will be usable in Lazarus. Also almost goes without saying but it should be possible to statically link programs using both FPC and C++ in the same executable.

3. The C and C++ runtime libraries will need to use FPC runtime library functionality for things like memory management (FPC's memory manager is pluggable) so that "crossing" memory management will work fine - even across dynamic library boundaries (e.g. calling `malloc` on the C++ side and then "FreeMem" on the Free Pascal side, even when the latter is in a dynamic library that shares a memory manager with the host program, should Just Work).

4. Lazarus' "codetools" will need to be expanded to support parsing C++ in addition to Free Pascal so that all of the IDE's automatic code writing and refactoring functionality will work for C++ too - things as simple as double clicking on a button and having the methods be written automatically in the editor with the cursor placed in the method body rely on this working.

These are from the top of my head, there might be other issues to handle, but even from the above it should be clear that it requires not only a lot of work but also a lot of different projects to work together (unless FPC and/or Lazarus devs decide to implement their own C++ compiler and runtime).

In the short term it might be easier to use Qt Creator which has some RAD-ish features. It is certainly clunkier, the API feels to be made with a code-first (as opposed to LCL's WYSIWYG-designer-first) approach and the IDE isn't as featureful (also at least for me it never works out of the box on Linux) but it is the closest open source project you can find for C++.