[OT] Some neat ideas from the Kotlin language
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Sat Feb 20 06:34:17 PST 2016
On 2/20/16 5:04 AM, Daniel N wrote:
> On Friday, 19 February 2016 at 21:30:37 UTC, Yuxuan Shui wrote:
>>> I think the same applies to Swift. But I think you're supposed to use
>>> it like this:
>>>
>>> if let a = var {
>>> // a is no unwrapped
>>> }
>>
>> But you would need to give the unwrapped value a new name.
>>
>
> Swift also has guard, which solves this issue.
>
> func ex(a: Int?) {
> // unwrap (possible to add additional check if desired)
> guard let a = a where a > 0 else {
> return // fail
> }
>
> a.ok // a is now a plain validated Int
> }
>
I'm glad you pointed this out, I did not know this. Having to rename
variables all the time is quite annoying.
-Steve
More information about the Digitalmars-d
mailing list