3 ms·
This is incorrect. You are not defining a or b. You are defining c as either a or b. Declarative way: c = condition ? a : b Imperative way: c = null
by sbergot 4y ago
This is incorrect. You are not defining a or b. You are defining c as either a or b.
Declarative way: c = condition ? a : b
Imperative way:
c = null
if condition:
c = a
else:
c = b
- Simran-B 4y agoBut in many imperative languages you would equally use a ternary operator? c = condition ? a : b And in Python at least, it would be: if condition: c = a else: c = b (i.e. no upfront declaration of c)
- dwenzek 4y agoI would not say this is incorrect as there is no definition of `a: condition`. As being declarative is about expectations, one translation could be: I expect that "either both a and the condition to be true, or b true and the condition false" once the computation done. a when: condition b when: not condition
- aatd86 4y agoThere must still be an assignment somewhere. This is a definition after all. c = a if condition else c = b Whether c is implicit or not doesn't matter too much. I think the comment you respond to is still correct.
- throwaway290 4y agoYou can say that declarative style is only constant assignment. It doesn't make sense to use = though because (unlike imperative style) assignment is all there is, in a way