I am having trouble with some case statements and have tried to reduce the statements down for a bug but then they seem to work. Am I missing something simple?
Here is the case statement that is not compiling.
when Command::None exit when Command::Error exit 1 when Command::DisputeCreate if opts[:exp_date]? expiration_month, expiration_year = opts[:exp_date].split("/") expiration_date = Time.utc(year: expiration_year.to_i, month: expiration_month.to_i, day: 1) end dispute_klass = case opts[:status]? || "open" when "open" then Braintree::Operations::Dispute::Sandbox::OpenDispute when "won" then Braintree::Operations::Dispute::Sandbox::WonDispute when "lost" then Braintree::Operations::Dispute::Sandbox::LostDispute else raise "the status #{opts[:status]} is not a valid status" end dispute_klass.new( amount: opts.fetch(:amount, BT::Transaction::Sandbox::Amount.authorized), card_number: opts.fetch(:card_number, BT::Transaction::Sandbox::Dispute.card_number), card_expiration: opts.fetch(:card_expiration, BT::Transaction::Sandbox::Card.valid_expiration)
This fails with the error.
Error: no argument named 'amount'
Matches are:
- Reference.new()
All three operations have an initializer with and amount argument.
Here is an overly reduced example one with arguments and one without. Both seem to work.
https://play.crystal-lang.org/#/r/ab31
https://play.crystal-lang.org/#/r/ab33
https://play.crystal-lang.org/#/r/ab37
Issue seems to be when there is another subclass that isn’t accounted for in the case
.
1 Like
Oh wow thanks for figuring that out. Is this something already known by the core team?
For reference: Discussion continued on the issue tracker in Case with subclasses not working · Issue #10292 · crystal-lang/crystal · GitHub
TLDR; The behaviour is expected, but the error message is terrible.
3 Likes