Crystal (programming language): Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 1:
'''Crystal''' is a [[general-purpose programming language|general-purpose]], [[object-oriented programming language|object-oriented]] programming language designed and developed by Ary Borenszweig and Juan Wajnerman and over one-hundred listed contributors.<ref>[https://github.com/manastech/crystal Crystal source code]</ref> Crystal is developed as open source andsoftware its(under syntaxthe is[[Apache License|Apache License]], Version 2.0) with syntax inspired by [[Ruby (programming language)|Ruby]]. The language is statically type-checked but withoutdoes havingnot torequire specifythat the type of variables or method arguments be specified. This is the result of advanced global [[Type inference|type inference]].<ref>[http://crystal-lang.org/2013/09/23/type-inference-part-1.html Type inference part 1]</ref> Its first official release was released in June 2014.<ref>[http://motion-express.com/blog/why-use-crystal-lang Why Crystal programming language?]</ref><ref>[http://crystal-lang.org/2014/06/19/crystal-0.1.0-released.html Crystal 0.1.0 released!]</ref> While the original Crystal compiler was written in Ruby, in 2013 a new compiler written using the Crystal programming language itself was released.<ref>[http://crystal-lang.org/2013/11/14/good-bye-ruby-thursday.html Good-bye Ruby Thursday]</ref> The current release version is 0.15 and the language is in an active development phase.
 
==Description==
Although resembling the Ruby programming language in syntax, Crystal compiles to much more efficient native code using an [[LLVM]] backend, at the cost of disallowing the dynamic aspects of Ruby. However, the advanced global type inference used by the Crystal compiler, combined with the usage of [[Union type|union types]], give Crystal the feel of a higher-level scripting language than many other static programming languages. Recent benchmarks have demonstrated that Crystal has a performance broadly similar to [[C (programming language)|C]] for a wide range of computing tasks.<ref>[https://github.com/kostya/benchmarks Some benchmarks of different languages]</ref><ref>[https://github.com/kostya/crystal-benchmarks-game Crystal implementations for The Computer Language Benchmarks Game]</ref><ref>[https://github.com/smarr/are-we-fast-yet/tree/master/benchmarks/Crystal Are We Fast Yet?]</ref> The language has automated garbage collection and currently offers a [[Boehm garbage collector|Boehm collector]]. Crystal possesses a macro system and supports generics and method and operator overloading. Crystal's concurrency model is inspired by [[Communicating sequential processes|communicating sequential processes]] (CSP), and implements light-weight fibers and channels (for communicating between fibers) inspired by the [[Go (programming language)|Go programming language]].
 
== Examples ==
Line 43:
server.listen
</syntaxhighlight>
 
===Type Inference and Union Types===
 
In the following code sample, there is no need to specify the type of method argument '''''a''''', which will be inferred to be of a union type of all types provided to the '''''print_max''''' function.
 
<syntaxhighlight lang="ruby">
def print_max(*a)
# The splat '*' indicates a vararg and max is a varidic function
# that takes a tuple of any size.
puts("#{a.max}") # Notice the use of string interpolation
end
 
print_max(5, 6, 3, 4.3, 9, 10, 7.9) # type of Array(Float64 | Int32)
print_max("lion", "rhinoceros", "zebra", "elephant") # type of Array(String)
print_max('1', 'a', 'i', '9') # type of Array(Char)
</syntaxhighlight>
 
The code sample above prints 10, zebra, and i.
 
===Concurrency===