LoopPerfect / neither

Either and Maybe monads for better error-handling in C++ ↔️
MIT License
249 stars 18 forks source link

Implement iterator api #2

Open nikhedonia opened 7 years ago

nikhedonia commented 7 years ago

A Maybe is a container like vector but has either 0 or 1 element. This would be convenient:

for(auto x : maybe(1) ) {
  std::cout << x << std::endl; // prints 1
} 

for(auto x : maybe<int>() ) {
  //loop not entered
} 

Functional maps should never have sideeffects. For sideeffects this way should be preferred.

rvarago commented 5 years ago

First of all: Congratulations for this awesome lib and also for the other projects, I really enjoyed the motivation for buckaroo and I hope to collaborate whenever possible!

Cool! I'm working on it. All right?

Just a question: Are we willing to make it possible to change wrapped value inside the loop? For instance: In the example, set x = 10?

If yes, it might make more sense to only allow iterations over auto const x

nikhedonia commented 5 years ago

Are we willing to make it possible to change wrapped value inside the loop?

I don't like the idea of mutations. So the answer is no.

it might make more sense to only allow iterations over auto const x

I think in c++ we cannot enforce the const when iterating by-value but we can enforce it for by-reference.

We also need to consider to overload begin/end for the case where the maybe is a rvalue (temporary) or contains a move only type eg. unique_ptr.

I'm not sure if for will extend the lifetime of a temporary until the end of the iteration. I guess a small test should reveal that quickly

First of all: Congratulations for this awesome lib and also for the other projects, I really enjoyed the motivation for buckaroo and I hope to collaborate whenever possible!

Thanks, this means a lot to us!

Cool! I'm working on it. All right?

Awesome!