Programming languages

I didn’t know what this was, so I looked it up:

Abstract: Crystal is a general-purpose, object-oriented programming language. With syntax inspired by Ruby, it’s a compiled language with static type-checking. Types are resolved by an advanced type inference algorithm.

4 Likes

Yep, all the beauty of Ruby, but better performance.

3 Likes

No wonder it wasn’t on my radar. I’ve never really dug into Ruby, so I hadn’t seen anything on Crystal prior to the discussion here. It appears that a lot of features are crammed into some of the statements and keywords because the code example is so much shorter than the comparison code.

3 Likes

Ruby became somewhat well known from Ruby on rails.

“Ruby was intentionally designed to be a beautiful, elegant, and human-centric language. Its creator, Yukihiro “Matz” Matsumoto, famously prioritized developer happiness and productivity over machine optimization, striving to create a language that felt natural and joyful to read and write”

Crystal code can be surprisingly compact. But once you get used to using those features, you really miss them when you are trying to figure out how to do the same job in another language.

3 Likes

Yeah, that explains for me; I didn’t do anything with Ruby on Rails, but Crystal does sound interesting to me; I’ll see if that translates into testing it out. It does. I tried going to the Crystal site and I typed in the gigasecond program. I don’t think I had any errors in typing; I carefully typed in each line, but one of them delivered an error in the compile test server on the app page; it was 1.000000000 so unless I counted wrong or that’s too many digits for the test program, something was off.

I did also download the image and put it on MX Linux for now; I set it up, but I haven’t done any more; it’ll be there when I can spare more time.

3 Likes

I never got into the Ruby on rails thing either, but at some point I was looking at different languages to compare performance with perl, and so I looked into Ruby, Python, Julia, command-line php, and others. Ruby was fun to program with.

I’m running MX 25, but the Crystal in the Debian repos is pretty old, so I got the latest package direct from the crystal site.

That gigasecond program works for me when I add some test code:

[jjs@bella crystal-test]$ cat gigasecond.cr
module Gigasecond
def self.from(start : Time) : Time
start + Time::Span.new(seconds: 1000000000)
end
end

p “1 gigasecond after 2011-04-25:”
p Gigasecond.from(Time.utc(2011, 4, 25))
[jjs@bella crystal-test]$ crystal gigasecond.cr
“1 gigasecond after 2011-04-25:”
2043-01-01 01:46:40Z
[jjs@bella crystal-test]$

2 Likes

Thanks for those details; I’m sure that makes quite a difference.

2 Likes

In Go(d) We (T)Rust

I started a few months ago studying and using Go. Never used it before. I remember some 15 years ago I read something about it but I didn’t find it interesting. Well maybe in the last years there have been improvements. Now I do like it very much. It’s so easy to use and it works well.

Now I’m curious about Rust. I read a lot in the last days and I was enthusiast. I thought: maybe this one is even better than Go, since in Go there are a few things missing. I like about Rust that one can create a hierarchy of modules and submodules inside a single file. And I like the “pre-processor” sort of instructions one can put in the code. I thought I could use Rust instead of Go.

But then I learned something very odd about Rust. I’m not sure if I understood it well… There in no inheritance in Rust… ?

Rust does not have class-based inheritance; instead, it uses traits for shared behavior and composition for data reuse.

I mean, if a struct embeds another one, it actually doesn’t: the base struct is just a regular field inside the second, the outer. There is no inheritance.

Structs can be reused as fields of another struct

This is not very elegant, I think. All methods of the base struct do not become methods of the outer struct. So I can’t call those methods in the outer struct. I must reimplement all the methods in the second struct?

Well, I decided Go is better. In Go I Trust.

4 Likes

So, in essence, like Ada does, but without the type enforcement?

2 Likes

I’ve been running native http benchmarks on various languages, and the httpd implementation in Go is among the very fastest.

Crystal also has a surprisingly fast httpd implementation.

In going through the drills on exercism, I’ve learned about crystal macros, which can remove drudgery by generating boilerplate code, and monkey patching, that lets you easily extend a data type - for instance, one of the exercises was to extend the integer type with a “to_roman” method, so:

3999.to_roman = MMMCMXCIX

4 Likes

I don’t know Ada. It was the first language I studied at school. I was never interested in Ada or Pascal… since I discovered C. When I learned C it was like a shock, I thought: this is amazing. I only like C similar languages. But I don’t like C++. There are so many “C++ frustrated programmers” and that is why they invented Go and Rust. I never liked using C++. Go is much better.

5 Likes

@goatherd

I like about Rust that one can create a hierarchy of modules and submodules inside a single file.

The Julia programming language allows this too, but one has to be alert when reading the code to make sure you understand the scoping rules. I.e. what functions and/or variables are available at a higher level.

I prefer to keep modules as standalone as possible. They should have a specific purpose and role to play, like miniature libraries.

Obviously one sometimes needs to import or include a module from another module to bring into scope some of the functionality provided by that module. This type of hierarchy is good, but maybe it’s just me, I dislike seeing nested modules in code (same for nested functions). I would much rather see imports declared at the top of the file, so that one gets a clear picture of the overall structure. In fact all the scoping and availability IMHO should be at the top of the module. (I am totally arrogant and opinionated.)

Although I haven’t used Rust in earnest, it seems like a very good language to learn.

2 Likes

This is good to know. Both of these efforts have recently made a positive impression on me as I’ve read various reports and articles here. Of the two, I’m probably more likely to use Go; in fact, if I can find one, I may copy in a Clock program and if one’s not out there, I may build one to get a little practice and code samples with the Go language.

3 Likes

I learned C++ & OO in college (I am old).

I am not a programmer, so after college, I quickly adopted Perl as the scripting language of choice.

Today, I use bash & Python.

I am a functional scripter, so I write functions (no OO for me).

6 Likes

I’m still learning crystal, almost halfway through the exercises on exercism.com -

I’ve been impressed by crystal’s macros, and the advanced functions for processing arrays and strings. Several times I’d been trying to think of how to create an algorithm to process some input and found that there was a crystal function for that.

For instance, there was a problem that need to process an input string in a sliding window. After wracking my brain, I found the function str.each_char.each_cons(n) does exactly what’s needed, no assembly required.

4 Likes