Syntactic sugar for assigning Optional values and conditional assignment

Hi Team!
I hope you are doing well.
I'm sorry for such a minor ideas but for my project they will make code more compact.

  1. Assigning optional only if it has a value. E.g.:
    var a: Int?
    var b = 0
    instead of:
    if (a != nil ) { b = a }
    use short:
    b =? a
    I can do it by implementing a custom operator but I think it's good to have it in the standard library:
    infix operator =? : AssignmentPrecedence
    extension Int {
    static func =?(left: inout Int, right: Int?) {
    if right != nil { left = right! }}
    }
  2. The second idea for the conditional assignment I don't know how to implement by myself. E.g.
    let c = 0
    var d = 0
    we have a ternary operator for the case when we want to assign an alternative value in case some condition is false:
    d = c > 0 ? c : 1
    but if we don't want to use any alternative then we need to write code like this:
    if (c > 0) { d = c }
    My suggestion is to simplify the syntax somehow. E.g.:
    d = c > 0 ? c or d = c > 0 ? c : _
    I understand that the proposals are looking as they're out of thin air, but when you are reading dozens of parameters from the external source it's good to make it as short as possible.

Thank you in advance!

Best regards,
Vsevolod Migdisov

You can also do b = a ?? b

(it’s not quite the same as in b will be reassigned if a is nil, but the end result is the same i.e b will either contain the value stored in a or 0)

1 Like

I think this is just d = max(c, 1).

Similarly, you can write this as d = max(c, d), although I prefer the original.

Do you have any other examples where the syntactic sugar for ternary operations might offer an improvement? Syntactic sugar additions have a very high bar for entry into the language.

I do the same in my code for both proposals - if condition is false then I assign the variable itself.
But I think it looks not quite elegant:
b = a ?? b
d = c > 0 ? c : d

Both proposals are not just for Int. It can be applied to String as well or any other types:
if (str != "") { str = "some string")
The idea is that if the condition is false then no assignment should be done.
The same as in the 1st proposal - if the value is nil then don't do the assignment at all.

Okay. It looks like the =? change has been pitched before and rejected, so I’m not sure if it’s worth pursuing.

The other change you mentioned doesn’t look like it’s been pitched before, so perhaps you can just focus on that.

I personally don’t see a need to optimize the syntax for if (c > 0) { d = c } as I think it is perfectly fine to write and is readable.

Thanks a lot, Suyash!

Swift requires braces for all if statements, so parentheses aren't needed like they are in other languages.

if c > 0 { d = c }

As for the first example, use map! :world_map:

a.map { b = $0 }

I've found both solutions for myself but I still think it's good to have it in the standard library:
infix operator ?= : AssignmentPrecedence
func ?=< T >(lhs: inout T, rhs: T?) { if rhs != nil { lhs = rhs! } }
func ?=< T >(lhs: inout T, rhs: (Bool, T)) { if rhs.0 { lhs = rhs.1 } }

No please don't use map for side-effects! There should be a forEach but for optionals, but until we have that please let's all keep map functional!

3 Likes
Terms of Service

Privacy Policy

Cookie Policy