Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Crystal (programming language)

From Wikipedia, the free encyclopedia
Object-oriented programming language
Crystal
ParadigmMulti-paradigm:object-oriented,concurrent
Designed byAry Borenszweig, Juan Wajnerman, Brian Cardiff
DeveloperManas Technology Solutions
First appearedJune 19, 2014; 11 years ago (2014-06-19)[1]
Stable release
1.17.1 Edit this on Wikidata / 22 July 2025; 2 months ago (22 July 2025)
Typing disciplinestatic,inferred,nominal,duck
Implementation languageCrystal
PlatformIA-32 (i386),x86-64,AArch64[2]
OSLinux,macOS,FreeBSD,OpenBSD,Windows[2]
LicenseApache License 2.0
Filename extensions.cr
Websitecrystal-lang.org
Influenced by
Ruby,Go[3]

Crystal is ahigh-levelgeneral-purpose,object-oriented programming language, designed and developed by Ary Borenszweig, Juan Wajnerman, Brian Cardiff and more than 400 contributors.[4] With syntax inspired by the languageRuby,[5] it is acompiled language withstatic type-checking, but specifying the types of variables or method arguments is generally unneeded. Types are resolved by an advanced globaltype inference algorithm.[6][7] Crystal is currently in active development. It is released asfree and open-source software under theApache 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 namedJoy, it was quickly renamed toCrystal.[8]

The Crystal compiler was first written in Ruby, but later rewritten in Crystal, thus becomingself-hosting, as of November 2013[update].[10] The first official version was released in June 2014.[11] In July 2016, Crystal joined theTIOBE index.

Description

[edit]

Although resembling the Ruby language in syntax, Crystal compiles to much more efficient native code using anLLVM backend, at the cost of precluding the dynamic aspects of Ruby. The advanced global type inference used by the Crystal compiler, combined withunion 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 aBoehm collector. Crystal possesses a macro system and supports generics as well as method and operator overloading. Its concurrency model is inspired bycommunicating sequential processes (CSP) and implements lightweight fibers and channels (for interfiber communication), inspired byGo.[3]

Examples

[edit]

Hello World

[edit]

This is the simplest way to write theHello World program in Crystal:

puts"Hello World!"

The same as in Ruby.

Or using anobject-oriented programming style:

classGreeterdefinitialize(@name:String)enddefsaluteputs"Hello#{@name}!"endendg=Greeter.new("world")g.salute

HTTP server

[edit]
require"http/server"server=HTTP::Server.newdo|context|context.response.content_type="text/plain"context.response.print"Hello world! The time is#{Time.local}"endserver.bind_tcp("0.0.0.0",8080)puts"Listening on http://0.0.0.0:8080"server.listen

TCP echo server

[edit]
require"socket"defhandle_client(client)message=client.getsclient.putsmessageendserver=TCPServer.new("localhost",1234)whileclient=server.accept?spawnhandle_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]ptypeof(desired_things.first)# typeof returns the compile time type, here (Symbol | String | Int32)pdesired_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 keywordspawn.

channel=Channel(Int32).newspawndoputs"Before first send"channel.send(1)puts"Before second send"channel.send(2)endputs"Before first receive"value=channel.receiveputsvalue# => 1puts"Before second receive"value=channel.receiveputsvalue# => 2

Adoption

[edit]

In 2020, it was reported that the infotainment units in vehicles produced byNikola Corporation were written in Crystal.[12] Much of the backend of theKagi search engine is written with Crystal.[13]

Further reading

[edit]

References

[edit]
  1. ^"Crystal 0.1.0 released!".crystal-lang. 19 June 2014.
  2. ^ab"Crystal Platform Support".crystal-lang.org.
  3. ^ab"Crystal multithreading support". 23 February 2024.
  4. ^"Contributors". RetrievedJuly 25, 2019 – viaGitHub.
  5. ^Borenszweig, Ary (June 16, 2016)."Crystal 0.18.0 released!".crystal-lang.org.
  6. ^Brian J., Cardiff (September 9, 2013)."Type inference part 1".crystal-lang.org.
  7. ^"Programming with Crystal: 'A language for humans and computers'".devm.io. July 3, 2023.
  8. ^abcDavid, María Inti (April 1, 2016)."The story behind #CrystalLang".manas.tech.
  9. ^Hsieh, Adler (September 20, 2015)."Why Crystal programming language?".adlerhsieh.com.
  10. ^Borenszweig, Ary (November 14, 2013)."Good bye Ruby Thursday".crystal-lang.org.
  11. ^Borenszweig, Ary (June 19, 2014)."Crystal 0.1.0 released!".crystal-lang.org.
  12. ^Pettinati, Martin (11 February 2020)."Nikola Motor Company: Crystal powered dashboards on the trucks of the future | Manas.Tech".Manas Technology Solutions.
  13. ^"Zac Nowicki – Tales from Kagi | CrystalConf 2023". 13 November 2023.

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Crystal_(programming_language)&oldid=1283755455"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp