> On 14 Jul 2015, at 23:18, Jens Alfke <
je...@mooseyard.com> wrote:
>
> It seems inconsistent that I can use a ‘guard’ statement to bind an optional value, but not to bind anything else. For example, if foo.color is an optional I can use
> guard let color = foo.color else { return } // works
> but if foo.count is an integer I can’t use
> guard let count = foo.count where foo.count > 0 else { return } // syntax error
Why? The former case unwraps an optional, the latter case does nothing that guard foo.count > 0 doesn't do.
> This would actually work as long as foo.count were an optional integer, which seems like a weird requirement in this case.
>
> I’m finding this is getting in the way of my using ‘guard let...’ as a uniform way of expressing preconditions in methods,
> because of course not all preconditions involve optionals.
>
> I know I can do
> let count = foo.count
> guard foo.count > 0 else { return }
> but it would feel more consistent (and compact!) to be able to use ‘guard let’ in all cases.
IF you allow
guard let something = somethingThatIsNotAnOptional where blah
you are also into
if let something = somethingThatIsNotAnOptional where blah
and
while let something = somethingThatIsNotAnOptional where blah
which kind of obfuscates the meaning a bit by putting the only thing that matters (the conditional) at the end.
if blah
{
let something = somethingThatIsNotAnOptional
...
}
is a bit easier to understand IMO