rethinkdb / rethinkdb

The open-source database for the realtime web.
https://rethinkdb.com
Other
26.76k stars 1.86k forks source link

switch / if-elseif-elseif-else term #3199

Closed danielmewes closed 9 years ago

danielmewes commented 10 years ago

Currently the r.branch() term isn't that great to use if you have more complex case distinctions.

Proposed by @wojons: Add a variadic term taking 2N+1 arguments

r.switch(cond1, val1, cond2, val2, ..., condN, valN, default_val)

Its semantics are those of the following code block in C:

if (cond1) return val1;
else if (cond2) return val2;
...
else if (condN) return valN;
else return default_val;

We could also make r.branch() variadic, instead of introducing a new term.

wojons commented 10 years ago

@danielmewes

Thank you, One thing i want to add. I think it would be important for that after the default value you can submit another aarray or object that allows u to control if its a if else or if its a switch. Or a r.break term will need to be added so it will stop letting things fall though.

timmaxw commented 10 years ago

I think that we should simply allow r.branch() to take 2N+1 arguments where N>=1. The current behavior of r.branch() is simply the N=1 case.

wojons commented 10 years ago

i think thise is ur guys call on how you want to do this.....

gchpaco commented 10 years ago

As far as bike shedding goes, multi-branch if statements mean "cond" to my brain. I could live with switch. Branch is a bit weird, but I could probably get used to it in time.

timmaxw commented 10 years ago

I don't really like the name r.branch either (I wanted to call it r.if_then_else) but given that we already have the command r.branch, I think we shouldn't introduce a new separate command.

mlucy commented 10 years ago

Another vote for making branch more powerful rather than adding another term. We already have so many that it's hard to keep track of them all.

danielmewes commented 10 years ago

Let's finalize this during the 1.17 ReQL discussion period, not now...

wojons commented 10 years ago

The only reason that i would recommend r.switch over r.branch is as a user first looking at rethinkdb and trying to find a way to do condations r.branch does not sound like its going to be for what i amlooking for i think it was @AtnNn that showed me that command. But as a devloper seeing a command called switch sounds like a switch statment.i would ad r.switch and deperatcate r.branch.

coffeemug commented 9 years ago

:+1: for extending r.branch and not introducing a new term.

deontologician commented 9 years ago

extending r.branch seems obviously good.

wojons commented 9 years ago

I think r.branch sounds more like its forking something but fair enough for extending it as long as i dont have to use 10 of them.

VeXocide commented 9 years ago

Extending r.branch seems obvious, are we considering renaming it as well?

danielmewes commented 9 years ago

I'd like to keep the renaming discussion separate and for later.

danielmewes commented 9 years ago

Marking as settled for making branch variadic to support multiple branches.

pilwon commented 9 years ago

:+1: fantastic idea to make r.branch more powerful!

encryptio commented 9 years ago

Done in next as of bf5b28b, CR 3201

mlucy commented 9 years ago

@encryptio -- is there a docs issue?

encryptio commented 9 years ago

@mlucy Writing an issue out now.