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

Chapter 1: An Introduction to Crystal

Crystal is a safe, performant, general-purpose, and object-oriented language. It was heavily inspired by Ruby's syntax and Go's and Erlang's runtimes, enabling a programmer to be very productive and expressive while creating programs that run efficiently on modern computers.

Crystal has a robust type system and can compile to native programs. Consequently, most programming errors and mistakes can be identified at compile time, giving you, among other things, null safety. Having types doesn't mean you have to write them everywhere, however. Crystal relies on its unique type interference system to identify the types of almost every variable in the program. Rare are the situations where the programmer has to write an explicit type somewhere. But when you do, union types, generics, and metaprogramming help a lot.

Metaprogramming is a technique where a structured view of the written program is accessed and modified by the program itself, producing new code. This is a place where Ruby shines with all its dynamism and built-in reflection model, and so does Crystal, in its own way. Crystal is capable of modifying and generating code during compilation time with macros and a zero-cost static reflection model. It feels like a dynamic language in every way, but it will compile the program down to pure and fast machine code.

Code written in Crystal is expressive and safe, but it's also fast – really fast. Once built, it goes head to head with other low-level languages such as C, C++, or Rust. It beats pretty much any dynamic language and some compiled languages too. Although Crystal is a high-level language, it can consume C libraries with no overhead, the lingua franca of system programming.

You can use Crystal today. After 10 years of intense development and testing, a stable and production-ready version was released in early 2021. Alongside it, a complete set of libraries (called "shards") are available, including web frameworks, database drivers, data formats, network protocols, and machine learning.

This chapter will introduce a brief history of the Crystal language and present some of its characteristics regarding performance and expressiveness. After that, it will bring you up to speed by explaining how to create and run your first Crystal program. Finally, you will learn about some of the challenges for the future of the language.

In particular, we will cover the following topics:

  • A bit of history
  • Exploring Crystal's expressiveness
  • Crystal programs are also FAST
  • Creating our first program
  • Setting up the environment

This should get you started on what Crystal is, understanding why it should be used, and learning how to execute your first program. This context is essential for learning how to program in Crystal, going from small snippets to fully functional and production-ready applications.

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