Crystal is an actively used programming language created in 2014. In computer software programming languages, Crystal is a general-purpose, object-oriented programming language, designed and developed by Ary Borenszweig and Juan Wajnerman and more than 200 contributors. With syntax inspired by the language Ruby, 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. Read more on Wikipedia...

5Years Old 17,358Users 43Jobs

Example code from the Hello World Collection:

# Hello world in Crystal

puts "Hello World"

Example code from Linguist:

#!/usr/bin/env bin/crystal --run
require "../../spec_helper"

describe "Type inference: declare var" do
  it "types declare var" do
    assert_type("a :: Int32") { int32 }
  end

  it "types declare var and reads it" do
    assert_type("a :: Int32; a") { int32 }
  end

  it "types declare var and changes its type" do
    assert_type("a :: Int32; while 1 == 2; a = 'a'; end; a") { union_of(int32, char) }
  end

  it "declares instance var which appears in initialize" do
    result = assert_type("
      class Foo
        @x :: Int32
      end

      Foo.new") { types["Foo"] }

    mod = result.program

    foo = mod.types["Foo"] as NonGenericClassType
    foo.instance_vars["@x"].type.should eq(mod.int32)
  end

  it "declares instance var of generic class" do
    result = assert_type("
      class Foo(T)
        @x :: T
      end

      Foo(Int32).new") do
        foo = types["Foo"] as GenericClassType
        foo_i32 = foo.instantiate([int32] of Type | ASTNode)
        foo_i32.lookup_instance_var("@x").type.should eq(int32)
        foo_i32
    end
  end

  it "declares instance var of generic class after reopen" do
    result = assert_type("
      class Foo(T)
      end

      f = Foo(Int32).new

      class Foo(T)
        @x :: T
      end

      f") do
        foo = types["Foo"] as GenericClassType
        foo_i32 = foo.instantiate([int32] of Type | ASTNode)
        foo_i32.lookup_instance_var("@x").type.should eq(int32)
        foo_i32
    end
  end

  it "declares an instance variable in initialize" do
    assert_type("
      class Foo
        def initialize
          @x :: Int32
        end

        def x
          @x
        end
      end

      Foo.new.x
      ") { int32 }
  end
end

Example code from Wikipedia:

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

Trending Repos

repo stars description
invidious 1184 Invidious is an alternative front-end to YouTube
crystal 13807 The Crystal Programming Language

Last updated December 10th, 2019

Edit Crystal