Crystal (programming language): Difference between revisions

Content deleted Content added
New Crystal programming logo source on Wikipedia with fair use
not needed when the title already contains the disambiguation
 
(124 intermediate revisions by 84 users not shown)
Line 1:
{{Short description|Object-oriented programming language}}
{{Infobox programming language
| name = Crystal
| logo = Crystal language logo.svg
|logo = The_Crystal_programming_language_logo.svg
| paradigm = [[Multi-paradigm programming language|Multi-paradigm]]: [[Object-oriented programming|object-oriented]], [[Concurrent computing|concurrent]]
|logo size = 260px
| released = {{Start date and age|2014|06|19}}<ref>{{Cite web|url=https://crystal-lang.org/2014/06/19/crystal-0.1.0-released.html|title=Crystal 0.1.0 released!|website=crystal-lang|date=19 June 2014 }}</ref>
| paradigm = [[Multi-paradigm programming language|Multi-paradigm]]: [[Object-oriented programming|Object-oriented]]
| designer = Ary Borenszweig, Juan Wajnerman, Brian = Ary BorenszweigCardiff
| developer = Manas Technology Solutions
| latest previewrelease version = 0.19.4{{wikidata|property|preferred|references|edit|P348|P548=Q2804309}}
| latest previewrelease date = {{Start date and age|2016{{wikidata|10qualifier|07preferred|single|P348|P548=Q2804309|P577}}|df=yes}}
| typing = [[Type system#Static type =checking|static]], [[Type system#StaticInferred type checking|staticinferred]], [[Nominal type system|nominal]], [[Duck_typing|duck]]
| influenced by = [[Ruby (programming language)|Ruby]], [[Go (programming language)|Go]]<ref name="go multithread" Crystal/>
| programming language = Crystal
| platform = '[[IA-32]]' (i386), '[[x86-64]]'
| operating system platform = [[OS XIA-32]] (i386), [[Linuxx86-64]], [[FreeBSDAArch64]]<ref name="platform-support" />
| operating system = [[Linux]], [[macOS]], [[FreeBSD]], [[OpenBSD]], [[Microsoft Windows|Windows]]<ref name="platform-support" />
| license = [[Apache License]] 2.0
| license = [[Apache License 2.0]]
| file ext = .cr
| file ext = .cr
| website = {{URL|https://crystal-lang.org/}}
| website = {{URL|https://crystal-lang.org}}}}
| influenced by = [[Ruby (programming language)|Ruby]],<ref name="rel_0.18.0"/> [[C (programming language)|C]], [[Rust (programming language)|Rust]], [[Go (programming language)|Go]],<ref name="rel_0.18.0"/> [[C Sharp (programming language)|C#]],<ref name="rel_0.18.0"/> [[Python (programming language)|Python]]<ref name="rel_0.18.0"/>
}}
 
'''Crystal''' is a [[high-level programming language|high-level]] [[general-purpose programming language|general-purpose]], [[object-oriented programming language|object-oriented]] programming language, designed and developed by Ary Borenszweig and, Juan Wajnerman, Brian Cardiff and overmore one-hundredthan listed400 contributors.<ref>[https://github.com/manastech/crystal Crystalname="contributors" source code]</ref> CrystalWith issyntax developedinspired as open source software (underby the language [[ApacheRuby License(programming language)|Ruby]],<ref Version 2name="rel_0.18.0)"/> withit syntaxis inspired bya [[Ruby (programmingcompiled language)|Ruby]]. Thewith language[[Type issystem#Static staticallytype checking|static type-checkedchecking]], but does not require thatspecifying the typetypes of variables or method arguments beis generally specifiedunneeded. ThisTypes isare theresolved resultby ofan advanced global [[type inference]] algorithm.<ref>[http://crystal-lang.org/2013/09/23/ name="type-inference-part-1.htmli" Type/><ref inferencename="devm" part 1]</ref> TheCrystal language is in an active development phase.
is currently in active development. It is released as [[free and open-source software]] under the [[Apache License]] version 2.0.
 
== History ==
Work on the language began in June 2011,<ref name="story"/> with the purposeaim of creating a language which hadmerging the elegance and productivity of Ruby andwith the speed, efficiency, and type safety of a compiled language.<ref name="why_crystal"/><ref name="story"/> Initially callednamed ''Joy'', it was quickly renamed to ''Crystal''.<ref name="story"/>
 
In November 2013, theThe Crystal compiler (previouslywas first written in Ruby), becamebut later rewritten in Crystal, thus becoming [[Self-hosting (compilers)|self-hosting]], {{as of|November 2013|lc=true}}.<ref name="goodbye_ruby"/> The first official version was released in June 2014.<ref name="rel_0.1.0"/> In July 2016, Crystal was added tojoined the [[TIOBE index|TIOBE Index]].
 
== 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 disallowingprecluding the dynamic aspects of Ruby. However, theThe advanced global type inference used by the Crystal compiler, combined with the usage of [[union type]]s, givegives Crystalit more the feel of a higher-level scripting language than many other comparable 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 languageIt has automated garbage collection and currently offers a [[Boehm garbage collector|Boehm collector]]. Crystal possesses a macro system and supports generics andas well as method and operator overloading. Crystal'sIts concurrency model is inspired by [[communicating sequential processes]] (CSP), and implements light-weightlightweight fibers and channels (for communicatinginterfiber between fiberscommunication), inspired by the [[Go (programming language)|Go programming language]].<ref name = "rel_0go multithread">{{cite web | url=https://forum.18crystal-lang.0"org/t/crystal-multithreading-support/6622/19 | title=Crystal multithreading support | date=23 February 2024 }}</ref>
 
== Examples ==
=== Hello World ===
This is the simplest way to write the [["Hello, World!" program|Hello World]] program in Crystal:
 
<syntaxhighlight lang="crystal">
=== [[Hello World]] ===
This is the simplest way to write the Hello World program in Crystal:
 
<syntaxhighlight lang="ruby">
puts "Hello World!"
</syntaxhighlight>
The same as in Ruby.
 
Or using an [[object-oriented programming]] style:
 
<syntaxhighlight lang="rubycrystal">
class Greeter
def initialize(@name : String)
@name = name
end
 
Line 54:
</syntaxhighlight>
 
=== HTTP Serverserver ===
<syntaxhighlight lang="crystal">
 
<syntaxhighlight lang="ruby">
require "http/server"
 
server = HTTP::Server.new(8080) do |context|
context.response.content_type = "text/plain"
context.response.print "Hello world! The time is #{Time.nowlocal}"
end
 
server.bind_tcp("0.0.0.0", 8080)
puts "Listening on http://0.0.0.0:8080"
server.listen
</syntaxhighlight>
 
===Type InferenceTCP andecho Unionserver Types===
<syntaxhighlight lang="crystal">
require "socket"
 
def handle_client(client)
message = client.gets
client.puts message
end
 
server = TCPServer.new("localhost", 1234)
while client = server.accept?
spawn handle_client(client)
end
</syntaxhighlight>
 
=== Type inference and union types ===
The following code defines an array containing different types with no usable common ancestor. Crystal automatically creates a union type out of the types of the individual items.
 
<syntaxhighlight lang="rubycrystal">
desired_things = [:unicorns, "butterflies", 1_000_000]
p typeof(desired_things.first) # typeof returns the compile time type, here (Int32Symbol | String | SymbolInt32)
p desired_things.first.class # the class method returns the runtime type, here Symbol
</syntaxhighlight>
 
=== Concurrency ===
Channels can be used to communicate between fibers, which are initiated using the keyword <code>spawn</code>.
 
<syntaxhighlight lang="crystal">
Channels can be used to communicate between fibers, which are initiated using the 'spawn' keyword.
 
<syntaxhighlight lang="ruby">
channel = Channel(Int32).new
 
Line 100 ⟶ 114:
</syntaxhighlight>
 
==References Adoption ==
 
In 2020, it was reported that the infotainment units in vehicles produced by [[Nikola Corporation]] were written in Crystal.<ref>{{cite web |last1=Pettinati |first1=Martin |title=Nikola Motor Company: Crystal powered dashboards on the trucks of the future {{!}} Manas.Tech |url=https://manas.tech/blog/2020/02/11/nikola-motor-company/ |website=Manas Technology Solutions |language=en |date=11 February 2020}}</ref> Much of the backend of the [[Kagi (search engine)|Kagi search engine]] is written with Crystal.<ref>{{cite web |title=Zac Nowicki – Tales from Kagi {{!}} CrystalConf 2023 |url=https://www.youtube.com/watch?v=r7t9xPajjTM |date=13 November 2023}}</ref>
 
== Further reading ==
{{Refbegin}}
* {{citation
| first1 = Simon
| last1 = St. Laurent
| first2 = Ivo
| last2 = Balbaert
| date = February 1, 2019
| title = Programming Crystal
| edition = P1.0
| publisher = [[Pragmatic Bookshelf]]
| isbn = 978-1-68050-286-2
| url = https://pragprog.com/book/crystal/programming-crystal
}}
* {{citation
| first1 = George
| last1 = Dietrich
| first2 = Guilherme
| last2 = Bernal
| date = May 27, 2022
| title = Crystal Programming
| publisher = [[Packt Publishing]]
| isbn = 978-1801818674
}}
* {{citation
| first1 = Ramon
| last1 = Wartala
| date = March 2016
| title = Die Ruby-artige Programmiersprache Crystal
| trans-title = The Ruby-like programming language Crystal
| work = [[Linux Magazine|Linux Magazin]]
| issue = 3/2016
| issn = 1432-640X
| language = de
| url = https://www.linux-magazin.de/ausgaben/2016/03/crystal/
}}
{{Refend}}
 
== References ==
{{Reflist|refs=
<ref name="platform-support">
{{cite web
| url = https://crystal-lang.org/reference/platform_support.html
| title = Crystal Platform Support
| website = crystal-lang.org
}}
</ref>
<ref name="contributors">
{{cite web
| url = https://github.com/crystal-lang/crystal/graphs/contributors
| title = Contributors
| accessdate=July 25, 2019
| via = [[GitHub]]
}}
</ref>
<ref name="type-inference-i">
{{cite web
| url = http://crystal-lang.org/2013/09/23/type-inference-part-1.html
| title = Type inference part 1
| last = Brian J.
| first = Cardiff
| date = September 9, 2013
| website = crystal-lang.org
}}
</ref>
<ref name="goodbye_ruby">
{{cite web
Line 108 ⟶ 189:
| last = Borenszweig
| first = Ary
| date = November 14, 2013
| website = crystal-lang.org
}}
</ref>
Line 116 ⟶ 199:
| last = Borenszweig
| first = Ary
| date = June 19, 2014
| website = crystal-lang.org
}}
</ref>
<ref name="why_crystal">
{{cite web
| url = httphttps://motion-expressadlerhsieh.com/blogp/why-use-crystal-lang
| title = Why Crystal programming language?
| last = Hsieh
| first = Adler
| date = September 20, 2015
| website = adlerhsieh.com
}}
</ref>
<ref name="story">
{{cite web
| url = httphttps://manas.com.artech/blog/2016/04/01/the-story-behind-crystal.html/
| title = The story behind #CrystalLang
| last = David
| first = María Inti
| date = April 1, 2016
| website = manas.tech
}}
</ref>
Line 140 ⟶ 229:
| last = Borenszweig
| first = Ary
| date = June 16, 2016
| quote = It's heavily inspired by Ruby, and other languages (like C#, Go and Python).
| website = crystal-lang.org
| quote =
}}
</ref>
<ref name="devm">
{{cite web
| url = https://devm.io/ruby/crystal-ruby-programming
| title = Programming with Crystal: 'A language for humans and computers'
| date = July 3, 2023
| website = devm.io
}}
</ref>
}}
 
== External links ==
* [http://{{Official website|crystal-lang.org/ Crystal official site]}}
* [https://githubcrystal-lang.comorg/manastechreference/crystal Crystal source codeDocumentation]
* {{Github|crystal-lang}}
* [https://reddit.com/r/crystal_programming /r/crystal_programming subreddit]
* [https://crystal-ann.com Crystal Announcements]
 
[[Category:Programming languages]]
[[Category:Multi-paradigm programming languages]]
[[Category:Object-oriented programming languages]]
[[Category:Concurrent programming languages]]
[[Category:Statically typed programming languages]]
[[Category:Cross-platform free software]]
[[Category:Cross-platform software]]
[[Category:Free and open source compilers]]
[[Category:Software using the Apache license]]
[[Category:Programming languages created in 2014]]
[[Category:2014 software]]