Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Crystal Programming

You're reading from   Crystal Programming A project-based introduction to building efficient, safe, and readable web and CLI applications

Arrow left icon
Product type Paperback
Published in May 2022
Publisher Packt
ISBN-13 9781801818674
Length 356 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
George Dietrich George Dietrich
Author Profile Icon George Dietrich
George Dietrich
Guilherme Bernal Guilherme Bernal
Author Profile Icon Guilherme Bernal
Guilherme Bernal
Arrow right icon
View More author details
Toc

Table of Contents (26) Chapters Close

Preface 1. Part 1: Getting Started
2. Chapter 1: An Introduction to Crystal FREE CHAPTER 3. Chapter 2: Basic Semantics and Features of Crystal 4. Chapter 3: Object-Oriented Programming 5. Part 2: Learning by Doing – CLI
6. Chapter 4: Exploring Crystal via Writing a Command-Line Interface 7. Chapter 5: Input/Output Operations 8. Chapter 6: Concurrency 9. Chapter 7: C Interoperability 10. Part 3: Learn by Doing – Web Application
11. Chapter 8: Using External Libraries 12. Chapter 9: Creating a Web Application with Athena 13. Part 4: Metaprogramming
14. Chapter 10: Working with Macros 15. Chapter 11: Introducing Annotations 16. Chapter 12: Leveraging Compile-Time Type Introspection 17. Chapter 13: Advanced Macro Usages 18. Part 5: Supporting Tools
19. Chapter 14: Testing 20. Chapter 15: Documenting Code 21. Chapter 16: Deploying Code 22. Chapter 17: Automation 23. Other Books You May Enjoy Appendix A: Tooling Setup 1. Appendix B: The Future of Crystal

Exploring Crystal's expressiveness

It is often said that Crystal is a language for humans and computers. This is because Crystal strives for a balance of being a surprisingly enjoyable language for programmers while also being very performant for machines. One cannot go without the other, and in Crystal, most abstractions come with no performance penalties. It has features and idioms such as the following:

  • Object-oriented programming: Everything is an object. Even classes themselves are objects, that is, instances of the Class. Primitive types are objects and have methods, too, and every class can be reopened and extended as needed. In addition, Crystal has inheritance, method/operator overloading, modules, and generics.
  • Static-typed: All variables have a known type at compile time. Most of them are deduced by the compiler and not explicitly written by the programmer. This means the compiler can catch errors such as calling methods that are not defined or trying to use a value that could be null (or nil in Crystal) at that time. Variables can be a combination of multiple types, enabling the programmer to write dynamic-looking code.
  • Blocks: Whenever you call a method on an object, you can pass in a block of code. This block can then be called from the method's implementation with the yield keyword. This idiom allows all sorts of iterations and control flow manipulation and is widespread among Ruby developers. Crystal also has closures, which can be used when blocks don't fit.
  • Garbage collection: Objects are stored in a heap, and their memory is automatically reclaimed when they are no longer in use. There are also objects created from a struct, allocated in the stack frame of the currently executing method, and they cease to exist as soon as the method finishes. Thus, the programmer doesn't have to deal with manual memory management.
  • Metaprogramming: Although Crystal isn't a dynamic language, it can frequently behave as if it were, due to its powerful compile-time metaprogramming. The programmer can use macros and annotations, together with information about all existing types (static reflection) to generate or mutate code. This enables many dynamic-looking idioms and patterns.
  • Concurrent programming: A Crystal program can spawn new fibers (lightweight threads) to execute blocking code, coordinating with channels. Asynchronous programming becomes easy to reason and follow. This model was heavily inspired by Go and other concurrent languages such as Erlang.
  • Cross-platform: Programs created with Crystal can run on Linux, macOS, and FreeBSD, targeting x86 or ARM (both 32-bit and 64-bit). This includes the new Apple Silicon chips. Support for Windows is experimental, it isn't ready just yet. The compiler can also produce small static binaries on each platform without dependencies for ease of distribution.
  • Runtime safety: Crystal is a safe language – this means there are no undefined behaviors and hidden crashes such as accessing an array outside its bounds, accessing properties on null, or accessing objects after they have already been freed. Instead, these become either runtime exceptions, compile-time errors, or can't happen due to runtime protections. The programmer has the option of weaving safety by using explicitly unsafe features of the language when necessary.
  • Low-level programming: Although Crystal is safe, using unsafe features is always an option. Things such as working with raw pointers, calling into native C libraries, or even using assembly directly are available to the brave. Many common C libraries have safe wrappers around them ready to use, allowing them to use their features from a Crystal program.

At first glance, Crystal is very similar to Ruby, and many syntactic primitives are the same. But Crystal took its own road, taking inspiration from many other modern languages such as Go, Rust, Julia, Elixir, Erlang, C#, Swift, and Python. As a result, it keeps most of the good parts of Ruby's slick syntax while providing changes to core aspects, such as metaprogramming and concurrency.

You have been reading a chapter from
Crystal Programming
Published in: May 2022
Publisher: Packt
ISBN-13: 9781801818674
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image