Re: Good idea/bad idea?
Re: Good idea/bad idea?
- Subject: Re: Good idea/bad idea?
- From: Alex Zavatone <email@hidden>
- Date: Thu, 24 Apr 2014 12:53:04 -0400
On Apr 24, 2014, at 11:21 AM, Roland King wrote:
> well not if it's actually
>
> x = (x) ?: @"yo";
>
> but
>
> x = (x) ? x : @yo" ;
>
> seems to be fine.
>
> nil is defined to be 0, 0x0, any other kind of zero you like.
>
> I don't like bools not being bools so I'd personally do
>
> x = (!x) ? @"yo" : x;
>
> or
>
> x = (!!x) ? x : @"yo" // preferred
>
> but it's all the same. What was it about this which worried you.
>
Well, actually omitting the first option in the ternary operator is stated to be legit and what I posted actually does appear to work. I've removed the parens as a result of a previous reply.
Here's a little more you can copy, paste and test:
NSString *x;
x = @"Hi";
x = x ?: @"yo";
NSLog(@"%@", x);
NSString *z;
//z = @"Hi";
z = z ?: @"hey";
NSLog(@"%@", z);
Just try commenting out and uncommenting the string assignments to x and z.
Seems to work as expected.
What worried me was that I've never seen this used in Objective-C in this manner. I've always used the ? operator to color the backgrounds of cells with alternating row colors. It seemed like a stretch to actually use it to achieve lazy instantiation and was wondering if there were any terrible demons I'd be summoning from the bowels of the compiler if we actually thought of using this approach.
I've added your preferred approach to this as well.
Seems like a nice little instantiation shorthand if this does not offend the compiler gods.
> On 24 Apr, 2014, at 11:08 pm, Alex Zavatone <email@hidden> wrote:
>
>> I guess my real question is "can we substitute the ternary operator for your if statement and if not, why?"
>>
>> Thanks man.
>>
>> On Apr 24, 2014, at 10:53 AM, Raheel Ahmad wrote:
>>
>>> Usually, I would access such variables as properties that are lazy loaded, in which case the accessing code is simply:
>>>
>>> self.x …
>>>
>>> and the accessor is
>>>
>>> - (NSString *) x {
>>> if (!_x) {
>>> _x = @“yo”;
>>> }
>>> return _x;
>>> }
>>>
>>> - Raheel
>>>
>>> On April 24, 2014 at 7:36:57 AM, Alex Zavatone (email@hidden) wrote:
>>>
>>>> I was just asked yesterday if there is any shorthand in Objective-C for "if this thing = nil, then instantiate a new instance from the class"
>>>>
>>>> Something like this:
>>>>
>>>> NSString x;
>>>>
>>>> if ([x isEqualtoString:nil]) {
>>>> x = @"yo";
>>>> }
>>>>
>>>> Feel free to replace NSString with any class.
>>>>
>>>> And we messed around a bit looking for any shorthand and though it looked like a terrible idea since the comparison is done against integers using the ternary operator, I'd like to know exactly why it's a terrible idea.
>>>>
>>>> NSString x;
>>>>
>>>> x = (x) ?: @"yo";
>>>>
>>>> Thanks in advance.
>>>> - Alex
>>>>
>>>> _______________________________________________
>>>>
>>>> Cocoa-dev mailing list (email@hidden)
>>>>
>>>> Please do not post admin requests or moderator comments to the list.
>>>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>>>
>>>> Help/Unsubscribe/Update your Subscription:
>>>>
>>>> This email sent to email@hidden
>>
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden