Re: Calling getter on const object

2010-11-09 Thread Eeyore
I'm new at this too, so perhaps I am completely off here. My guess is that it 
isn't the const that is messing this up, but the non-class type. The problem I 
see is that "frame" is a pointer to an objc_object and objc_objects aren't the 
same as NSObjects (but, like I said, I'm new, so maybe they are interchangeable 
here). Does it work if you try declaring frame as a NSObject 
instead of an id?

Aaron


On Nov 8, 2010, at 8:37 AM, Jonny Taylor wrote:

> I'm afraid I'm not sure if I understand exactly what you're asking. I would 
> like to be able access the "frame number" property (which is in fact stored 
> as a variable within the class, and is fixed soon after instantiation) in 
> spite of the fact that I only have a 'const' pointer to the object at the 
> time I want to access it. Semantically I don't see why it should be unsafe to 
> attempt to find out the value of that property.
> 
> Either way, hopefully the actual code helps to answer your question:
> 
> -(void)analyzeFrameAsync:(id)frame
> {
>   int num = frame.frameNumber;// Temporary variable avoid 
> problems with calling method on 'frame' [const] from block
>   dispatch_async(analysisSerialQueue,
>   ^{
>   [analyzer processFrame:frame];
>   self.latestFrameNumberProcessed = num;  // I can't call 
> frame.frameNumber from within the block (compiler error)
>   });
> }
> 
> 
> 
> On 8 Nov 2010, at 16:29, David Duncan wrote:
> 
>> It might be useful to know what you expect to get from the const reference 
>> in the first place? Primarily because I highly suspect that it is not 
>> necessary to ensure the semantics that you desire.
>> 
>> On Nov 8, 2010, at 4:59 AM, Jonny Taylor wrote:
>> 
>>> I have encountered what I presume is a common newcomer's problem, but I 
>>> haven't had any luck with google (maybe I'm using the wrong search terms?). 
>>> Suppose I have a const id, e.g. "const id frame". If I 
>>> attempt to call a getter for the object I get the following compiler error:
>>> request for member 'frameNumber' in 'frame', which is of non-class type 
>>> 'objc_object* const'
>> 
>> --
>> David Duncan
>> 
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/eeyore%40monsterworks.com
> 
> This email sent to eey...@monsterworks.com
> 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-09 Thread Sean McBride
On Tue, 9 Nov 2010 12:08:57 +, Jonny Taylor said:

>For what it's worth, my bug has been closed with the comment "Please
>upgrade to the LLVM Compiler when Xcode 4 is released". Hopefully it
>won't be too long now, and if all the blocks business is sorted that
>will make me very happy!

You can build and use clang yourself now:


--

Sean McBride, B. Eng s...@rogue-research.com
Rogue Researchwww.rogue-research.com
Mac Software Developer  Montréal, Québec, Canada


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-09 Thread Jonny Taylor
>>> I now think that what I am seeing is a minor parsing sort of issue within 
>>> the compiler (gcc 4.2) when mixing id with blocks. 
>> The more fundamental problem is that gcc C++ does not fully support blocks, 
>> period.
> Indeed, as soon as it gets a sniff of c++ near a block things start to go 
> badly wrong, but I would have hoped that "pure objC" compiled with a c++ 
> compiler would pass - and especially pure objC in a .m file (which also leads 
> to a different error). Oh well, I'll add another blocks-related bug report. I 
> find blocks to be an endless source of bleeding-edge torment, but I like them 
> too much to give them up!
For what it's worth, my bug has been closed with the comment "Please upgrade to 
the LLVM Compiler when Xcode 4 is released". Hopefully it won't be too long 
now, and if all the blocks business is sorted that will make me very 
happy!___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-08 Thread Jonny Taylor
On 8 Nov 2010, at 20:11, Greg Parker wrote:
>> I now think that what I am seeing is a minor parsing sort of issue within 
>> the compiler (gcc 4.2) when mixing id with blocks. 
> The more fundamental problem is that gcc C++ does not fully support blocks, 
> period.
Indeed, as soon as it gets a sniff of c++ near a block things start to go badly 
wrong, but I would have hoped that "pure objC" compiled with a c++ compiler 
would pass - and especially pure objC in a .m file (which also leads to a 
different error). Oh well, I'll add another blocks-related bug report. I find 
blocks to be an endless source of bleeding-edge torment, but I like them too 
much to give them up!___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-08 Thread Greg Parker
On Nov 8, 2010, at 10:53 AM, Jonny Taylor wrote:
> I now think that what I am seeing is a minor parsing sort of issue within the 
> compiler (gcc 4.2) when mixing id with blocks. 

The more fundamental problem is that gcc C++ does not fully support blocks, 
period.


-- 
Greg Parker gpar...@apple.com Runtime Wrangler


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-08 Thread Jonny Taylor
> On Mon, Nov 8, 2010 at 10:53 AM, Jonny Taylor  wrote:
>>// Both these have errors: "request for member 'frameNumber' 
>> in 'm', which is of non-class type 'objc_object* const'"
>>// when compiled as ObjC++ (.mm file).
> 
> Aha, this is an important point. That's why I didn't immediately pick
> up on the actual problem you were facing.
> 
> Clang parses this without error. It looks like GCC was just never
> updated to handle properties in protocol definitions.
Yes, sorry - I hadn't realised that bit was important at the time... though as 
I noted there is a different (and also, I believe, erroneous) error for a .m 
file___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-08 Thread Kyle Sluder
On Mon, Nov 8, 2010 at 10:53 AM, Jonny Taylor  wrote:
>                // Both these have errors: "request for member 'frameNumber' 
> in 'm', which is of non-class type 'objc_object* const'"
>                // when compiled as ObjC++ (.mm file).

Aha, this is an important point. That's why I didn't immediately pick
up on the actual problem you were facing.

Clang parses this without error. It looks like GCC was just never
updated to handle properties in protocol definitions.

--Kyle Sluder
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-08 Thread Jonny Taylor
> I'm new at this too, so perhaps I am completely off here. My guess is that it 
> isn't the const that is messing this up, but the non-class type. The problem 
> I see is that "frame" is a pointer to an objc_object and objc_objects aren't 
> the same as NSObjects (but, like I said, I'm new, so maybe they are 
> interchangeable here). Does it work if you try declaring frame as a 
> NSObject instead of an id?
It was your suggestion here that got me thinking along the right track, in 
fact, but that declaration does not actually make any sense as far as I know - 
that is not the normal way of declaring a generic pointer that conforms to a 
protocol, and it leads to a whole load of warnings/errors elsewhere in the 
code...___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-08 Thread Jonny Taylor
Thankyou everybody for your help (and patience!) with this one. I have indeed 
been making some wrong assumptions and as I result much of what I was writing 
was incorrect. From the various helpful replies I have received, I have (I 
hope!) got a much clearer understanding of what is and isn't going on.

I now think that what I am seeing is a minor parsing sort of issue within the 
compiler (gcc 4.2) when mixing id with blocks. In the example 
code below, everything works ok except for two lines, which lead to compile 
errors. [The mention of 'const' in the error was what was leading to my 
incorrect claims earlier, since that had got me thinking the problem was 
related to how you can't by default modify variables declared outside blocks. 
Sorry!].

I believe the compiler should not be objecting to the lines that it reports 
errors for...
Jonny

= BEGIN CODE =

// Declare a class with a property
@interface MyClass
@property (readwrite) int frameNumber;
-(int)frameNumber2; // Try declaring a straight function 
just to see if it makes a difference
@end

void foo(MyClass *m)
{
printf("%d %d n", m.frameNumber, m.frameNumber2);   // ok

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
printf("%d %d\n", m.frameNumber, m.frameNumber2);   // ok
});
}

// Try the same, but with a protocol rather than a concrete class type
@protocol MyProtocol
@property (readwrite) int frameNumber;
-(int)frameNumber2;
@end

void foo2(id m)
{
printf("%d %d n", m.frameNumber, m.frameNumber2);   // ok
printf("%d %d\n", [m frameNumber], [m frameNumber2]);   // ok


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
// Both these have errors: "request for member 'frameNumber' in 
'm', which is of non-class type 'objc_object* const'"
// when compiled as ObjC++ (.mm file).
// I get a different error: "request for member 'frameNumber' 
in something not a structure or union"
// when compiled as ObjC (.m file).
printf("%d\n", m.frameNumber);
printf("%d\n", m.frameNumber2);
// Both these are ok
printf("%d %d\n", [m frameNumber], [m frameNumber2]);
});
}

= END CODE =

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-08 Thread Kyle Sluder
On Mon, Nov 8, 2010 at 8:37 AM, Jonny Taylor  wrote:
> I'm afraid I'm not sure if I understand exactly what you're asking. I would 
> like to be able access the "frame number" property (which is in fact stored 
> as a variable within the class, and is fixed soon after instantiation) in 
> spite of the fact that I only have a 'const' pointer to the object at the 
> time I want to access it. Semantically I don't see why it should be unsafe to 
> attempt to find out the value of that property.

Since you haven't posted enough code for me to reproduce your exact
scenario, I've made a different demo: https://gist.github.com/667967

I don't get the same error you get. Rather, when compiling with GCC I
get "request for member ‘blah’ in something not a structure or union".
With clang, I get no errors at all. Constness doesn't come up here.

--Kyle Sluder
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-08 Thread David Duncan
On Nov 8, 2010, at 9:04 AM, Jonny Taylor wrote:

> In this particular case I don't have much choice in the matter - the standard 
> behaviour of blocks is to provide 'const' access to variables declared 
> outside the block (and with good reason I think). In the example I gave, I 
> want to access the property from within a block. I can work around it as 
> shown in my example, or declare the function parameter (frame object pointer) 
> with the __block prefix, but I feel that neither should be necessary.

Blocks don't redeclare values as 'const', it makes copies of those values for 
the block's own use. That is what is meant by providing 'const' access – if you 
change the variables, you won't see that change reflected in the originals. For 
objects the copied value is just a pointer (the object will be retained) so the 
'const' access is usually a moot point, since you don't care about the pointer 
not changing, you care about the object not changing. There is no direct 
support for const objects in Blocks – if you have an object pointer, you are 
free to change the object to your heart's content.

So as I surmised when I asked the question, you don't need the 'const' modifier 
at all. Just declare and use the variable normally.
--
David Duncan

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-08 Thread Jonny Taylor
In this particular case I don't have much choice in the matter - the standard 
behaviour of blocks is to provide 'const' access to variables declared outside 
the block (and with good reason I think). In the example I gave, I want to 
access the property from within a block. I can work around it as shown in my 
example, or declare the function parameter (frame object pointer) with the 
__block prefix, but I feel that neither should be necessary.

I extended my question to the more general case of a getter on a const object 
because (maybe because I come from a c++ background?) I am in the habit of 
declaring object parameters as const if I do not expect them to change within a 
function, just to make it clear to myself what is or isn't happening, etc. I 
have situations where I don't see why I shouldn't declare a function parameter 
as 'const', because nothing within it is changing, but if I want to "read" 
*any* state of the object in objective c (via a getter) it seems that I cannot 
declare it const... which seems rather odd to me.

If you'll forgive my thinking in c++ terms, the equivalent in c++ would be to 
write something like:

int Frame::FrameNumber() const { return frameNumber; }

void SomeFunction(const Frame *frame)
{
int num = frame->FrameNumber(); // Would not be permitted in the objC 
equivalent, since 'frame' is const
}

// or one might alternatively write

void AnotherFunction(Frame *frame)
{
dispatch_async(analysisSerialQueue,
^{
// Do some processing
latestFrameNumberProcessed = frame->FrameNumber();  
// Would not be permitted either in objC since frame becomes const within the 
block
});
}


On 8 Nov 2010, at 16:53, David Duncan wrote:

> My question is why do you need to have a 'const' pointer.
> 
> On Nov 8, 2010, at 8:37 AM, Jonny Taylor wrote:
> 
>> I'm afraid I'm not sure if I understand exactly what you're asking. I would 
>> like to be able access the "frame number" property (which is in fact stored 
>> as a variable within the class, and is fixed soon after instantiation) in 
>> spite of the fact that I only have a 'const' pointer to the object at the 
>> time I want to access it. Semantically I don't see why it should be unsafe 
>> to attempt to find out the value of that property.
> 
> --
> David Duncan
> 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-08 Thread David Duncan
My question is why do you need to have a 'const' pointer.

On Nov 8, 2010, at 8:37 AM, Jonny Taylor wrote:

> I'm afraid I'm not sure if I understand exactly what you're asking. I would 
> like to be able access the "frame number" property (which is in fact stored 
> as a variable within the class, and is fixed soon after instantiation) in 
> spite of the fact that I only have a 'const' pointer to the object at the 
> time I want to access it. Semantically I don't see why it should be unsafe to 
> attempt to find out the value of that property.

--
David Duncan

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-08 Thread Jonny Taylor
I'm afraid I'm not sure if I understand exactly what you're asking. I would 
like to be able access the "frame number" property (which is in fact stored as 
a variable within the class, and is fixed soon after instantiation) in spite of 
the fact that I only have a 'const' pointer to the object at the time I want to 
access it. Semantically I don't see why it should be unsafe to attempt to find 
out the value of that property.

Either way, hopefully the actual code helps to answer your question:

-(void)analyzeFrameAsync:(id)frame
{
int num = frame.frameNumber;// Temporary variable avoid 
problems with calling method on 'frame' [const] from block
dispatch_async(analysisSerialQueue,
^{
[analyzer processFrame:frame];
self.latestFrameNumberProcessed = num;  // I can't call 
frame.frameNumber from within the block (compiler error)
});
}



On 8 Nov 2010, at 16:29, David Duncan wrote:

> It might be useful to know what you expect to get from the const reference in 
> the first place? Primarily because I highly suspect that it is not necessary 
> to ensure the semantics that you desire.
> 
> On Nov 8, 2010, at 4:59 AM, Jonny Taylor wrote:
> 
>> I have encountered what I presume is a common newcomer's problem, but I 
>> haven't had any luck with google (maybe I'm using the wrong search terms?). 
>> Suppose I have a const id, e.g. "const id frame". If I 
>> attempt to call a getter for the object I get the following compiler error:
>> request for member 'frameNumber' in 'frame', which is of non-class type 
>> 'objc_object* const'
> 
> --
> David Duncan
> 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Calling getter on const object

2010-11-08 Thread David Duncan
It might be useful to know what you expect to get from the const reference in 
the first place? Primarily because I highly suspect that it is not necessary to 
ensure the semantics that you desire.

On Nov 8, 2010, at 4:59 AM, Jonny Taylor wrote:

> I have encountered what I presume is a common newcomer's problem, but I 
> haven't had any luck with google (maybe I'm using the wrong search terms?). 
> Suppose I have a const id, e.g. "const id frame". If I attempt 
> to call a getter for the object I get the following compiler error:
> request for member 'frameNumber' in 'frame', which is of non-class type 
> 'objc_object* const'

--
David Duncan

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Calling getter on const object

2010-11-08 Thread Jonny Taylor
I have encountered what I presume is a common newcomer's problem, but I haven't 
had any luck with google (maybe I'm using the wrong search terms?). Suppose I 
have a const id, e.g. "const id frame". If I attempt to call a 
getter for the object I get the following compiler error:
request for member 'frameNumber' in 'frame', which is of non-class type 
'objc_object* const'

Is there any way of declaring the getter (which in this case is just the 
auto-synthesized one...) as const (or otherwise express the fact that it can be 
safely called on a const object)? I assume that the auto-synthesized one *is* 
const - certainly there's no reason for it not to be that I can think of. 

The reason I am in fact encountering this is when I attempt to access 
properties from within a block, where the instance is treated as 'const' by 
default.

There are of course ways I can work around all this, I just feel that I am 
missing the 'right' way of doing this. Any comments?

Thanks
Jonny

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com