Skip to content

Is it possible to make this function generic? #570

Answered by TimWhiting
codehz asked this question in Q&A
Discussion options

You must be logged in to vote

You don't actually have to add div to the function argument type in this case. Just the result type.

fun collect-arr(action: () -> yield<a> ()): div list<a> {
  var acc := ctx _
  handle(action) fun yield(i) {
    acc := acc ++ ctx Cons(i, _)
  }
  acc ++. Nil;
}

There is no change in behavior of the program. The div effect signifies that something can diverge (not terminate). It doesn't say that it must diverge, it is just saying that in pathological cases it might.

However, in this particular case it seems to be an issue with local variable inference. You can switch it to explicitly use a heap allocated reference, and it can infer a non-divergent type:

fun collect-arr(action: () -> yie…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@TimWhiting
Comment options

Answer selected by codehz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants