Hey guys
I’ve been study crystal and I’ve try to bring some ruby gems, that I like, in crystal.
So I tried to implement something like ruby micro-case(GitHub - serradura/u-case: Represent use cases in a simple and powerful way while writing modular, expressive and sequentially logical code.),
but I got a problem that I can’t solve that.
Basically, I have a Result class that has a value attribute and others.
class Result(U)
getter is_success : Bool
getter value : U
getter result_type : String
property is_proccessed : Bool
def initialize(@is_success : Bool, @value : U, @result_type = "ok")
@is_proccessed = false
end
def on_error(&block : Result(U) -> H) : Result(U) forall H
if !is_success && !is_proccessed
block.call(self)
self.is_proccessed = true
end
self
end
def on_success(&block : Result(U) -> H) : Result(U) forall H
if is_success && !is_proccessed
block.call(self)
self.is_proccessed = true
end
self
end
end
Result class has some chainable methods like on_error and on_success,
the propouse is to execute block when succesfully or a error block when fail.
To use that class I have created a DivisionCase class that’s make a simple math operation and returns a Hash with result
class DivisionCase
def self.call(one : Int32, two : Int32)
result = (one / two).to_f
success_data = {"division" => result}
Result(typeof(success_data)).new(true, success_data, "success")
rescue
data = {"error" => {"attr" => "attr error message here"}}
Result(typeof(data)).new(false, data, "error_type")
end
end
Putting everything together, we have something like this:
result = DivisionCase.call(1, 0)
.on_error() do |res|
pp "simple error"
end
.on_success() do |res|
p "Division #{res.value["division"]} "
end
And this doesn’t work, when I execute the code above I have the following error message:
error in line 13
Error: no overload matches 'Proc(Result(Hash(String, Float64)), String)#call' with type Result(Hash(String, Hash(String, String)))
Overloads are:
- Proc(*T, R)#call(*args : *T)
I tried to investigate what’s the problem and I figure out that error occurs when I put the rescue code, if I remove the rescue block, all the things is working.
I really don’t know how to do that, if someone can help, please tell me
Here is the code working (without a rescue block) Carcin
And here is the code that isn’t work Carcin