• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Any way to get a warning if a non-boolean type is used in an if expression?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Any way to get a warning if a non-boolean type is used in an if expression?


  • Subject: Re: Any way to get a warning if a non-boolean type is used in an if expression?
  • From: Jens Alfke <email@hidden>
  • Date: Fri, 18 Oct 2013 07:50:41 -0700


On Oct 18, 2013, at 1:36 AM, Quincey Morris <email@hidden> wrote:

Yet I'm not sure that you've thought this through entirely. In order for 'if (self.active)' to escape your projected warning, it would have to be referring to a property of type 'bool'.

Yes, that _is_ what Rick was thinking in the moment, which is why he wrote the _expression_ the way he did.

This means that, to write a syntactically immaculate test, you'd need to know in advance what the type of property this is, out of a range of plausible guesses. If you know what it is, you're unlikely to write 'self.active' when you mean 'self.active.boolValue’.

*Facepalm* I really didn’t think Rick’s post was hard to understand … what’s going on here?

Rick did not mean to write “self.active.boolValue”. That is not the point at all. If he had remembered in the moment that ‘active’ was of type ‘NSNumber’, he would have written the proper test. His mistake was misremembering the type and assuming ‘active’ was of type ‘BOOL’ and writing the conditional that way.

...recent thread where Jens (unintentionally) hilariously complained that if he (accidentally) forgot to provide the implementation of a custom property, his app didn't work. (Apps usually don't work if you leave out the code. We feel his pain.)

“Hilariously?” Again, I don’t think you’re understanding the issue. I didn’t complain that the app didn’t work, I complained that the compiler let the app build at all.

If you declare a C function Foo( ), and call that function, and then build, you get a link error and the app won’t run.
If you declare an extern variable and don’t provide a definition, again you get a link error.
If you declare an Objective-C method “-foo” in an @interface and then don’t implement it in your @implementation, you get a compile error.

The odd situation is that, with newer compilers, if you declare an Objective-C property and don’t implement the getter/setter, and don’t even issue an @synthesize or @dynamic directive, the compiler _doesn’t_ complain but decides that you meant to synthesize it, and the program builds and runs, but with unexpected semantics (if you intended to have a custom getter/setter.)

In other words, the default behavior of property declarations is now inconsistent with pretty much anything else you can declare — instead of issuing an error if you don’t give a definition, it goes ahead and makes up a default one for you. I will agree that this is often a useful shortcut, but it lets you make mistakes that can bite pretty hard.

Anyway, to bring this back to the original topic, Clang _does_ provide an optional warning to detect this situation, for people like me who don’t like the new behavior. Which is analogous to what Rick is asking for with ‘if’ statements (only the behavior’s been around a lot longer…)

—Jens
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Any way to get a warning if a non-boolean type is used in an if expression?
      • From: Quincey Morris <email@hidden>
References: 
 >Re: Any way to get a warning if a non-boolean type is used in an if expression? (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: Any way to get a warning if a non-boolean type is used in an if expression?
  • Next by Date: Re: Any way to get a warning if a non-boolean type is used in an if expression?
  • Previous by thread: Re: Any way to get a warning if a non-boolean type is used in an if expression?
  • Next by thread: Re: Any way to get a warning if a non-boolean type is used in an if expression?
  • Index(es):
    • Date
    • Thread