rtype-native 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/LICENSE +9 -0
- data/README.md +448 -0
- data/Rakefile +12 -0
- data/benchmark/benchmark.rb +192 -0
- data/ext/rtype/extconf.rb +3 -0
- data/ext/rtype/rtype.c +122 -0
- data/ext/rtype/rtype.h +5 -0
- data/spec/rtype_spec.rb +541 -0
- data/spec/spec_helper.rb +5 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3a5d14f3ae8593646d772106c340e8d41b594c4f
|
4
|
+
data.tar.gz: ec5849c75b6a42d8c30dfe41bf2eb48a0fe54bb5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fddff4c61beacc969280f481972b339231b20dcc617f6c7f941d76b7b4c92d2baa70b2b20d5357ea0ff653abe2c6961e002dac0b2b8321be2a7efc3f31a70cc5
|
7
|
+
data.tar.gz: 23f5bc94ee53094bdeef9ea2f98073086bc9ac08a1f63d4d27b77e152acfdc267fc92b130d97a9da722fc560f1a854008e67ff61e9133311597af922123aa19e
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Sputnik Gugja
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,448 @@
|
|
1
|
+
# Rtype: ruby with type
|
2
|
+
[](https://badge.fury.io/rb/rtype)
|
3
|
+
[](https://travis-ci.org/sputnikgugja/rtype)
|
4
|
+
[](https://coveralls.io/github/sputnikgugja/rtype?branch=master)
|
5
|
+
|
6
|
+
You can do the type checking in Ruby with this gem!
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
require 'rtype'
|
10
|
+
|
11
|
+
rtype :sum, [:to_i, Numeric] => Numeric
|
12
|
+
def sum(a, b)
|
13
|
+
a.to_i + b
|
14
|
+
end
|
15
|
+
|
16
|
+
sum(123, "asd")
|
17
|
+
# (Rtype::ArgumentTypeError) for 2nd argument:
|
18
|
+
# Expected "asd" to be a Numeric
|
19
|
+
|
20
|
+
class Test
|
21
|
+
rtype_self :invert, {state: Boolean} => Boolean
|
22
|
+
def self.invert(state:)
|
23
|
+
!state
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
Test::invert(state: 0)
|
28
|
+
# (Rtype::ArgumentTypeError) for 'state' argument:
|
29
|
+
# Expected 0 to be a Boolean
|
30
|
+
```
|
31
|
+
|
32
|
+
## Requirements
|
33
|
+
- Ruby >= 2.1
|
34
|
+
- MRI or RBX if native extension is used. Rtype itself without it is pure-ruby gem, therefore you can use Rtype in JRuby, etc.
|
35
|
+
|
36
|
+
## Features
|
37
|
+
- Provide type checking for argument and return
|
38
|
+
- Support type checking for [keyword argument](#keyword-argument)
|
39
|
+
- [Type checking for array elements](#array)
|
40
|
+
- [Duck typing](#duck-typing)
|
41
|
+
- Custom type behavior
|
42
|
+
|
43
|
+
## Installation
|
44
|
+
Run `gem install rtype` or add `gem 'rtype'` to your `Gemfile`
|
45
|
+
|
46
|
+
And add to your `.rb` source file:
|
47
|
+
```ruby
|
48
|
+
require 'rtype'
|
49
|
+
```
|
50
|
+
|
51
|
+
### Native extension
|
52
|
+
Rtype itself is pure-ruby gem. but you can make it more faster by native extension.
|
53
|
+
|
54
|
+
Just run `gem install rtype-native` or add `gem 'rtype-native'` to your `Gemfile`, then Rtype use it.
|
55
|
+
(Do not `require 'rtype-native'`)
|
56
|
+
|
57
|
+
It is only for MRI or RBX. JRuby extension not supported yet.
|
58
|
+
|
59
|
+
## Usage
|
60
|
+
|
61
|
+
### Supported Type Behaviors
|
62
|
+
- `Module`
|
63
|
+
- Value must be an instance of this module/class or one of its superclasses
|
64
|
+
- `Any` : An alias for `BasicObject` (means Any Object)
|
65
|
+
- `Boolean` : `true` or `false`
|
66
|
+
- `Symbol`
|
67
|
+
- Value must have(respond to) a method with this name
|
68
|
+
- `Regexp`
|
69
|
+
- Value must match this regexp pattern
|
70
|
+
- `Range`
|
71
|
+
- Value must be included in this range
|
72
|
+
- `Array` (tuple)
|
73
|
+
- Value must be an array
|
74
|
+
- Each of value's elements must be valid
|
75
|
+
- Value's length must be equal to the array's length
|
76
|
+
- Of course, nested array works
|
77
|
+
- Example: [Array](#array)
|
78
|
+
- This can be used as a tuple
|
79
|
+
- `Proc`
|
80
|
+
- Value must return a truthy value for this proc
|
81
|
+
- `true`
|
82
|
+
- Value must be **truthy**
|
83
|
+
- `false`
|
84
|
+
- Value must be **falsy**
|
85
|
+
- `nil`
|
86
|
+
- Only available for **return type**. void return type in other languages
|
87
|
+
- Special Behaviors
|
88
|
+
- `Rtype::and(*types)` : Ensure value is valid for all the types
|
89
|
+
- It also can be used as `Rtype::Behavior::And[*types]` or `include Rtype::Behavior; And[...]`
|
90
|
+
- `Rtype::or(*types)` : Ensure value is valid for at least one of the types
|
91
|
+
- It also can be used as `Rtype::Behavior::Or[*types]` or `include Rtype::Behavior; Or[...]`
|
92
|
+
- `Rtype::xor(*types)` : Ensure value is valid for only one of the types
|
93
|
+
- It also can be used as `Rtype::Behavior::Xor[*types]` or `include Rtype::Behavior; Xor[...]`
|
94
|
+
- `Rtype::not(*types)` : Ensure value is not valid for all the types
|
95
|
+
- It also can be used as `Rtype::Behavior::Not[*types]` or `include Rtype::Behavior; Not[...]`
|
96
|
+
- `Rtype::nilable(type)` : Ensure value can be nil
|
97
|
+
- It also can be used as `Rtype::Behavior::Nilable[type]` or `include Rtype::Behavior; Nilable[...]`
|
98
|
+
- You can create custom behavior by extending `Rtype::Behavior::Base`
|
99
|
+
|
100
|
+
### Examples
|
101
|
+
|
102
|
+
#### Basic
|
103
|
+
```ruby
|
104
|
+
require 'rtype'
|
105
|
+
|
106
|
+
class Example
|
107
|
+
rtype :test, [Integer] => nil
|
108
|
+
def test(i)
|
109
|
+
end
|
110
|
+
|
111
|
+
rtype :any_type_arg, [Any] => nil
|
112
|
+
def any_type_arg(arg)
|
113
|
+
end
|
114
|
+
|
115
|
+
rtype :return_type_test, [] => Integer
|
116
|
+
def return_type_test
|
117
|
+
"not integer"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
e = Example.new
|
122
|
+
e.test("not integer")
|
123
|
+
# (Rtype::ArgumentTypeError) for 1st argument:
|
124
|
+
# Expected "not integer" to be a Integer
|
125
|
+
|
126
|
+
e.any_type_arg("Any argument!") # Works
|
127
|
+
|
128
|
+
e.return_type_test
|
129
|
+
# (Rtype::ReturnTypeError) for return:
|
130
|
+
# Expected "not integer" to be a Integer
|
131
|
+
```
|
132
|
+
|
133
|
+
#### Keyword argument
|
134
|
+
```ruby
|
135
|
+
require 'rtype'
|
136
|
+
|
137
|
+
class Example
|
138
|
+
rtype :say_your_name, {name: String} => Any
|
139
|
+
def say_your_name(name:)
|
140
|
+
puts "My name is #{name}"
|
141
|
+
end
|
142
|
+
|
143
|
+
# Mixing positional arguments and keyword arguments
|
144
|
+
rtype :name_and_age, [String, {age: Integer}] => Any
|
145
|
+
def name_and_age(name, age:)
|
146
|
+
puts "Name: #{name}, Age: #{age}"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
Example.new.say_your_name(name: "Babo") # My name is Babo
|
151
|
+
Example.new.name_and_age("Bamboo", age: 100) # Name: Bamboo, Age: 100
|
152
|
+
|
153
|
+
Example.new.say_your_name(name: 12345)
|
154
|
+
# (Rtype::ArgumentTypeError) for 'name' argument:
|
155
|
+
# Expected 12345 to be a String
|
156
|
+
```
|
157
|
+
|
158
|
+
#### Duck typing
|
159
|
+
```ruby
|
160
|
+
require 'rtype'
|
161
|
+
|
162
|
+
class Duck
|
163
|
+
rtype :says, [:to_i] => Any
|
164
|
+
def says(i)
|
165
|
+
puts "duck:" + " quack"*i.to_i
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
Duck.new.says("2") # duck: quack quack
|
170
|
+
```
|
171
|
+
|
172
|
+
#### Array
|
173
|
+
This can be used as a tuple.
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
rtype :func, [[Numeric, Numeric]] => Any
|
177
|
+
def func(arr)
|
178
|
+
puts "Your location is (#{arr[0]}, #{arr[1]}). I will look for you. I will find you"
|
179
|
+
end
|
180
|
+
|
181
|
+
func [1, "str"]
|
182
|
+
# (Rtype::ArgumentTypeError) for 1st argument:
|
183
|
+
# Expected [1, "str"] to be an array with 2 elements:
|
184
|
+
# - [0] index : Expected 1 to be a Numeric
|
185
|
+
# - [1] index : Expected "str" to be a Numeric
|
186
|
+
|
187
|
+
func [1, 2, 3]
|
188
|
+
# (Rtype::ArgumentTypeError) for 1st argument:
|
189
|
+
# Expected [1, 2, 3] to be an array with 2 elements:
|
190
|
+
# - [0] index : Expected 1 to be a Numeric
|
191
|
+
# - [1] index : Expected 2 to be a Numeric
|
192
|
+
|
193
|
+
func [1]
|
194
|
+
# (Rtype::ArgumentTypeError) for 1st argument:
|
195
|
+
# Expected [1] to be an array with 2 elements:
|
196
|
+
# - [0] index : Expected 1 to be a Numeric
|
197
|
+
# - [1] index : Expected nil to be a Numeric
|
198
|
+
|
199
|
+
func [1, 2] # Your location is (1, 2). I will look for you. I will find you
|
200
|
+
```
|
201
|
+
|
202
|
+
#### rtype with attr_accessor
|
203
|
+
`rtype_accessor`
|
204
|
+
|
205
|
+
You can use `rtype_accessor_self` for static accessor.
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
require 'rtype'
|
209
|
+
|
210
|
+
class Example
|
211
|
+
rtype_accessor :value, String
|
212
|
+
attr_accessor :value
|
213
|
+
def initialize
|
214
|
+
@value = 456
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
Example.new.value = 123
|
219
|
+
# (Rtype::ArgumentTypeError) for 1st argument:
|
220
|
+
# Expected 123 to be a String
|
221
|
+
|
222
|
+
Example.new.value
|
223
|
+
# (Rtype::ReturnTypeError) for return:
|
224
|
+
# Expected 456 to be a String
|
225
|
+
```
|
226
|
+
|
227
|
+
#### Combined type
|
228
|
+
```ruby
|
229
|
+
### TEST 1 ###
|
230
|
+
require 'rtype'
|
231
|
+
|
232
|
+
class Example
|
233
|
+
rtype :and_test, [Rtype::and(String, :func)] => Any
|
234
|
+
def and_test(arg)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
Example.new.and_test("A string")
|
239
|
+
# (Rtype::ArgumentTypeError) for 1st argument:
|
240
|
+
# Expected "A string" to be a String
|
241
|
+
# AND Expected "A string" to respond to :func
|
242
|
+
```
|
243
|
+
```ruby
|
244
|
+
### TEST 2 ###
|
245
|
+
# ... require rtype and define Example the same as above ...
|
246
|
+
|
247
|
+
class String
|
248
|
+
def func; end
|
249
|
+
end
|
250
|
+
|
251
|
+
Example.new.and_test("A string") # Works!
|
252
|
+
```
|
253
|
+
|
254
|
+
#### Combined duck type
|
255
|
+
Application of duck typing and combined type
|
256
|
+
|
257
|
+
```ruby
|
258
|
+
require 'rtype'
|
259
|
+
|
260
|
+
module Game
|
261
|
+
ENEMY = [
|
262
|
+
:name,
|
263
|
+
:level
|
264
|
+
]
|
265
|
+
|
266
|
+
class Player < Entity
|
267
|
+
include Rtype::Behavior
|
268
|
+
|
269
|
+
rtype :attack, [And[*ENEMY]] => Any
|
270
|
+
def attacks(enemy)
|
271
|
+
"Player attacks '#{enemy.name}' (level #{enemy.level})!"
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
class Slime < Entity
|
276
|
+
def name
|
277
|
+
"Powerful Slime"
|
278
|
+
end
|
279
|
+
|
280
|
+
def level
|
281
|
+
123
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
Game::Player.new.attacks Game::Slime.new
|
287
|
+
# Player attacks 'Powerful Slime' (level 123)!
|
288
|
+
```
|
289
|
+
|
290
|
+
#### Position of `rtype` && (symbol || string)
|
291
|
+
```ruby
|
292
|
+
require 'rtype'
|
293
|
+
|
294
|
+
class Example
|
295
|
+
# Works. Recommended
|
296
|
+
rtype :hello_world, [Integer, String] => String
|
297
|
+
def hello_world(i, str)
|
298
|
+
puts "Hello? #{i} #{st
|
299
|
+
end
|
300
|
+
|
301
|
+
# Works
|
302
|
+
def hello_world_two(i, str)
|
303
|
+
puts "Hello? #{i} #{str}"
|
304
|
+
end
|
305
|
+
rtype :hello_world_two, [Integer, String] => String
|
306
|
+
|
307
|
+
# Also works (String will be converted to Symbol)
|
308
|
+
rtype 'hello_world_three', [Integer, String] => String
|
309
|
+
def hello_world_three(i, str)
|
310
|
+
puts "Hello? #{i} #{str}"
|
311
|
+
end
|
312
|
+
end
|
313
|
+
```
|
314
|
+
|
315
|
+
#### Outside of module (root)
|
316
|
+
Yes, it works
|
317
|
+
|
318
|
+
```ruby
|
319
|
+
rtype :say, [String] => Any
|
320
|
+
def say(message)
|
321
|
+
puts message
|
322
|
+
end
|
323
|
+
|
324
|
+
say "Hello" # Hello
|
325
|
+
```
|
326
|
+
|
327
|
+
#### Static method
|
328
|
+
Use `rtype_self`
|
329
|
+
|
330
|
+
```ruby
|
331
|
+
require 'rtype'
|
332
|
+
|
333
|
+
class Example
|
334
|
+
rtype_self :say_ya, [:to_i] => Any
|
335
|
+
def self.say_ya(i)
|
336
|
+
puts "say" + " ya"*i.to_i
|
337
|
+
end
|
338
|
+
end
|
339
|
+
|
340
|
+
Example::say_ya(3) #say ya ya ya
|
341
|
+
```
|
342
|
+
|
343
|
+
#### Check type information
|
344
|
+
This is just the 'information'
|
345
|
+
|
346
|
+
Any change of this doesn't affect type checking
|
347
|
+
|
348
|
+
```ruby
|
349
|
+
require 'rtype'
|
350
|
+
|
351
|
+
class Example
|
352
|
+
rtype :test, [:to_i] => Any
|
353
|
+
def test(i)
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
Example.new.method(:test).type_info
|
358
|
+
# => [:to_i] => Any
|
359
|
+
Example.new.method(:test).argument_type
|
360
|
+
# => [:to_i]
|
361
|
+
Example.new.method(:test).return_type
|
362
|
+
# => Any
|
363
|
+
```
|
364
|
+
|
365
|
+
## Documentation
|
366
|
+
[RubyDoc.info](http://www.rubydoc.info/gems/rtype)
|
367
|
+
|
368
|
+
## Benchmarks
|
369
|
+
Result of `rake benchmark` ([source](https://github.com/sputnikgugja/rtype/tree/master/benchmark/benchmark.rb))
|
370
|
+
|
371
|
+
### MRI
|
372
|
+
```
|
373
|
+
Ruby version: 2.1.7
|
374
|
+
Ruby engine: ruby
|
375
|
+
Ruby description: ruby 2.1.7p400 (2015-08-18 revision 51632) [x64-mingw32]
|
376
|
+
Rtype version: 0.2.0
|
377
|
+
Rubype version: 0.3.1
|
378
|
+
Sig version: 1.0.1
|
379
|
+
Contracts version: 0.13.0
|
380
|
+
Typecheck version: 0.1.2
|
381
|
+
Warming up --------------------------------------
|
382
|
+
pure 85.600k i/100ms
|
383
|
+
rtype 25.735k i/100ms
|
384
|
+
rubype 20.922k i/100ms
|
385
|
+
sig 8.960k i/100ms
|
386
|
+
contracts 4.659k i/100ms
|
387
|
+
typecheck 1.102k i/100ms
|
388
|
+
Calculating -------------------------------------
|
389
|
+
pure 3.273M (± 2.4%) i/s - 16.435M
|
390
|
+
rtype 340.376k (± 1.9%) i/s - 1.724M
|
391
|
+
rubype 268.253k (± 4.2%) i/s - 1.339M
|
392
|
+
sig 100.050k (± 1.9%) i/s - 501.760k
|
393
|
+
contracts 50.027k (± 2.0%) i/s - 251.586k
|
394
|
+
typecheck 11.317k (± 1.4%) i/s - 57.304k
|
395
|
+
|
396
|
+
Comparison:
|
397
|
+
pure: 3272978.0 i/s
|
398
|
+
rtype: 340375.8 i/s - 9.62x slower
|
399
|
+
rubype: 268252.5 i/s - 12.20x slower
|
400
|
+
sig: 100050.1 i/s - 32.71x slower
|
401
|
+
contracts: 50026.9 i/s - 65.42x slower
|
402
|
+
typecheck: 11317.1 i/s - 289.21x slower
|
403
|
+
```
|
404
|
+
|
405
|
+
### JRuby
|
406
|
+
Without Rubype that doesn't support JRuby
|
407
|
+
|
408
|
+
```
|
409
|
+
Ruby version: 2.2.3
|
410
|
+
Ruby engine: jruby
|
411
|
+
Ruby description: jruby 9.0.5.0 (2.2.3) 2016-01-26 7bee00d Java HotSpot(TM) 64-Bit Server VM 25.60-b23 on 1.8.0_60-b27 +jit [Windows 10-amd64]
|
412
|
+
Rtype version: 0.2.0
|
413
|
+
Sig version: 1.0.1
|
414
|
+
Contracts version: 0.13.0
|
415
|
+
Typecheck version: 0.1.2
|
416
|
+
Warming up --------------------------------------
|
417
|
+
pure 29.127k i/100ms
|
418
|
+
rtype 4.566k i/100ms
|
419
|
+
sig 4.162k i/100ms
|
420
|
+
contracts 776.000 i/100ms
|
421
|
+
typecheck 981.000 i/100ms
|
422
|
+
Calculating -------------------------------------
|
423
|
+
pure 6.705M (±12.0%) i/s - 32.826M
|
424
|
+
rtype 89.262k (± 3.3%) i/s - 447.468k
|
425
|
+
sig 74.070k (± 2.0%) i/s - 370.418k
|
426
|
+
contracts 25.399k (± 2.9%) i/s - 127.264k
|
427
|
+
typecheck 12.461k (± 9.0%) i/s - 61.803k
|
428
|
+
|
429
|
+
Comparison:
|
430
|
+
pure: 6705166.5 i/s
|
431
|
+
rtype: 89262.5 i/s - 75.12x slower
|
432
|
+
sig: 74070.1 i/s - 90.52x slower
|
433
|
+
contracts: 25398.9 i/s - 263.99x slower
|
434
|
+
typecheck: 12460.7 i/s - 538.10x slower
|
435
|
+
```
|
436
|
+
|
437
|
+
## Rubype, Sig
|
438
|
+
Rtype is influenced by [Rubype](https://github.com/gogotanaka/Rubype) and [Sig](https://github.com/janlelis/sig).
|
439
|
+
|
440
|
+
If you don't like Rtype, You can use other type checking gem such as Contracts, Rubype, Rtc, Typecheck, Sig.
|
441
|
+
|
442
|
+
## Author
|
443
|
+
Sputnik Gugja (sputnikgugja@gmail.com)
|
444
|
+
|
445
|
+
## License
|
446
|
+
MIT license (@ Sputnik Gugja)
|
447
|
+
|
448
|
+
See `LICENSE` file.
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "rspec/core/rake_task"
|
2
|
+
|
3
|
+
# Default pattern is 'spec/**{,/*/**}/*_spec.rb'
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
# Benchmark
|
9
|
+
desc "Compare with pure ruby and other gems"
|
10
|
+
task :benchmark do
|
11
|
+
ruby "benchmark/benchmark.rb"
|
12
|
+
end
|
@@ -0,0 +1,192 @@
|
|
1
|
+
require 'benchmark/ips'
|
2
|
+
|
3
|
+
is_mri = RUBY_ENGINE == 'ruby'
|
4
|
+
|
5
|
+
require "rtype"
|
6
|
+
require "rubype" if is_mri
|
7
|
+
require "sig"
|
8
|
+
require "contracts"
|
9
|
+
require "contracts/version"
|
10
|
+
require "typecheck"
|
11
|
+
|
12
|
+
puts "Ruby version: #{RUBY_VERSION}"
|
13
|
+
puts "Ruby engine: #{RUBY_ENGINE}"
|
14
|
+
puts "Ruby description: #{RUBY_DESCRIPTION}"
|
15
|
+
|
16
|
+
puts "Rtype version: #{Rtype::VERSION}"
|
17
|
+
puts "Rubype version: #{Rubype::VERSION}" if is_mri
|
18
|
+
puts "Sig version: #{Sig::VERSION}"
|
19
|
+
puts "Contracts version: #{Contracts::VERSION}"
|
20
|
+
puts "Typecheck version: #{Typecheck::VERSION}"
|
21
|
+
|
22
|
+
class PureTest
|
23
|
+
def sum(x, y)
|
24
|
+
x + y
|
25
|
+
end
|
26
|
+
|
27
|
+
def mul(x, y)
|
28
|
+
x * y
|
29
|
+
end
|
30
|
+
|
31
|
+
def args(a, b, c, d)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
pure_obj = PureTest.new
|
35
|
+
|
36
|
+
class RtypeTest
|
37
|
+
rtype :sum, [Numeric, Numeric] => Numeric
|
38
|
+
def sum(x, y)
|
39
|
+
x + y
|
40
|
+
end
|
41
|
+
|
42
|
+
rtype :mul, [:to_i, :to_i] => Numeric
|
43
|
+
def mul(x, y)
|
44
|
+
x * y
|
45
|
+
end
|
46
|
+
|
47
|
+
rtype :args, [Integer, Numeric, String, :to_i] => Any
|
48
|
+
def args(a, b, c, d)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
rtype_obj = RtypeTest.new
|
52
|
+
|
53
|
+
if is_mri
|
54
|
+
class RubypeTest
|
55
|
+
def sum(x, y)
|
56
|
+
x + y
|
57
|
+
end
|
58
|
+
typesig :sum, [Numeric, Numeric] => Numeric
|
59
|
+
|
60
|
+
def mul(x, y)
|
61
|
+
x * y
|
62
|
+
end
|
63
|
+
typesig :mul, [:to_i, :to_i] => Numeric
|
64
|
+
|
65
|
+
def args(a, b, c, d)
|
66
|
+
end
|
67
|
+
typesig :args, [Integer, Numeric, String, :to_i] => Any
|
68
|
+
end
|
69
|
+
rubype_obj = RubypeTest.new
|
70
|
+
end
|
71
|
+
|
72
|
+
class SigTest
|
73
|
+
sig [Numeric, Numeric], Numeric,
|
74
|
+
def sum(x, y)
|
75
|
+
x + y
|
76
|
+
end
|
77
|
+
|
78
|
+
sig [:to_i, :to_i], Numeric,
|
79
|
+
def mul(x, y)
|
80
|
+
x * y
|
81
|
+
end
|
82
|
+
|
83
|
+
# nil means wildcard
|
84
|
+
sig [Integer, Numeric, String, :to_i], nil,
|
85
|
+
def args(a, b, c, d)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
sig_obj = SigTest.new
|
89
|
+
|
90
|
+
class ContractsTest
|
91
|
+
include Contracts
|
92
|
+
|
93
|
+
Contract Num, Num => Num
|
94
|
+
def sum(x, y)
|
95
|
+
x + y
|
96
|
+
end
|
97
|
+
|
98
|
+
Contract RespondTo[:to_i], RespondTo[:to_i] => Num
|
99
|
+
def mul(x, y)
|
100
|
+
x * y
|
101
|
+
end
|
102
|
+
|
103
|
+
Contract Int, Num, String, RespondTo[:to_i] => Any
|
104
|
+
def args(a, b, c, d)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
contracts_obj = ContractsTest.new
|
108
|
+
|
109
|
+
class TypecheckTest
|
110
|
+
extend Typecheck
|
111
|
+
|
112
|
+
typecheck 'Numeric, Numeric -> Numeric',
|
113
|
+
def sum(x, y)
|
114
|
+
x + y
|
115
|
+
end
|
116
|
+
|
117
|
+
typecheck '#to_i, #to_i -> Numeric',
|
118
|
+
def mul(x, y)
|
119
|
+
x * y
|
120
|
+
end
|
121
|
+
|
122
|
+
typecheck 'Integer, Numeric, String, #to_i -> BasicObject',
|
123
|
+
def args(a, b, c, d)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
typecheck_obj = TypecheckTest.new
|
127
|
+
|
128
|
+
Benchmark.ips do |x|
|
129
|
+
x.report("pure") do |times|
|
130
|
+
i = 0
|
131
|
+
while i < times
|
132
|
+
pure_obj.sum(1, 2)
|
133
|
+
pure_obj.mul(1, 2)
|
134
|
+
pure_obj.args(1, 2, "c", 4)
|
135
|
+
i += 1
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
x.report("rtype") do |times|
|
140
|
+
i = 0
|
141
|
+
while i < times
|
142
|
+
rtype_obj.sum(1, 2)
|
143
|
+
rtype_obj.mul(1, 2)
|
144
|
+
rtype_obj.args(1, 2, "c", 4)
|
145
|
+
i += 1
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
if is_mri
|
150
|
+
x.report("rubype") do |times|
|
151
|
+
i = 0
|
152
|
+
while i < times
|
153
|
+
rubype_obj.sum(1, 2)
|
154
|
+
rubype_obj.mul(1, 2)
|
155
|
+
rubype_obj.args(1, 2, "c", 4)
|
156
|
+
i += 1
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
x.report("sig") do |times|
|
162
|
+
i = 0
|
163
|
+
while i < times
|
164
|
+
sig_obj.sum(1, 2)
|
165
|
+
sig_obj.mul(1, 2)
|
166
|
+
sig_obj.args(1, 2, "c", 4)
|
167
|
+
i += 1
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
x.report("contracts") do |times|
|
172
|
+
i = 0
|
173
|
+
while i < times
|
174
|
+
contracts_obj.sum(1, 2)
|
175
|
+
contracts_obj.mul(1, 2)
|
176
|
+
contracts_obj.args(1, 2, "c", 4)
|
177
|
+
i += 1
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
x.report("typecheck") do |times|
|
182
|
+
i = 0
|
183
|
+
while i < times
|
184
|
+
typecheck_obj.sum(1, 2)
|
185
|
+
typecheck_obj.mul(1, 2)
|
186
|
+
typecheck_obj.args(1, 2, "c", 4)
|
187
|
+
i += 1
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
x.compare!
|
192
|
+
end
|