jakt icon indicating copy to clipboard operation
jakt copied to clipboard

Consider not providing the `++`/`--` operators

Open kud1ing opened this issue 2 years ago • 8 comments

Jakt seems to provide the ++/-- operators, e.g. used in https://github.com/SerenityOS/jakt/blob/main/samples/control_flow/continue.jakt

Swift had them but removed them, see the discussion in https://github.com/apple/swift-evolution/blob/master/proposals/0004-remove-pre-post-inc-decrement.md

kud1ing avatar Jun 10 '22 10:06 kud1ing

Having my roots in Python: agreed, this is not needed, nothing wrong with += 1.

linusg avatar Jun 10 '22 18:06 linusg

imo we should keep it, at least the postincr one, and have it evaluate to the previous value, I find myself writing a lot of (the equivalent of) this in a lot of places

let foo = .things[.index++]

With that op removed, we'd have to also come up with a useless name

let index = .index
.index += 1 // or .index++ a.la Go
let foo = .things[index]

Which is just inelegant

Seems to me like the original problem is with ++foo/foo++ used as a statement?

alimpfard avatar Jun 10 '22 18:06 alimpfard

Also, Swift has custom operators, which makes that removal tolerable for anyone that cares now (https://grep.app/search?q=prefix%20operator%20%5C%2B%5C%2B%5B%5E%2B%3D%2A-%5D&regexp=true&filter[lang][0]=Swift)

And as the last part of my argument, I think we can trust the serenity developers to remain as they are with regards to "cute" and "clever" usages of such operators - it's not as if we're designing this language for anyone else

alimpfard avatar Jun 10 '22 19:06 alimpfard

at least the postincr one

That seems like a decent compromise, one of the major issues I have with prefix/postfix increment/decrement is that they do slightly different things in a very similar looking way (and that they create a different way of saying +=, but granted you can't use that as an expression).

Also, from a syntax POV ++.index looks horrendous IMO, and that's actual code from the selfhosted compiler. :flushed:

And as the last part of my argument, I think we can trust the serenity developers to remain as they are with regards to "cute" and "clever" usages of such operators

Sure, as a first step how would you feel about making += the default recommended style for standalone statements and only using ++ where actually needed for said cute / clever code?

linusg avatar Jun 10 '22 19:06 linusg

Sure, as a first step how would you feel about making += the default recommended style

I think even banning foo++ used as a statement is okay, I have no problems with using foo += 1 for statements - if anything, that's more clear

alimpfard avatar Jun 10 '22 20:06 alimpfard

With that op removed, we'd have to also come up with a useless name

let index = .index
.index += 1 // or .index++ a.la Go
let foo = .things[index]

Which is just inelegant

I disagree, it's just two lines:

let foo = .things[.index]
.index += 1

I'm in favour of removing ++ entirely, I don't think it adds much, but it does encourage people to write confusing code. Though, just having postincrement without preincrement is still a big improvement IMO.

AtkinsSJ avatar Jun 11 '22 08:06 AtkinsSJ

That doesn't have the same semantics in C++, and I didn't really think about the fact that Jakt does not have references.

Though imo removing an op that hasn't been abused in 3 years of serenity C++ just because it can be is silly, particularly as it comes with a significant readability improvement (imo) if used correctly.

On another note, we want to have member fns on primitives eventually as I understand, so what's the difference between x.preincrement() and x++ really? Or more realistically, between exchange(x, x+1) and x++?

People can write confusingly clever code whether or not this one operator exists, is what I'm getting at

alimpfard avatar Jun 11 '22 09:06 alimpfard

That doesn't have the same semantics in C++

Ah, my lack of C++ knowledge is showing. :sweat_smile:

AtkinsSJ avatar Jun 11 '22 09:06 AtkinsSJ