-
Notifications
You must be signed in to change notification settings - Fork 36
/
crystal.cr
73 lines (56 loc) · 1.35 KB
/
crystal.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Features of Crystal
# - Ruby-inspired syntax.
# - Statically type-checked but without having to specify the type of variables or method arguments.
# - Be able to call C code by writing bindings to it in Crystal.
# - Have compile-time evaluation and generation of code, to avoid boilerplate code.
# - Compile to efficient native code.
# A very basic HTTP server
require "http/server"
server = HTTP::Server.new(8080) do |request|
HTTP::Response.ok "text/plain", "Hello world, got #{request.path}!"
end
puts "Listening on http://0.0.0.0:8080"
server.listen
module Foo
abstract def abstract_method : String
@[AlwaysInline]
def with_foofoo
with Foo.new(self) yield
end
struct Foo
def initialize(@foo : ::Foo)
end
def hello_world
@foo.abstract_method
end
end
end
class Bar
include Foo
@@foobar = 12345
def initialize(@bar : Int32)
end
macro alias_method(name, method)
def {{ name }}(*args)
{{ method }}(*args)
end
end
def a_method
"Hello, World"
end
alias_method abstract_method, a_method
def show_instance_vars : Nil
{% for var in @type.instance_vars %}
puts "@{{ var }} = #{ @{{ var }} }"
{% end %}
end
end
class Baz < Bar; end
lib LibC
fun c_puts = "puts"(str : Char*) : Int
end
baz = Baz.new(100)
baz.show_instance_vars
baz.with_foofoo do
LibC.c_puts hello_world
end