Search code examples
c#swiftexpression-bodied-members

Does Swift support Expression-bodied members?


In C#, there's a feature called 'Expression-bodied members' which lets you use an expression to satisfy the body of a function/method or property. For instance, instead of writing these...

void PushItem(object item) {
    someStack.push(item)
}

int AddFour(int x) {
    return x + 4;
}

int SomeProperty{
    get{ return _someProperty; }
    set{ _someProperty = value; }
}

You can just write these...

void PushItem(object item) => someStack.push(item)

int AddFour(int x) => x + 4;

int SomeProperty{
    get => _someProperty;
    set => _someProperty = value;
}

Wondering if Swift has anything similar. It really keeps the code nice, lean and readable.


Solution

  • Looking into swift documentation in closures section,

    I have never came across something like this,

    Even on the side menu there are no signs of anything similar to that

    Swift doesn't support that feature and its not clear if it will be in the future.

    Read more about Swift here