Skip to content

Language design question: value of block: #1841

@dbp

Description

@dbp

Pyret has good support for mutation, iteration, etc. This requires being able to have multiple expressions/statements per block. This is not allowed by a well-formedness check, so if I write:

fun sum(l :: List<Number>) -> Number:
  var acc = 0
  for each(n from l):
     acc := acc + n
  end
  acc
end

I'll get an error prompting me to wrap the body in block:.

I certainly can, as either:

fun sum(l :: List<Number>) -> Number block:
  var acc = 0
  for each(n from l):
     acc := acc + n
  end
  acc
end

or

fun sum(l :: List<Number>) -> Number:
  var acc = 0
  block:
    for each(n from l):
       acc := acc + n
    end
    acc
  end
end

But, it's a bit clumsy, and in the case I want to do multiple things inside the body of the for each loop, I have to add another block:.

fun sum-odd-indices(l :: List<Number>) -> Number block:
  var acc = 0
  var idx = 0
  for each(n from l) block:
     when num-modulo(idx,2) == 0:
       acc := acc + idx
     end
     idx := idx + 1
  end
  acc
end

Now, the reason why this exists is in order to produce an error when someone accidentally puts multiple expressions inside a block (but, not on the same line, as that triggers a different error). Or, intends the function to return two things, not realizing that this is impossible.

My question is -- is the error (which suggests they add block, which in the functional case is not helpful) actually worth it? vs. them having to discover by testing that the final expression in the function is what is returned.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions