Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Safe bet statically typed languages?
View unanswered posts
View posts from last 24 hours

Goto page 1, 2  Next  
Reply to topic    Gentoo Forums Forum Index Gentoo Chat
View previous topic :: View next topic  
Author Message
carcajou
Apprentice
Apprentice


Joined: 10 Jun 2008
Posts: 248

PostPosted: Thu Jul 14, 2022 12:34 pm    Post subject: Safe bet statically typed languages? Reply with quote

Hi guys.

So this summer I wanted to start learning new programming language. "Old school" way, buying a good book and going step by step.

My experience is primary related to dynamic languages (Ruby and Lisp). I code just for fun, as a hobby (I'm not developer and my day job is business administration - so no programming at all). I wanted to change a focus from dynamic to statically typed languages, but it is a wide range of choices. The last time I programmed in statically typed language, it was almost 20 years ago in a high school using Pascal. I apologize for asking such general question here, but I do not participate at other tech related and relevant communities.

There is no specific domain. Just something general purpose and to go with the flow. Also, I do not care about trends and hypes, because I will most probably not use it professionally.

I initially considered to keep it simple and go with the plain C, however, I am not sure I would actually ever go low-level and use it's manual memory management features (or downsides).

I see there's a huge "hype" around Rust, but it is difficult for me to make a distinction between PR and actual technical advantages. Additionally, language seem intimidatingly enormous (in terms of size - C++ way).

Also, the language should be supported in Gentoo.

I would appreciate a feedback, recommendation and maybe a tip on the book. Thank you.
Back to top
View user's profile Send private message
pa4wdh
l33t
l33t


Joined: 16 Dec 2005
Posts: 883

PostPosted: Thu Jul 14, 2022 3:29 pm    Post subject: Reply with quote

In my opinion you can't go wrong with C. C in itself is still widely used and many languages take inspiration from C. Don't worry about the downsides, you will encounter them :), and there's also value in learning that.

I personally avoid rust wherever I can :)
_________________
The gentoo way of bringing peace to the world:
USE="-war" emerge --newuse @world

My shared code repository: https://code.pa4wdh.nl.eu.org
Music, Free as in Freedom: https://www.jamendo.com
Back to top
View user's profile Send private message
Leonardo.b
Guru
Guru


Joined: 10 Oct 2020
Posts: 308

PostPosted: Thu Jul 14, 2022 4:21 pm    Post subject: Reply with quote

C. I think it is like a LEGO game: you combine routines from various libraries to build new programs. I like it.

I started by reading K&R, a classic.
This guide also:
https://beej.us/guide/bgc/

There are a lot of real-world programs, easy to read and tweak even for a beginner.
Back to top
View user's profile Send private message
spica
Guru
Guru


Joined: 04 Jun 2021
Posts: 330

PostPosted: Thu Jul 14, 2022 6:47 pm    Post subject: Reply with quote

Starting a complex programming language is difficult and time-consuming, and the initial mood can end before there is any result.
Back in 2012 I wanted to learn Ruby and started a pet project on Rails. The performance of the application was not enough according to pagespeed, and I rewrote it in Golang.
Golang is simple, easy, and you will not spend too much time learning it. In my opinion it is like C with batteries, the work with strings is easy, in most cases no need to specify pointers* explicitly, simple parallelization, good for writing microservices, can be built statically.
There's a tutorial with sandbox on their site.

When I was learning C, I wrote own emulator of Z80, but that was really time consuming.
Back to top
View user's profile Send private message
miket
Guru
Guru


Joined: 28 Apr 2007
Posts: 497
Location: Gainesville, FL, USA

PostPosted: Fri Jul 15, 2022 2:24 am    Post subject: Reply with quote

I second Spica's recomendation of Go. Like C, it is a simple language that compiles quickly into executables that run quickly. Go's syntax improves on C's syntax. It treats strings as a primitive type and handles allocation and garbage collection for you.

Go is safer. C offers you many ways to shoot yourself in the foot (buffer overflows, out-of-bounds array access, and misuse of pointers and casts, to name a few); like Pascal, Go catches most of those errors at compile time and the rest at run time. Go does this with a syntax that is much less fussy than that of Pascal.

Indeed, Go programs have a syntax that tends to be as uncluttered as the syntax of interpreted languages but with full type checking and with much better runtime performance.

Go has some fancier features that you are better off ignoring as you learn the language. Stay away from goroutines and channels until you have a solid foundation--and only then if you have measurable reason to use them.

Take a look at A Tour of Go as a good starting point.
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6780

PostPosted: Fri Jul 15, 2022 5:07 am    Post subject: Reply with quote

I would recommend one of (probably in this order)
  1. Java
  2. C++
  3. Go
In no case, I would recommend pure C.

Let me give reasons why: Java is a really old-school typed language. In particular, it has a rich set of libraries (especially if you use guava), compiles extremely fast and runs almost as fast as C, C++, and Go. It is perhaps the best way to learn “clean” programming. It does all the nasty memory management for you. The disadvantage is that especially on Unix you cannot expect everybody to have a java code interpreter and its libraries installed, so distributing java on Unix is a bit nasty.

When you want to program a bit closer to the machine and also learn about pointers and the problems involved with it, C++ is the way to go: It contains all of pure C as a subset (so you can use all the advantages of pure C) (for the experts: yes, there are few subtle differences in some corner cases, but in practice these play no role), but for simple things like strings (but also more complex data structures like maps which you will need sooner or later) you can keep things simple: The STL (the C++ standard library) has “practically” automatic memory management and “feels” a bit like java, although underneath lies ”manual” memory management (but it is hidden by the well-thought libraries).

Go is the new hype: It has the convenience of java (no pointers, full memory management), but is still a bit faster than java, because it compiles into a true binary which does not need to be interpreted (although with JIT compilation of java the advantage is not much). Also some of the clumsy notation of java (and even more of C++) is avoided in go. The main disadvantage is that it is so new and so the set of available libraries is not very rich yet. Another disadvantage is that you will not really learn about classical concepts like pointers (as in C), and so if for some reason eventually you want really full speed (and not a garbage collection kick in in the wrong moment), you cannot get it with go. Another problem with go (the same with rust) is that it is available only on a few architectures. For instance, current gcc-12.1.1_p20220625 cannot compile a go compiler on x86, but nobody seems to care much about it (and it is not the first time that this happened).
Back to top
View user's profile Send private message
Goverp
Advocate
Advocate


Joined: 07 Mar 2007
Posts: 2179

PostPosted: Fri Jul 15, 2022 8:43 am    Post subject: Reply with quote

FWIW, I'd avoid antiques like C and C++. There are plenty of more modern static-typed languages. I'd plump for Rust (even though I've yet to write anything in it myself). It would probably be harder to learn, but the skills acquired would be more valuable, and the programs produced on the way far more secure... C lets you make far too many mistakes (the static typing just makes one small subset of errors harder, but you can find how to defeat it all over stackoverflow.com). What C++ tried to do 40+ years ago, Rust does far better today.

I guess if you want an old-style static-typed language that's easy to learn, you could try BASIC. :-)
_________________
Greybeard
Back to top
View user's profile Send private message
C5ace
Guru
Guru


Joined: 23 Dec 2013
Posts: 484
Location: Brisbane, Australia

PostPosted: Fri Jul 15, 2022 10:22 am    Post subject: Reply with quote

Go with "C".

I started in 1985 with assembler on the first Japanese IBM clone i received as a gift using the "Debug" that was on the IBM DOS floppy. Then tried Turbo Pascal and ran against a wall. Finally changed to Turbo C and was able to earn some money with coding.

I recommend the book "C for Dummies" to learn the practical basics of "C". Also study the documentation of www.gnu.org/software/libc/ and the sources of "sys-libs/glibc".

CodeBlocks "dev-miscs/codeblocks" is a very easy to use IDE. Emerge with "contrib" flag enabled.
_________________
Observation after 30 years working with computers:
All software has known and unknown bugs and vulnerabilities. Especially software written in complex, unstable and object oriented languages such as perl, python, C++, C#, Rust and the likes.
Back to top
View user's profile Send private message
xineg
Tux's lil' helper
Tux's lil' helper


Joined: 14 Mar 2006
Posts: 115
Location: Australia.

PostPosted: Fri Jul 15, 2022 12:46 pm    Post subject: Reply with quote

++ for C
It's outstanding the support C is getting here. I confess my love for C.
Many of the other languages we use are also written in C (like python). Linux is written in C.
Rust is written in C and C++. And of course Real men use C!
Back to top
View user's profile Send private message
pa4wdh
l33t
l33t


Joined: 16 Dec 2005
Posts: 883

PostPosted: Fri Jul 15, 2022 2:06 pm    Post subject: Reply with quote

I generally avoid go and any project written in it.
It's a google language and i don't trust google anywhere near my devices :)
_________________
The gentoo way of bringing peace to the world:
USE="-war" emerge --newuse @world

My shared code repository: https://code.pa4wdh.nl.eu.org
Music, Free as in Freedom: https://www.jamendo.com
Back to top
View user's profile Send private message
miket
Guru
Guru


Joined: 28 Apr 2007
Posts: 497
Location: Gainesville, FL, USA

PostPosted: Fri Jul 15, 2022 4:13 pm    Post subject: Reply with quote

I have some of quibbles re. the dismissal of Go.
mv wrote:
Another disadvantage is that you will not really learn about classical concepts like pointers (as in C)
Go very definitely does have pointers--including pointers to functions. What doesn't allow are pointer arithmetic and willy-nilly type casts--two of the things that have led to a lot of CVE's in C programs over the years.

mv wrote:
and so if for some reason eventually you want really full speed (and not a garbage collection kick in in the wrong moment), you cannot get it with go.
At least in the Go applications I've written or used, I've never noticed a garbage-collection stall. What I have seen in high-traffic environments (using non-Go programs, mind you) are stalls from the database (especially when it decides to use a novel execution plan) or from network congestion. Stalls in the Go garbage collector likely pale in comparison.

mv wrote:
Another problem with go (the same with rust) is that it is available only on a few architectures. For instance, current gcc-12.1.1_p20220625 cannot compile a go compiler on x86, but nobody seems to care much about it (and it is not the first time that this happened).
I have not found that bug report, but I suspect there's been no fix for the specific problem because the preferred ways to get an x86 Go compiler work. Because Go is a GCC language, it supports many more target instruction sets than does Rust--though library support may be less comfortable in those architectures. (The lack of GCC support is, in my opinion, the biggest reason to keep Rust of out the linux kernel.)

Also, from what I've seen, Rust has a much steeper learning curve than does Go--plus a lot more politics.
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6780

PostPosted: Sat Jul 16, 2022 6:48 am    Post subject: Reply with quote

miket wrote:
I have some of quibbles re. the dismissal of Go.
mv wrote:
Another disadvantage is that you will not really learn about classical concepts like pointers (as in C)
Go very definitely does have pointers

I would call them references, though the go terminology is a bit confusing here.
Quote:
What doesn't allow are pointer arithmetic and willy-nilly type casts

So exactly that makes the concept of a pointers is excluded - that's why I would call them references. This is not bad per se - as you wrote, pointers can lead to all sort of problems if the programmer makes a mistake, which is easy to do - but one cannot say that one understands what pointers are if one only knows go.
Quote:
mv wrote:
and so if for some reason eventually you want really full speed (and not a garbage collection kick in in the wrong moment), you cannot get it with go.
At least in the Go applications I've written or used, I've never noticed a garbage-collection stall.

Whether you notice it or not - it is something which is not needed in, say, C++ and thus slows down the application. For a real-time application like the kernel it is certainly a no-go for this reason. If you have lots of memory and an application which hardly needs memory, it is of course less noticable. The same problem with java BTW: That's what I dislike most about java, that even a tiny application takes lots of memory (even worse, you have to decide in the beginning how much it takes.).
Quote:
mv wrote:
Another problem with go (the same with rust) is that it is available only on a few architectures. For instance, current gcc-12.1.1_p20220625 cannot compile a go compiler on x86, but nobody seems to care much about it (and it is not the first time that this happened).
I have not found that bug report

Me neither for that version, but I had not spent time to report it (gcc only compiles on x86 in current testing only with USE=-go); when looking for the error in the net, I saw that this happens regularly on all sorts of architectures and then needs months for being fixed. It seems that in practice nobody really cares about go on gcc. And the official go compilers are binary (although you can bootstrap once you have some go).
Quote:
The lack of GCC support is, in my opinion, the biggest reason to keep Rust of out the linux kernel.

rust bootstrapping is insane: If you want to bootstrap it for a new architecture you have to compile practically every existing rust version in that order - meanwhile probably some hundreds of versions. I almost couldn't believe it when I learnt about it, but it seems to be true: Whenever the rust developers invent a new feature, they hype it that much that it must get used immediately in the next compiler version. All that without having a "minimal rust" for bootstrapping. I would never use/recommend a language developed by people lacking that much common sense.
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6780

PostPosted: Sat Jul 16, 2022 7:03 am    Post subject: Reply with quote

Goverp wrote:
FWIW, I'd avoid antiques like C and C++.

That's exactly why I strongly discouraged C. C is extremely clumsy, and except for some specialized applications like the kernel, there is no reason why you shouldn't use the more advanced C++ features: C is like using electric bike (C++) without the motor. You can use it for “philosophical”/ecological reason or to show how a tough guy you are or on streets where motors are forbidden. But if you are honest, it is not really sane to switch off the motor for normal usage.

However, I disagree that C++ is antique: The language has improved so much in the previous years that it is really nice to use. And concerning speed, it is unbeatable, although you have practically all concepts of younger languages as well.

Yes, with go and rust some error sources are eliminated - but mainly at the cost of removing things from the language. go at least additionally gives a lot of convenience by providing full memory management, rust not really. Another reason why I would never recommend rust, I gave in another posting.
Back to top
View user's profile Send private message
Etal
Veteran
Veteran


Joined: 15 Jul 2005
Posts: 1932

PostPosted: Sat Jul 16, 2022 2:22 pm    Post subject: Reply with quote

mv wrote:
rust bootstrapping is insane: If you want to bootstrap it for a new architecture you have to compile practically every existing rust version in that order - meanwhile probably some hundreds of versions. I almost couldn't believe it when I learnt about it, but it seems to be true: Whenever the rust developers invent a new feature, they hype it that much that it must get used immediately in the next compiler version. All that without having a "minimal rust" for bootstrapping. I would never use/recommend a language developed by people lacking that much common sense.


That's not true. You can bootstrap rust-1.54.0 with mrustc on amd64, so that's 8 versions to rebuild to latest. You can also cross-compile to any supported architecture. I went through this exercise last winter, both my amd64 and arm64 rust packages are both derived from my bootstrapped build.

Having a compiler written in the language is not at all unique to rust either, if you look at pretty much any common compiled language, it's the same process. Good luck getting Haskell or OpenJDK bootstrapped.

Also, gcc-rust is in the works, and the GCC steering committee is ready to accept it into GCC 13. Once that's done, the whole bootstrapping thing will be moot.
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


Joined: 20 Jul 2019
Posts: 1699
Location: South America

PostPosted: Sat Jul 16, 2022 4:18 pm    Post subject: Reply with quote

Well, I'm biased towards C++, so I'll state my reasons to try to compensate for it :)
  • As already stated, it has a large subset that is compatible with C.
  • It is possible to both write high-level, object-oriented style programs that look like Java, and low-level, procedural-style programs that look like C (see this). Plus this, which I don't know if it is available in any other language, but that's probably advanced use.
  • Shares with C the same execution environment model (only augmented to specify object construction and destruction details).
  • Has both reference types, which sort of can be used like in Java, and true pointer types like C.
  • Supports automatic storage duration like C, not just static and dynamic / allocated storage duration, which enables programming techniques that avoid both manual memory management and use of garbage collection (see the "local objects" part).
  • "What you don't use, you don't pay for".
  • On the implementation side, also as already stated, good compiler and architecture support, unlike Go and Rust. Yes, IMO getting Rust support in GCC would be a significant improvement, but that day has not arrived yet ("arrive" = be available in e.g. Gentoo's amd64 stable branch).
  • Available compilers produce machine code. So no interpreters, no middle layer between code and actual hardware like the Java virtual machine, no nothing.
  • The only "extra luggage" on both [GNU/]Linux and the BSDs produced by available compilers when compiling a simple C++ program, compared to a simple C program, is getting linked to a C++ standard library implementation (like GCC's libstdc++ or LLVM's libc++). Reasonable price for all the added functionality if you ask me.
  • Last but not least, I like how the creator of C++ thinks and reasons :)
mv wrote:
[...] for simple things like strings (but also more complex data structures like maps which you will need sooner or later) you can keep things simple: The STL (the C++ standard library) has “practically” automatic memory management and “feels” a bit like java, although underneath lies ”manual” memory management (but it is hidden by the well-thought libraries).).
Agreed.

mv wrote:
C is like using electric bike (C++) without the motor. You can use it for “philosophical”/ecological reason or to show how a tough guy you are or on streets where motors are forbidden. But if you are honest, it is not really sane to switch off the motor for normal usage.
Nice analogy :D

kukibl wrote:
I wanted to change a focus from dynamic to statically typed languages, [,,,]
It should be noted that, while compatible with C in this aspect, C++ is stricter with types.

kukibl wrote:
language seem intimidatingly enormous (in terms of size - C++ way).
C++ is a big language, but it is not necessary to have deep knowledge of every aspect. Probably nobody does anyway :) Documentation is available when knowledge of specific details is required for some project. And there is no need to use every feature in every program. Being big just means that there is a wide array of choices for picking the subset that best fits the program's needs :)

Goverp wrote:
FWIW, I'd avoid antiques like C and C++.
Ideas like this probably come from the unfortunate fact that there seem to be fewer easily available resources that describe modern C and C++ (as in C 2017 and C++ 2020) than those that describe '90s C and C++. While '90s C might not be that different from modern C, I would definitely throw away everything that describes anything older than at least C++ 2011.
_________________
NeddySeagoon wrote:
I'm not a witch, I'm a retired electronics engineer :)
Ionen wrote:
As a packager I just don't want things to get messier with weird build systems and multiple toolchains requirements though :)
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 22671

PostPosted: Sat Jul 16, 2022 4:58 pm    Post subject: Reply with quote

mrustc is an external project run by someone who wanted a less crazy way to bootstrap Rust. Since it is external, it doesn't keep up with the breakneck pace of Rust feature dependence, which is why it is already 8 releases behind. I think it is useful that mrustc offers a way to bootstrap without going all the way to the beginning, but since it is external and does keep falling behind, I can't let its existence excuse the way the core Rust community handles the bootstrap process. (Although if I had to pick a single reason to avoid Rust, it's how painful it is to parse. Even reading well commented Rust code where I know from the comments what it should do, I can't see how the line noise on my screen results in the compiler producing the claimed program.)

Yes, compilers written in the language they compile are common, but the degree of backward compatibility varies widely. Haskell and Java on Linux have always been a pain in part because of their messy bootstrap process. Compare this to gcc, which historically could be built with any C89 compiler you had on hand, and would then handle bootstrapping itself the rest of the way. Even now, a fairly current gcc (gcc-10) only requires an mpfr from 2011 or newer. According to Prerequisites for GCC:
  • <gcc-4.8 could be bootstrapped with a C89 compiler. =gcc-4.8.0 was released 2013-03-22. This suggests you could use a compiler from the early 1990s up through 2012 (gcc 4.7 released 2012-03-22) and expect it to work.
  • <gcc-11 could be bootstrapped with a C++98 compiler. =gcc-11.1 was released 2021-04-27. =gcc-10.x saw gcc-10.4 released 2022-06-28, and presumably still can be built with a C++98 compiler.
  • >=gcc-11 requires a C++11 compiler, so you will need something from within the roughly 10 years before gcc-11 was released.
If I read that right, you can go from a bare C89 compiler (if you can even find one that lacks everything newer anymore) to gcc-4.7. gcc-4.7 had substantial, but not total, support for C++11, so let's call it only a C99/C++98 compiler and not a C++11 compiler. Using gcc-4.7, you can then build gcc-10, which gets you a full C++11 compiler. Per the prerequisites page, a C++11 compiler can build gcc-11 and later, with no upper bound documented, which presumably means that gcc-13 (still in development) can be built using a C++11 compiler. Therefore, using any random C89 compiler you can find, you can perform two intermediate hops to reach the cutting edge compiler, crossing more than 2 decades of development.

Compare that to Rust, where the official position is that you usually need the most recent release. As I write this, Rust 1.62 is current, and was released June 30 2022. The preceding release, Rust 1.61 was released May 19 2022. The most recent Rust that mrustc can handle is 8 versions ago, and goes back only to July 29 2021. If you want to avoid mrustc and use the official bootstrap process, then crossing the roughly 7 years since Rust 1.0 was released on May 15 2015 will require ~62 steps. It might be worse, since Rust 1.0 probably cannot be built with just a C89 compiler. I could not quickly find documentation on how to build Rust 1.0.

C can cross decades with two intermediate hops. Rust can cross about a year with 8 intermediate hops and the aid of an external project.

Of course, no discussion of the Rust release process would be complete without at least one dishonorable mention of their recommended install instructions, a curlpipesh:
Code:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20485

PostPosted: Sat Jul 16, 2022 5:26 pm    Post subject: Reply with quote

GDH-gentoo wrote:
C++ is a big language, but it is not necessary to have deep knowledge of every aspect. Probably nobody does anyway :) Documentation is available when knowledge of specific details is required for some project. And there is no need to use every feature in every program. Being big just means that there is a wide array of choices for picking the subset that best fits the program's needs :)
Big also makes it somewhat difficult to approach. Are you familiar with idea of "C+ / Minimal C++ / Sane C++"? After I get caught up with C, I was intending to look in that direction, but I haven't found any resources for a beginner on how to learn C++ and avoid those features. What I've read seems mostly intended for an audience who has a solid understanding of C++.
_________________
Quis separabit? Quo animo?
Back to top
View user's profile Send private message
Leonardo.b
Guru
Guru


Joined: 10 Oct 2020
Posts: 308

PostPosted: Sat Jul 16, 2022 6:57 pm    Post subject: Reply with quote

GDH-gentoo wrote:
Last but not least, I like how the creator of C++ thinks and reasons :)

How?
Back to top
View user's profile Send private message
Goverp
Advocate
Advocate


Joined: 07 Mar 2007
Posts: 2179

PostPosted: Sat Jul 16, 2022 7:49 pm    Post subject: Reply with quote

I once stood next to a coffee-break discussion between Edsger Dijkstra, Bjarne Stroustrup and Donald Knuth. It didn't make me like C++ any more.
_________________
Greybeard
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6780

PostPosted: Sat Jul 16, 2022 10:31 pm    Post subject: Reply with quote

Etal wrote:
mv wrote:
rust bootstrapping is insane: If you want to bootstrap it for a new architecture you have to compile practically every existing rust version in that order - meanwhile probably some hundreds of versions. I almost couldn't believe it when I learnt about it, but it seems to be true: Whenever the rust developers invent a new feature, they hype it that much that it must get used immediately in the next compiler version. All that without having a "minimal rust" for bootstrapping. I would never use/recommend a language developed by people lacking that much common sense.


That's not true. You can bootstrap rust-1.54.0 with mrustc on amd64, so that's 8 versions to rebuild to latest.

Great - somebody else than the original rust developers wrote a more decent rust compiler - and still 8 versions of rebuilding are currently needed. You couldn't prove my point about the lack of common sense of the developers any better.
Quote:
Having a compiler written in the language is not at all unique to rust either

That's not what I complained about. I complained about the hyping of every new corner case feature by immediately using it in the compiler. Decent compilers work with the first (or even more minimal) version of the language exactly because of bootstrapping; more than 2 bootstrap steps are very exceptional.
Quote:
Good luck getting Haskell or OpenJDK bootstrapped.

I don't know about Haskell, but for java (hence java-8) there was icedtea. I haven't examined, but I doubt that the bootstrapping of java itself is the problem but more the complexity of the build system and libraries.
Quote:
Also, gcc-rust is in the works

That's good news, although I somehow doubt that the original rust developers are the driving force for this. My bet is that it will end as gcc-java and apparently now already gcc-go.
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6780

PostPosted: Sat Jul 16, 2022 11:44 pm    Post subject: Reply with quote

GDH-gentoo wrote:
  • It is possible to both write high-level, object-oriented style programs that look like Java, and low-level, procedural-style programs that look like C (see this). Plus this, which I don't know if it is available in any other language, but that's probably advanced use.

Generics are also known in java-8 and implemented there in a much more powerful and simpler way. The latter is not surprising, because java learnt several years from the mistakes made in C++. And in contrast to C++, you could already do the same as generics can do with earlier java versions, because every class inherits from class Object in java; generics are just syntactic sugar which provide you with compile-time errors instead of run-time errors. And as mentioned, this syntactic sugar is relatively easy to handle, and gives clear error messages if you make some mistake. That's a big advantage over C++ where error messages related to templates are usually cryptic, to say the least, and sometimes you get even only run-time instead of compile-time errors. Both does not happen with java generics.
Quote:
  • Shares with C the same execution environment model (only augmented to specify object construction and destruction details).

That's IMHO the main bonus of C++, although it is also somewhat problematic. In particular, unless you write your own memory management (and seriously: who does?), you have malloc calls for every tiny temporary string you use which slows things down. That's part of the reason why java - despite being usually not compiled to plain machine code (except for hotspots with JIT) - is sometimes even faster than C++: No system calls are necessary for memory management (except one at the start), and the built-in memory management is very efficient.
Quote:
  • Has both reference types, which sort of can be used like in Java, and true pointer types like C.

I disagree that reference types can be ”sort of” used like in java: This was the idea for simple function calls, but essentially that is the only case in which it works. Already for return values you have to rely on return value optimization in C++ which is not guaranteed (and in fact does not happen at -O0) to have something which can be “sort of“ used like in java. But the latter does already not work properly anymore for iterated function calls as they happen frequently in connection with operator overload (e.g. if a matrix library is implemented in the naive way).
Quote:

Yes, for code for which every CPU cycle is important, C++ should probably be the language of choice. Although doing it correctly is extremely hard. Cf. the remark about the matrix library: Every composition of expressions must get its own class, or otherwise you cannot avoid getting bazillions of redundant copy operations. In java, this problem does not exist, not only because there is no operator overload (which is not actually the cause of the problem, but the iteration of function calls is), but because all objects are always passed by reference, including in the return value of functions - something which cannot be “emulated” in C++ due to lack of automatic memory management. Return value optimization is only a mild mitigation of this missing concept of the language in some cases.
Quote:
  • Available compilers produce machine code. So no interpreters, no middle layer between code and actual hardware like the Java virtual machine, no nothing

Again, this is only a point concerning speed which in practice is not much worse in java. We speak about a factor of ~2 or less in most cases, and only if the C++ code is written by an experienced programmer with speed in mind. For most application this is irrelevant. And for simple applications (e.g. mainly string manipulations), java can even be faster then C++ when both are used in the idiomatic way (due to the faster memory management with less kernel calls in java. In fact, for simple applications, java will never execute a garbage collection while C++ has to free memory regularly.)
Quote:
C++ is a big language, but it is not necessary to have deep knowledge of every aspect. Probably nobody does anyway

That's an important point which holds in the same way for java: Many aspects of the language are just necessary to write efficient libraries. If you are not a general library author, you do not need to know too much about, e.g. templates. In java, the power-user tool is reflection (which allows to write libraries which you can not even dream of in C++; here is an example: https://www.baeldung.com/introduction-to-autovalue TL;DR: By just using an annotation you get methods of a class “automatically” defined in an idiomatic way, depending on your description of the class). But as a normal java user you will never learn about anything of it and just use such libraries.
Quote:
I would definitely throw away everything that describes anything older than at least C++ 2011.

Indeed, C++-11 should better have been called C+=2: It is practically a new language with ground-up new concepts which also required a complete rewrite of the STL to get a library which does not give the bad surprises from C++-98. Just one example: In C++-98, if you stored an auto_ptr in a vector, it could just happen that the object pointed to was freed (hence you had memory access errors with all the nasty security implication), just because you added something to the vector. The reason was the way how vector and auto_ptr were implemented (and they had to be implemented this way to satisfy the other contract guarantees of the library). But to avoid the problem, you need the clumsy concept of rvalues. The latter falls under the earlier mentioned category: Unless you are a library author, you probably do not have to learn about it. But without this concept (which was introduced only in C++-11), it is not possible to fix the library. The “workaround” previously was to document that you must not store auto_ptr in containers, but the underlying problem was actually much deeper.

OTOH, in practice most people use nowadays reference-counting pointers in C++ which avoid all sort of problems but actually contradicts the "what you don't use you do not pay for" principle: This comes at the price of efficiency, and then speed of java and C++ code comes even more close.
Back to top
View user's profile Send private message
Etal
Veteran
Veteran


Joined: 15 Jul 2005
Posts: 1932

PostPosted: Sun Jul 17, 2022 4:16 am    Post subject: Reply with quote

Hu wrote:
mrustc is an external project run by someone who wanted a less crazy way to bootstrap Rust. Since it is external, it doesn't keep up with the breakneck pace of Rust feature dependence, which is why it is already 8 releases behind. I think it is useful that mrustc offers a way to bootstrap without going all the way to the beginning, but since it is external and does keep falling behind, I can't let its existence excuse the way the core Rust community handles the bootstrap process.

Let's remember GCC was also an external project, run by someone who wanted a Free compiler to bootstrap his operating system. C was created by Bell Labs, not GNU.

Hu wrote:
(Although if I had to pick a single reason to avoid Rust, it's how painful it is to parse. Even reading well commented Rust code where I know from the comments what it should do, I can't see how the line noise on my screen results in the compiler producing the claimed program.)

You could say the same of many C and C++ codebases to an untrained eye.

Hu wrote:
Yes, compilers written in the language they compile are common, but the degree of backward compatibility varies widely. Haskell and Java on Linux have always been a pain in part because of their messy bootstrap process.


In other words, no different than Rust. No, it's worse than rust. Remember how Java had to be bootstrapped when it was open-sourced? Let me remind you: First, you had to build the now-obsolete GNU Compiler for Java, last seen in GCC 6, which could just barely compile Java 1.4. Then you'd use that to build Eclipse's ECJ compiler, to compile Java 5. Then, you'd use the IcedTea build harness, courtesy of RedHat, to build a proper Java 6 JDK. And then you could use that to build OpenJDK 7. Then, one-by-one, you build each successive version until you reach OpenJDK 17 or whatever the current version is. And because a new version of Java now comes out every 6 months, and non-LTS versions get dropped as soon as the new version comes out, Gentoo devs don't even bother to package the those versions. If you wanted to build these versions, you would run into typical bit-rot issues afflicting C/C++ codebases - code which newer, ever-stricter versions of gcc refuse to compile, dependencies on headers which were deprecated and subsequently removed, automake scripts that get confused when a dependency version number is greater than 9…

Hu wrote:
Compare this to gcc, which historically could be built with any C89 compiler you had on hand, and would then handle bootstrapping itself the rest of the way.


Historically speaking, GCC 1.0 was released on 1987, that's 12 years before C89 became a thing. And, oh yes, and you needed a C compiler to build it. There were already a number of proprietary ones available.

GCC 1.0 came out 9 years after K&R published their book. In comparison, Rust 1.0 came out 7 years ago. There's still time development to slow down and for third-party compiler writers to catch up.

Hu wrote:
Even now, a fairly current gcc (gcc-10) only requires an mpfr from 2011 or newer. According to Prerequisites for GCC:
  • <gcc-4.8 could be bootstrapped with a C89 compiler. =gcc-4.8.0 was released 2013-03-22. This suggests you could use a compiler from the early 1990s up through 2012 (gcc 4.7 released 2012-03-22) and expect it to work.
  • <gcc-11 could be bootstrapped with a C++98 compiler. =gcc-11.1 was released 2021-04-27. =gcc-10.x saw gcc-10.4 released 2022-06-28, and presumably still can be built with a C++98 compiler.
  • >=gcc-11 requires a C++11 compiler, so you will need something from within the roughly 10 years before gcc-11 was released.
If I read that right, you can go from a bare C89 compiler (if you can even find one that lacks everything newer anymore) to gcc-4.7. gcc-4.7 had substantial, but not total, support for C++11, so let's call it only a C99/C++98 compiler and not a C++11 compiler. Using gcc-4.7, you can then build gcc-10, which gets you a full C++11 compiler. Per the prerequisites page, a C++11 compiler can build gcc-11 and later, with no upper bound documented, which presumably means that gcc-13 (still in development) can be built using a C++11 compiler. Therefore, using any random C89 compiler you can find, you can perform two intermediate hops to reach the cutting edge compiler, crossing more than 2 decades of development. Compare that to Rust, where the official position is that you usually need the most recent release. As I write this, Rust 1.62 is current, and was released June 30 2022. The preceding release, Rust 1.61 was released May 19 2022. The most recent Rust that mrustc can handle is 8 versions ago, and goes back only to July 29 2021.


I'm sure by the time Rust is standardized, it will be possible to hop versions as well. Give it a decade, after all, by the time ANSI C standard was adopted, C has already existed for 17 years.

And did I mention that an alternate Rust implementation is on track to be included in GCC 13?

Hu wrote:
If you want to avoid mrustc and use the official bootstrap process, then crossing the roughly 7 years since Rust 1.0 was released on May 15 2015 will require ~62 steps. It might be worse, since Rust 1.0 probably cannot be built with just a C89 compiler. I could not quickly find documentation on how to build Rust 1.0.


You didn't find it because you can't. If you want to build Rust 1.0, you have to go through all the pre-releases. Then, you have to dig through the pre-history repo, back to when it was a one-man project, before it was even backed by Mozilla. Eventually, you will find a proto-Rust compiler written OCaml.

Hu wrote:
C can cross decades with two intermediate hops. Rust can cross about a year with 8 intermediate hops and the aid of an external project.

See above.

Hu wrote:
Of course, no discussion of the Rust release process would be complete without at least one dishonorable mention of their recommended install instructions, a curlpipesh:
Code:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh


How'd you get your C compiler? Let me guess, you downloaded it as part of a stage tarball and then extracted it onto your root partition. Possibly over plaintext because SSL wasn't widespread back then.
Back to top
View user's profile Send private message
Dr.Willy
Guru
Guru


Joined: 15 Jul 2007
Posts: 547
Location: NRW, Germany

PostPosted: Sun Jul 24, 2022 2:19 pm    Post subject: Re: Safe bet statically typed languages? Reply with quote

kukibl wrote:
I initially considered to keep it simple and go with the plain C, however, I am not sure I would actually ever go low-level and use it's manual memory management features (or downsides).
[…]
I see there's a huge "hype" around Rust, but it is difficult for me to make a distinction between PR and actual technical advantages. Additionally, language seem intimidatingly enormous (in terms of size - C++ way).

Off the top of my head, there seems to be a significant overlap of statically-typed and manually-managed-memory languages.
Static typing has changed in the last quite a bit over the last decade however. 20 years ago, most of the statically typed main-stream languages would have C-style typing, nowadays languages seem to be designed around Hindley-Milner type systems popularized by ML.
Since you mentioned Lisp and Ruby, functional programming does not appear to be alien to you. Perhaps you would be interested in Haskell?
It has a quite extensive static type-system while also being memory-managed. Haskell also has been around for long enough that good literature should be available.

If you would like to go imperative, consider having a look at Zig. It should give you a similar experience to C, both in terms of being low-level and "small" but without the flint-stone vibe.

As for Rust my opinion is that the hype is justified, however many of the things that make Rust great are also pretty much exclusive to Rust - not neccessarily what I'd want from my first foray into static typing. It is also a rather large language. Not quite C++ large, but still.

Also lol @ the discussion about Rust bootstrapping in a thread where a guy wants a recommendation for a hobby language.
_________________
gentoo repos: kakoune | oil | hyper-v
Back to top
View user's profile Send private message
carcajou
Apprentice
Apprentice


Joined: 10 Jun 2008
Posts: 248

PostPosted: Tue Aug 02, 2022 1:03 pm    Post subject: Reply with quote

Guys, thank you very much for all the feedback and suggestions.

I will go with either Go or Haskell (actually an interesting proposal I haven't thought about before).
Back to top
View user's profile Send private message
eohrnberger
Apprentice
Apprentice


Joined: 09 Dec 2004
Posts: 250

PostPosted: Thu Dec 01, 2022 12:55 am    Post subject: Reply with quote

The most useful capabilities in an IDE / programming language that I've experienced is the ability to set a break point on a line of code, see that that line of code wasn't resulting in what you needed from that line of code, backup the 'next statement to be executed' (IP) a few lines, revise the code below that point, and step forward a line at a time through the revised code to see if the code changes you've made get the results that you need.

If not, backup the 'next statement to be executed' (IP) a few lines, tweak the code below that point again, and then single step to see if your code changes get the results you need.

An 'immediate' window, where you can type / copy paste in lines of code, using the variables currently in the debugger's scope, to see what the results of those lines of code would result in.

Rinse, lather repeat as needed. All rather 'interactive' if you will.

This isn't a typical capability of complied languages, and you have to make code changes, save the source file, recompile, and re-execute to your break point and try again. What I'm describing eliminates those intermediate steps, which may or may not be available in an interpreted programming language.

What I'd be looking for is a programming language / IDE for Linux which would harken back to MS Office's VBA or Visual Basic version 3 or version 3.5, where what I describe above was the common place method of development and refining code to do what you needed.

That and easy language syntax and robust and easy to understand object model would seem to be a winner. Throw in interoperability to multiple desktop applications and their automation, well, that'd be a more than generous bonus, wouldn't it?

Nothing like that available in Linux at all? Anyone? I surely don't know of one, but where should I look?
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Gentoo Chat All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum