Newer
Older
require "../../spec_helper"
describe "Code gen: generic class type" do
it "codegens inherited generic class instance var" do
run(%(
class Foo(T)
def initialize(@x : T)
end
def x
@x + 1
end
end
class Bar < Foo(Int32)
end
Bar.new(1).x
)).to_i.should eq(2)
end
Ary Borenszweig
committed
it "creates pointer of unspecified generic type" do
run(%(
struct Int32
def to_i
self
end
end
struct Char
def to_i
ord
end
end
class Foo(T)
def initialize(@x : T)
end
def x
@x
end
end
p = Pointer(Foo).malloc(1_u64)
p.value = Foo.new(1)
p.value = Foo.new('a')
p.value.x.to_i
)).to_i.should eq('a'.ord)
end
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
it "creates pointer of unspecified generic type with inherited class" do
run(%(
class Foo(T)
def initialize(@x : T)
end
def x
@x + 1
end
end
class Bar < Foo(Int32)
end
p = Pointer(Foo).malloc(1_u64)
p.value = Bar.new(1)
p.value.x
)).to_i.should eq(2)
end
it "creates pointer of unspecified generic type with inherited class (2)" do
run(%(
class Foo(T)
def initialize(@x : T)
end
def x
@x + 1
end
end
class Bar(T) < Foo(T)
end
p = Pointer(Foo).malloc(1_u64)
p.value = Bar.new(1)
p.value.x
)).to_i.should eq(2)
end