Crystal (programming language): Difference between revisions
No shortdesc needed (WP:SDNONE) Tags: Reverted Visual edit |
remove errant ref tag |
||
(5 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
{{Short description| |
{{Short description|Object-oriented programming language}} |
||
{{About|the [[programming language]]||Crystal (disambiguation)}} |
{{About|the [[programming language]]||Crystal (disambiguation)}} |
||
{{Infobox programming language |
{{Infobox programming language |
||
Line 6: | Line 6: | ||
| paradigm = [[Multi-paradigm programming language|Multi-paradigm]]: [[Object-oriented programming|object-oriented]], [[Concurrent computing|concurrent]] |
| paradigm = [[Multi-paradigm programming language|Multi-paradigm]]: [[Object-oriented programming|object-oriented]], [[Concurrent computing|concurrent]] |
||
| 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> |
| 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> |
||
| designer = |
| designer = Ary Borenszweig, Juan Wajnerman, Brian Cardiff |
||
| developer = Manas Technology Solutions |
| developer = Manas Technology Solutions |
||
| latest release version = {{wikidata|property| |
| latest release version = {{wikidata|property|preferred|references|edit|P348|P548=Q2804309}} |
||
| latest release date = {{ |
| latest release date = {{Start date and age|{{wikidata|qualifier|preferred|single|P348|P548=Q2804309|P577}}|df=yes}} |
||
| typing = [[Type system#Static type checking|static]], [[Type system#Inferred type checking|inferred]], [[Nominal type system|nominal]], [[Duck_typing|duck]] |
| typing = [[Type system#Static type checking|static]], [[Type system#Inferred type checking|inferred]], [[Nominal type system|nominal]], [[Duck_typing|duck]] |
||
| influenced by = [[Ruby (programming language)|Ruby]], [[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"/> |
| influenced by = [[Ruby (programming language)|Ruby]], [[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"/> |
||
| programming language = Crystal |
| programming language = Crystal |
||
| platform = [[IA-32]] (i386), [[x86-64]], [[AArch64]]<ref name="platform-support" /> |
| platform = [[IA-32]] (i386), [[x86-64]], [[AArch64]]<ref name="platform-support" /> |
||
| operating system = [[Linux]], [[macOS]], [[FreeBSD]], [[OpenBSD]]<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 |
||
Line 32: | Line 32: | ||
== Examples == |
== Examples == |
||
=== Hello World === |
=== Hello World === |
||
This is the simplest way to write the [[Hello World]] program in Crystal: |
This is the simplest way to write the [["Hello, World!" program|Hello World]] program in Crystal: |
||
<syntaxhighlight lang="crystal"> |
<syntaxhighlight lang="crystal"> |
||
Line 169: | Line 169: | ||
| via = [[GitHub]] |
| via = [[GitHub]] |
||
}} |
}} |
||
</ref> |
|||
</ref> |
</ref> |
||
<ref name="type-inference-i"> |
<ref name="type-inference-i"> |
Latest revision as of 22:50, 21 March 2024
Paradigm | Multi-paradigm: object-oriented, concurrent |
---|---|
Designed by | Ary Borenszweig, Juan Wajnerman, Brian Cardiff |
Developer | Manas Technology Solutions |
First appeared | June 19, 2014[1] |
Stable release | 1.12.2[2]
/ 31 May 2024 |
Typing discipline | static, inferred, nominal, duck |
Implementation language | Crystal |
Platform | IA-32 (i386), x86-64, AArch64[3] |
OS | Linux, macOS, FreeBSD, OpenBSD, Windows[3] |
License | Apache License 2.0 |
Filename extensions | .cr |
Website | crystal-lang |
Influenced by | |
Ruby, C, Rust, Go,[4] C#,[4] Python[4] |
Crystal is a high-level general-purpose, object-oriented programming language, designed and developed by Ary Borenszweig, Juan Wajnerman, Brian Cardiff and more than 400 contributors.[5] With syntax inspired by the language Ruby,[4] it is a compiled language with static type-checking, but specifying the types of variables or method arguments is generally unneeded. Types are resolved by an advanced global type inference algorithm.[6][7] Crystal is currently in active development. It is released as free and open-source software under the Apache License version 2.0.
History[edit]
Work on the language began in June 2011,[8] with the aim of merging the elegance and productivity of Ruby with the speed, efficiency, and type safety of a compiled language.[9][8] Initially named Joy, it was quickly renamed to Crystal.[8]
The Crystal compiler was first written in Ruby, but later rewritten in Crystal, thus becoming self-hosting, as of November 2013[update].[10] The first official version was released in June 2014.[11] In July 2016, Crystal joined the TIOBE index.
Description[edit]
Although resembling the Ruby language in syntax, Crystal compiles to much more efficient native code using an LLVM backend, at the cost of precluding the dynamic aspects of Ruby. The advanced global type inference used by the Crystal compiler, combined with union types, gives it more the feel of a higher-level scripting language than many other comparable programming languages. It has automated garbage collection and offers a Boehm collector. Crystal possesses a macro system and supports generics as well as method and operator overloading. Its concurrency model is inspired by communicating sequential processes (CSP) and implements lightweight fibers and channels (for interfiber communication) inspired by Go.[4]
Examples[edit]
Hello World[edit]
This is the simplest way to write the Hello World program in Crystal:
puts "Hello World!"
The same as in Ruby.
Or using an object-oriented programming style:
class Greeter
def initialize(@name : String)
end
def salute
puts "Hello #{@name}!"
end
end
g = Greeter.new("world")
g.salute
HTTP server[edit]
require "http/server"
server = HTTP::Server.new do |context|
context.response.content_type = "text/plain"
context.response.print "Hello world! The time is #{Time.local}"
end
server.bind_tcp("0.0.0.0", 8080)
puts "Listening on http://0.0.0.0:8080"
server.listen
TCP echo server[edit]
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
Type inference and union types[edit]
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.
desired_things = [:unicorns, "butterflies", 1_000_000]
p typeof(desired_things.first) # typeof returns the compile time type, here (Symbol | String | Int32)
p desired_things.first.class # the class method returns the runtime type, here Symbol
Concurrency[edit]
Channels can be used to communicate between fibers, which are initiated using the keyword spawn
.
channel = Channel(Int32).new
spawn do
puts "Before first send"
channel.send(1)
puts "Before second send"
channel.send(2)
end
puts "Before first receive"
value = channel.receive
puts value # => 1
puts "Before second receive"
value = channel.receive
puts value # => 2
Further reading[edit]
- St. Laurent, Simon; Balbaert, Ivo (February 1, 2019), Programming Crystal (P1.0 ed.), Pragmatic Bookshelf, ISBN 978-1-68050-286-2
- Dietrich, George; Bernal, Guilherme (May 27, 2022), Crystal Programming, Packt Publishing, ISBN 978-1801818674
- Wartala, Ramon (March 2016), "Die Ruby-artige Programmiersprache Crystal" [The Ruby-like programming language Crystal], Linux Magazin (in German), no. 3/2016, ISSN 1432-640X
References[edit]
- ^ "Crystal 0.1.0 released!". crystal-lang. 19 June 2014.
- ^ "Release 1.12.2". 31 May 2024. Retrieved 21 June 2024.
- ^ a b "Crystal Platform Support". crystal-lang.org.
- ^ a b c d e
Borenszweig, Ary (June 16, 2016). "Crystal 0.18.0 released!". crystal-lang.org.
It's heavily inspired by Ruby, and other languages (like C#, Go and Python).
- ^ "Contributors". Retrieved July 25, 2019 – via GitHub.
- ^ Brian J., Cardiff (September 9, 2013). "Type inference part 1". crystal-lang.org.
- ^ "Programming with Crystal: 'A language for humans and computers'". devm.io. July 3, 2023.
- ^ a b c David, María Inti (April 1, 2016). "The story behind #CrystalLang". manas.tech.
- ^ Hsieh, Adler (September 20, 2015). "Why Crystal programming language?". motion-express.com.
- ^ Borenszweig, Ary (November 14, 2013). "Good bye Ruby Thursday". crystal-lang.org.
- ^ Borenszweig, Ary (June 19, 2014). "Crystal 0.1.0 released!". crystal-lang.org.
External links[edit]
- Programming languages
- Multi-paradigm programming languages
- Object-oriented programming languages
- Concurrent programming languages
- Statically typed programming languages
- Cross-platform free software
- Cross-platform software
- Free compilers and interpreters
- Software using the Apache license
- Programming languages created in 2014
- 2014 software