// HACKER NEWS — CYBERSECURITY
A few good ideas in programming languages
Here are just a few programming language features I love:
A great idea that I first encountered in Crystal, is the idea of flow typing.
Crystal is a compiled programming langauge with static type-checking with syntax very similar to the Ruby programming language. Something that keeps Crystal feeling like a dynamically typed language is how a variable can be assigned to multiple types throughout the course of its lifetime, unlike most statically typed languages I've used. Here is an example:
What's most interesting about this is that there is some time when my_var is just a Int32, there is a region where it is guaranteed to be a String and then there is a region where the compiler cannot actually guarantee one or the other... so its type is the union of all the possibilities. Now if you try to run a String method on my_var, it'll fail because its not a String, its a Int32 | String and the compiler will force you to add a check like if my_var.is_a?(String) which narrows the possible types to just a String.
This is a great example of using fancy type inference to make a compiled language feel dynamic without paying much of a runtime penalty.
Typescript also has flow typing and type narrowing!
Rust is a systems programming language which guarantees memory safety without a garbage collector.
One large class of memory safety bugs in concurrent programs is the loathsome data race: when multiple threads read and write the same memory location simultaneously without synchronization.
The borrow checker is how Rust is able to statically prevent data races at compile-time. It enforces the following rules:
This might remind you of a readers-writer lock which is a lock which allows many readers OR a singular writer. This is because to prevent data races, we only need to synchronize reads with respect to writes. The point of concurrent synchronization is to serialize the writes to a given memory location.