Constructors must return self



  • So every programming language I've used until this year implicitly returns "this" at the end of your constructor. Automatically. For example (C# or Java):


    public class Foo

    {

        public Foo()

        {

            // Initialize some variables and stuff

        }

    }

    Well Objective-C makes you do this instead:

    @implementation Foo

    -(Foo*) init

    {

        self = [super init];

        // Initialize some variables and stuff

        return self;

    }

    @end

    Stupid, but I can deal with it.

    Except that the compiler doesn't give you an error if you forget to return self. And your app will crash with a BAD_ACCESS_EXCEPTION at the main method stack frame and tell you that <ABCSomeType> does not accept the "release" method where <ABCSomeType> is clearly part of a third-party framework you're using because ABC prefixes all classes in that framework. So you Google <ABCSomeType> and find absolutely nothing, meaning no one else has encountered this problem and that type is some kind of undocumented internal or private class. And you will waste hours trying to figure out what the problem is and even consider turning Automatic Reference Counting off because it is apparently over-releasing something, then you'll start to pull your hair out as you consider what your impatient bosses will say if you tell them you had a one-or-two-week setback because you had to turn ARC off and fix all your existing code with retain and release messages. Eventually by some miracle you notice that a constructor ("init" method in Objective-C) is forgetting to return self, and the evil thoughts you start thinking towards Apple make you wonder just what your potential is for turning into a psycopathic genocidal maniac given the right circumstances.

    Every other language/compiler in the world* already errors if you have a non-void method that doesn't return anything. But Apple hasn't figured out how to do that yet. To be fair there was a warning, but it got lost in a sea of warnings about "Such and such feature is not available prior to XCode 4.2". Awesome. I'm using XCode 4.2. Why exactly are you telling me this?

    TL;DR version: XCode gives you warnings for doing stuff that isn't supported in old versions of XCode yet allows you to compile code where non-void methods fail to return anything.

    P.S. I wish Apple would quit spending their $100 billion on trying to crush Android and instead use it to update their developer tools. It's not 1995 anymore, but you couldn't tell by using XCode. And they spend so much time making the Apple end-user experience shiny and awesome but then take a monster fifty-pound crap on developers.

    *Cue pages and pages of pedantic dickweedery and pointless debate. 



  • Disclamer: I'm not doing iOS development actively anymore (back to higher studies)

    I remember the return self being in the class file template. And it's about the first thing I learned in Obj-C that you should do that and don't ask questions about it. (Also iOS not having automatic garbage collection does not help in any way)

    I never got the "Not available before ..." warning. But I know It's possible to make apps that don't generate warnings (we used to have days dedicated to doing that). There is also the code analysis tool to help you with eventual references problems. It has some false positives but it almost never misses (we had 2-3 of them but it was really some crappy code that ended up to be refactored).

    But I will give you that XCode's tendency to crash is really annoying. And it could use some improvements (though the 4.x already improves the 3.x version)




  • I think the javascript debugger flags this for you. Why don't you just use that?

    /ducks



  • @mott555 said:

    So every programming language I've used until this year implicitly returns "this" at the end of your constructor. Automatically. For example (C# or Java):

    I take it then that you haven't worked with Perl "Objects". There are an awful lot of similarities, except that every one knows that Perl Objects are just a kludge thrown on top of an old non object oriented language and that Objective C was "designed" to be object oriented. Also, in Perl, the last value evaluated is returned, if there is no return statement.

     



  • Even if he had worked with PERL, PERL will fail to run if you have strict enabled (which you do right?). His point was less about it requiring the explicit return self, and more about it not failing when something that was required was missing.



  • @Rick said:

    @mott555 said:

    So every programming language I've used until this year implicitly returns "this" at the end of your constructor. Automatically. For example (C# or Java):

    I take it then that you haven't worked with Perl "Objects". There are an awful lot of similarities, except that every one knows that Perl Objects are just a kludge thrown on top of an old non object oriented language and that Objective C was "designed" to be object oriented. Also, in Perl, the last value evaluated is returned, if there is no return statement.

     

    Or Python, which always passes the "self" reference as this first parameter to methods, including the constructor.



  • Well, at least you get to program on a shiny Mac!

    I'd really like to try iOS programming but I'm sticking to Android right now

    1. setting up a dedicated Mac or hackintosh (VM)
    2. Objective C
    3. $100 / year


  • @Rick said:

    Objective C was "designed" to be object oriented

    You're implying that Objective-C was designed, as opposed to shat out by as person who hates developers? That's hilarious!



  • @The_Assimilator said:

    @Rick said:
    Objective C was "designed" to be object oriented

    You're implying that Objective-C was designed, as opposed to shat out by as person who hates developers? That's hilarious!

    More accurate would be to say Obj-C was "designed" to be object-oriented while also using a bog-standard non-OO C compiler. So it's basically like those guys who use 20,000 #define statements to turn a C compiler into a PASCAL compiler, but for some reason people take Obj-C more seriously.





  • @mott555 said:

    Every other language/compiler in the world* already errors if you have a non-void method that doesn't return anything. But Apple hasn't figured out how to do that yet. To be fair there was a warning
     

    I've had almost exactly the same problem with C++ compiled by gcc this week.
    If you don't have a return statement in a non-void function, it spits out a warning, but not an error. I was sloppily hacking together some throw-away code with a bazillion of warnings I ignored and ended up hunting this bug. IIRC, MSVC gives you a warning if some code paths don't have a return statement (because the standard allows it), but gives an error if there is no return in any code path. Which is much saner, IMO.

    And enabling -Werror isn't an option because I was explicitly ignoring the other warnings in the first place.



  • Hey, at least you guys HAVE constructors. UnrealScript has no concept of a constructor, and the nearest analogues, Auto State's Begin label or PreBeginPlay/PostBeginPlay, are kinda sketchy, overridden in most classes they'd be useful in (either by being bStatic which can be bypassed or redirected in native code behind the scenes which can't) also don't execute until you run the game. This might sound perfectly logical but given you can create and configure Actors in the editor you can poke values into the classes using the Actor Properties window but your object can't do anything to reflect its state (such as error checking the values you just entered before the game starts) without using native code behind the scenes. There IS an error handling system that can be invoked from the tools menu, but again, this runs on native code, so scripters can't access it.

    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARGH!


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.