Title
The Crystal Programming Language
Go Home
Category
Description
A language for humans and computers
Address
Phone Number
+1 609-831-2326 (US) | Message me
Site Icon
The Crystal Programming Language
Page Views
0
Share
Update Time
2022-05-04 00:46:32

"I love The Crystal Programming Language"

www.crystal-lang.org VS www.gqak.com

2022-05-04 00:46:32

Home Forum Blog Sponsors Community Conference Team Docs GitHub Home Forum Blog Sponsors Community Conference Team Docs GitHub menu crystal A language for humans and computers Install Learn Try Online Fund Crystal and help it keep growing at Latest release 1.4.1 22 April 2022 - More release notes Syntax Crystal’s syntax is heavily inspired by Ruby’s, so it feels natural to read and easy to write, and has the added benefit of a lower learning curve for experienced Ruby devs. # A very basic HTTP serverrequire "http/server"server = HTTP::Server.new do |context| context.response.content_type = "text/plain" context.response.print "Hello world, got #{context.request.path}!"endputs "Listening on http://127.0.0.1:8080"server.listen(8080) Start learning Crystal with the Language Reference Type system Crystal is statically type checked, so any type errors will be caught early by the compiler rather than fail on runtime. Moreover, and to keep the language clean, Crystal has built-in type inference, so most type annotations are unneeded. def shout(x) # Notice that both Int32 and String respond_to `to_s` x.to_s.upcaseendfoo = ENV["FOO"]? || 10typeof(foo) # => (Int32 | String)typeof(shout(foo)) # => String Read more about Crystal's type system Null reference checks All types are non-nilable in Crystal, and nilable variables are represented as a union between the type and nil. As a consequence, the compiler will automatically check for null references in compile time, helping prevent the dreadful billion-dollar mistake. if rand(2) > 0 my_string = "hello world"endputs my_string.upcase Running the previous file: $ crystal hello_world.crError in hello_world.cr:5: undefined method 'upcase' for Nil (compile-time type is (String | Nil))puts my_string.upcase ^~~~~~ Macros Crystal’s answer to metaprogramming is a powerful macro system, which ranges from basic templating and AST inspection, to types inspection and running arbitrary external programs. class Object def has_instance_var?(name) : Bool {{ @type.instance_vars.map &.name.stringify }}.includes? name endendperson = Person.new "John", 30person.has_instance_var?("name") #=> trueperson.has_instance_var?("birthday") #=> false Read more about macros Concurrency Model Crystal uses green threads, called fibers, to achieve concurrency. Fibers communicate with each other using channels, as in Go or Clojure, without having to turn to shared memory or locks. channel = Channel(Int32).newtotal_lines = 0files = Dir.glob("*.txt")files.each do |f| spawn do lines = File.read_lines(f) channel.send lines.size endendfiles.size.times do total_lines += channel.receiveendputs total_lines Read more about Crystal's concurrency model C-bindings Crystal has a dedicated syntax to easily call native libraries, eliminating the need to reimplement low-level tasks. # Fragment of the BigInt implementation that uses GMP@[Link("gmp")]lib LibGMP alias Int = LibC::Int alias ULong = LibC::ULong struct MPZ _mp_alloc : Int32 _mp_size : Int32 _mp_d : ULong* end fun init_set_str = __gmpz_init_set_str(rop : MPZ*, str : UInt8*, base : Int) : Int fun cmp = __gmpz_cmp(op1 : MPZ*, op2 : MPZ*) : Intendstruct BigInt