shiika-lang / shiika

A statically-typed programming language
MIT License
223 stars 15 forks source link

impl. `return` from a block #242

Open yhara opened 3 years ago

yhara commented 3 years ago

eg.

class A
  def self.foo
    [1,2,3].each do |i: Int|
      p i
      return if i %2 == 0
    end
    p "unreachable"  # this should not be printed
  end
end
yhara commented 3 years ago

On the other hand, I feel like the return below escapes from fn, not self.foo. Maybe return should behave differently in a fn and a block (like Ruby's lambda and proc.)

class A
  def self.foo
    f = fn{
      [1,2,3].each do |i: Int|
        p i
        return if i %2 == 0
      end
    }
    3.times{|i: Int| f.call}
  end
end
yhara commented 8 months ago

This is possible but not easy (needs something like exception handlling)

yhara commented 8 months ago

Another possibility is to use CPS(Continuation Passing Style).