Skip to content

Commit 29d5c30

Browse files
committed
chore: add types file, update sig for Option
1 parent 2e1ae80 commit 29d5c30

9 files changed

Lines changed: 116 additions & 46 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
1515
## Usage
1616

1717
```ruby
18-
require "rs/result"
18+
require "rs/types"
1919

2020
x = Some.new(2)
2121
x = None.new

Steepfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ target :lib do
66
signature "sig"
77

88
# Directory name
9-
check "lib"
9+
check "lib/rs/types"
1010
# ignore "lib/templates/*.rb"
1111

12-
# library "pathname" # Standard libraries
12+
# library "rs/types" # Standard libraries
1313
# library "strong_json" # Gems
1414

1515
# configure_code_diagnostics(D::Ruby.default) # `default` diagnostics setting (applies by default)

lib/rs/option.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative "result/version"
4+
45
module Rs
56
# https://doc.rust-lang.org/std/option/index.html
67
# https://doc.rust-lang.org/std/option/enum.Option.html

lib/rs/types.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "option"
4+
require_relative "result"

lib/rs/types/T.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
class T
4+
def initialize(value)
5+
@value = value
6+
end
7+
8+
def inspect
9+
"T[#{@value.class}]"
10+
end
11+
end
12+
13+
class TE
14+
def initialize(value, error)
15+
@value = value
16+
@error = error
17+
end
18+
19+
def inspect
20+
"TE[#{@value.class}, #{@error.class}]"
21+
end
22+
end
23+
24+
# p T.new(0)
25+
# p TE.new(0, "a")
26+
27+
# p = Proc.new { |x| p x }
28+
# l = lambda { |x| p x }
29+
# a1 = -> (x) { p x }
30+
31+
# a1.(6, 0)

sig/rs/option.rbs

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ module Rs
22
# https://doc.rust-lang.org/std/option/index.html
33
# https://doc.rust-lang.org/std/option/enum.Option.html
44
# https://doc.rust-lang.org/src/core/option.rs.html
5-
class Option
5+
class Option[V]
66
class TypeError < StandardError
7-
def initialize: (untyped value_type) -> void
7+
# wanted Class[V]
8+
# no need declare new type V
9+
def initialize: [V] (T[V] value_type) -> void
810
end
911

1012
class TypeNilClass < StandardError
@@ -19,80 +21,93 @@ module Rs
1921
class UnwrapNone < StandardError
2022
end
2123

22-
def is_some: () -> untyped
24+
def is_some: () -> bool
2325

24-
def is_some_and: () { (?) -> untyped } -> (untyped | false)
26+
def is_some_and: () { (V) -> bool } -> bool
2527

26-
def is_none: () -> untyped
28+
def is_none: () -> bool
2729

28-
def is_none_or: () { (?) -> untyped } -> (true | untyped)
30+
def is_none_or: () { (V) -> bool } -> bool
2931

30-
def expect: (untyped msg) -> untyped
32+
def expect: (String msg) -> V
3133

32-
def unwrap: () -> untyped
34+
def unwrap: () -> V
3335

34-
def unwrap_or: (untyped default) -> untyped
36+
def unwrap_or: (V default) -> V
3537

36-
def unwrap_or_else: () { (?) -> untyped } -> untyped
38+
def unwrap_or_else: () { () -> V } -> V
3739

38-
def map: (untyped value_type) { (?) -> untyped } -> untyped
40+
def map: [U] (T[U] value_type) { (V) -> U } -> Option[U]
3941

40-
def tap: () { (?) -> untyped } -> self
42+
def tap: () { (V) -> bool } -> self
4143

42-
def map_or: (untyped default) { (?) -> untyped } -> untyped
44+
def map_or: [U] (U default) { (V) -> U } -> U
4345

44-
def map_or_else: (untyped default) { (?) -> untyped } -> untyped
46+
# work
47+
# type int_or_string = Integer | String
48+
#
49+
# not work
50+
# type ld = lambda { () -> U }
51+
# type ld = lambda { -> U }
52+
# type pd = Proc(U)
53+
#
54+
# want, crystal like
55+
# type D = -> U
56+
# type F = (V) -> U
57+
# type F = V -> U
58+
def map_or_else: [U] (Proc default) { (V) -> U } -> U
4559

46-
def ok_or: (untyped error) -> untyped
60+
def ok_or: [E] (E error) -> Rs::Result[V, E]
4761

48-
def ok_or_else: () { (?) -> untyped } -> untyped
62+
def ok_or_else: [E] () { () -> E } -> Rs::Result[V, E]
4963

50-
def and: (untyped other) -> untyped
64+
def and: [U] (Option[U] other) -> Option[U]
5165

52-
def and_then: (untyped value_type) { (?) -> untyped } -> untyped
66+
def and_then: [U] (T[U] value_type) { (V) -> Option[U] } -> Option[U]
5367

54-
def select: () { (?) -> untyped } -> untyped
68+
def select: () { (V) -> bool } -> bool
5569

56-
def or: (untyped other) -> (self | untyped)
70+
def or: (self other) -> self
5771

58-
def or_else: () { (?) -> untyped } -> (self | untyped)
72+
def or_else: () { () -> self } -> self
5973

60-
def xor: (untyped other) -> untyped
74+
def xor: (self other) -> self
6175

62-
def self.from: (untyped value_type) { (?) -> untyped } -> untyped
76+
def self.from: [V] (T[V] value_type) { () -> V } -> self
6377

64-
def self.from?: (untyped value_type) { (?) -> untyped } -> untyped
78+
def self.from?: [V] (T[V] value_type) { () -> V? } -> self
6579

66-
def self.from!: (untyped value_type) { (?) -> untyped } -> untyped
80+
def self.from!: [V] (T[V] value_type) { () -> V } -> self
6781
end
6882
end
6983

70-
class Some < Rs::Option
71-
@value: untyped
84+
class Some[V] < Rs::Option[V]
85+
@value: V
7286

73-
@value_type: untyped
87+
# wanted Class[V]
88+
@value_type: T[V]
7489

75-
attr_reader value_type: untyped
90+
attr_reader value_type: T[V]
7691

77-
def self.[]: (untyped value_type) { (?) -> untyped } -> untyped
92+
def self.[]: [V] (T[V] value_type) { () -> V } -> self
7893

7994
def inspect: () -> ::String
8095

81-
def ==: (untyped other) -> untyped
96+
def ==: (Rs::Option[V] other) -> bool
8297

83-
def initialize: (untyped value) -> void
98+
def initialize: (V value) -> void
8499
end
85100

86-
class None < Rs::Option
87-
@value_type: untyped
101+
class None[V] < Rs::Option[V]
102+
@value_type: T[V]
88103

89-
attr_reader value_type: untyped
104+
attr_reader value_type: T[V]
90105

91-
def self.[]: (untyped value_type) -> untyped
106+
def self.[]: [V] (T[V] value_type) -> self
92107

93108
def inspect: () -> ::String
94109

95-
def ==: (untyped other) -> untyped
110+
def ==: (Rs::Option[V] other) -> bool
96111

97-
def initialize: (?untyped value_type) -> void
112+
def initialize: (?T[V] value_type) -> void
98113
end

sig/rs/result.rbs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ module Rs
22
# https://doc.rust-lang.org/std/result/index.html
33
# https://doc.rust-lang.org/std/result/enum.Result.html
44
# https://doc.rust-lang.org/src/core/result.rs.html
5-
class Result
5+
class Result[V, E]
6+
VERSION: String
7+
68
class TypeError < StandardError
79
def initialize: (untyped value_type, untyped error_type) -> void
810
end
@@ -74,7 +76,7 @@ module Rs
7476
end
7577
end
7678

77-
class Ok < Rs::Result
79+
class Ok[V, E] < Rs::Result[V, E]
7880
@value: untyped
7981

8082
@value_type: untyped
@@ -94,7 +96,7 @@ class Ok < Rs::Result
9496
def initialize: (untyped value) -> void
9597
end
9698

97-
class Err < Rs::Result
99+
class Err[V, E] < Rs::Result[V, E]
98100
@error: untyped
99101

100102
@error_type: untyped

sig/rs/types/T.rbs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class T[T]
2+
@value: T
3+
4+
def initialize: (T value) -> void
5+
6+
# every thing is object
7+
# why raise error Type `E` does not have method `class`
8+
def inspect: () -> ::String
9+
end
10+
11+
class TE[T, E]
12+
@value: T
13+
14+
@error: E
15+
16+
def initialize: (T value, E error) -> void
17+
def inspect: () -> ::String
18+
end

spec/spec_helper.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "rs/option"
4-
require "rs/result"
3+
require "rs/types"
54

65
RSpec.configure do |config|
76
# Enable flags like --only-failures and --next-failure

0 commit comments

Comments
 (0)