Skip navigation.
 
mlRe: Cocoa et al as HCI usability problem
FROM : Peter Duniho
DATE : Mon May 19 23:49:01 2008

On May 19, 2008, at 2:05 PM, Greg Titus wrote:

> On May 19, 2008, at 12:08 PM, Peter Duniho wrote:
> [...]

>> However, _with_ reflection we can do much of the same kinds of 
>> things that Obj-C does, without knowing in advance the classes 
>> that might use the NSUndoManager class.
>>
>> One advantage I see in Cocoa is that, because classes may respond 
>> to selectors that they didn't even declare, NSUndoManager can 
>> simply set a temporary variable, and then catch a selector to be 
>> saved away for later invocation.  This makes the reflection aspect 
>> ("introspection" in Objective-C) more transparent.

>
> Right. I'm glad you see that. Another place to look for the same 
> type of thing is Distributed Objects in Obj-C. It's another piece 
> of the frameworks that use the ability to catch invocations "in 
> flight". In that case, in order to serialize the method and 
> arguments, send the data across the network or between threads, 
> unserialize on the other side, and invoke in that other machine/
> process/thread.


Well, except that this would generally be implemented by the 
framework, no?

In .NET, it supports remote invocations, and this uses (in part) 
reflection, but the application writer never needs to know the gory 
details.  I agree that reflection is slightly more awkward than 
what's been presented in terms of Objective-C's message paradigm, and 
I accept that as a benefit for the authors of the framework.  But the 
end-user doesn't need any of this, or even need to care how it's 
implemented.  Imposing a particular language on the end-user in order 
to support the framework author doesn't seem compelling to me.

> Thanks very much for the example. The same sort of facility 
> (getting method from name) is available in Objective-C, of course, 
> and Andreas Mayer replied on the list an interesting example of how 
> he uses that in AppleScript handling. What C# seems to be missing 
> is the reverse facility (going from a compiled method call back out 
> to name + arguments), which my sample NSUndoManager code demonstrated.


Well, I didn't show the invocation code because it's trivial.  You 
just use the MethodInfo object along with the target instance and the 
saved parameters to call the MethodInfo.Invoke() method.

> Interestingly, given your earlier remarks about the desirability of 
> compile time checking, in Objective-C [[undoManager 
> prepareWithInvocationTarget:self] setColor:mColor] is type checked. 
> The compiler knows about the -setColor: method declaration it has 
> seen and can check that mColor is an appropriate type.


With reflection, you can have type checking, though it's more 
explicit.  With the anonymous method (as I said, more idiomatic in C# 
anyway), type checking is implicit.

> (Because Obj-C still lets you call any method on any object the 
> result of bad typing here will be a warning rather than an error, 
> but Obj-C programmers generally learn to pay attention to and fix 
> all warnings.) Whereas I suspect that when you are using the 
> reflection facilities in C# in the way you are above, that there is 
> no type checking being performed. Is that correct?


There's no compile-time type checking with reflection, that's correct 
(though you can certainly include run-time type checking if you want 
to ensure your program doesn't blow up :) ).  That's certainly one 
reason to avoid reflection if it's not necessary (and it wouldn't be 
in this particular example...I offered the reflection example as a 
way of illustrating the more general point).

The fact is, often there are better alternatives to reflection in 
C#.  I simply presented reflection as a "closest match" to the 
specific Objective-C functionality being shown.

> That is one of the advantages of having the dynamism built into the 
> language runtime rather than a reflection API built on top. Another 
> advantage is that code can be written that doesn't need to know 
> whether reflection is being used or not. In the Distributed Objects 
> case, for instance, it is very common to pass around proxies as 
> arguments to code that doesn't have any idea that the methods it is 
> calling on those argument objects actually get forwarded somewhere 
> else entirely.


I haven't had time to look at the Distributed Objects example. 
However, I suspect that there are equivalent idioms in C#/.NET that 
don't require the end-user (that is, the application writer) to know 
about reflection.  The reflection aspect would be "under the hood".

>> In reality, I would (and have) more likely implement an undo 
>> manager that uses anonymous methods.  Then all you're saving to 
>> your undo state is a delegate that does what you want (assumes the 
>> "property" semantic I posted):
>>
>>    undoManager.AddUndo(delegate { color = mColor; });
>>
>> This is more idiomatic in C# and wouldn't need all that messy 
>> reflection stuff.  Executing the undo is a simple matter of 
>> invoking the delegate that was passed, a simple one-line operation 
>> that reads like a method call (note that the use of the name 
>> "delegate" is very different in C# than in Cocoa...C# "delegate" 
>> is more like a function pointer than an actual object to which 
>> some selector has been delegated).

>
> Like I said earlier, I don't know C#, but this doesn't appear to me 
> what the code would actually look like.  The trouble is that the 
> whole point is we want to be able to undo a previous -setColor: 
> call. If mColor is a reference to the "this" object's current 
> color, then at the time that the undo happens, the value of the 
> reference "mColor" will be the _new_ color, not the old color that 
> we want to restore.


Sorry, you're right.  Typo on my part.  The actual code would look 
like this:

    NSColor colorPrev = color;

    undoManager.AddUndo(delegate { color = colorPrev; });

The local variable "colorPrev" would be "captured" and stored as part 
of the anonymous method.  For a given instance of the anonymous 
method, it will have the value exactly as it was at the moment that 
the anonymous method was created (i.e. at the time that AddUndo() was 
called).

> So that line of code will just set it to itself. What is needed is 
> to store the mColor value as it is at the time the anonymous 
> delegate is created, not at the time the delegate is executed. Is 
> it possible for an anonymous method to have its own instance 
> variables (in this case, to store the old color)?


Captured variables are essentially instance variables for the 
anonymous method.

> From looking at the docs it doesn't appear to. Nor does it seem 
> possible to do so with named method delegates.
>

>> I don't understand the comments saying that you can't do something 
>> similar in Java.  Java has the same kind of reflection features 
>> that C# has.  The anonymous method approach wouldn't work, as Java 
>> doesn't have anything equivalent to C# delegates, but Java does 
>> have interfaces and using those with anonymous types in lieu of 
>> anonymous methods is a common Java idiom that works well.

>
> Only with fixed signatures, though.


Not really.  Using an anonymous type in Java in lieu of an anonymous 
method in C#, you can use local variables (declared "final") in a 
similar fashion as that used to capture variables in C#.  So you'd 
just need a single interface, and then reference the necessary local 
variable data from within an anonymous type implementing that interface.

I've yet to run into anything in Java where I could have used an 
anonymous method in C#, but couldn't get an anonymous type to work in 
a similar fashion in Java.  I find the anonymous method syntax more 
elegant, but the Java approach is workable and generally feature-
identical.

Pete

Related mailsAuthorDate
mlCocoa et al as HCI usability problem Julius Guzy May 18, 13:33
mlRe: Cocoa et al as HCI usability problem Jason Stephenson May 18, 15:36
mlRe: Cocoa et al as HCI usability problem Erik Buck May 18, 16:39
mlRe: Cocoa et al as HCI usability problem Gary L. Wade May 18, 18:03
mlRe: Cocoa et al as HCI usability problem David Wilson May 18, 18:15
mlRe: Cocoa et al as HCI usability problem Jens Alfke May 18, 18:41
mlRe: Cocoa et al as HCI usability problem Michael Vannorsdel May 18, 19:56
mlRe: Cocoa et al as HCI usability problem I. Savant May 18, 22:29
mlRe: Cocoa et al as HCI usability problem P Teeson May 18, 22:38
mlCocoa et al as HCI usability problem Johnny Lundy May 18, 23:03
mlRe: Cocoa et al as HCI usability problem Jason Stephenson May 18, 23:28
mlRe: Cocoa et al as HCI usability problem ??????? ????????? May 18, 23:39
mlRe: Cocoa et al as HCI usability problem Michael Vannorsdel May 18, 23:54
mlRe: Cocoa et al as HCI usability problem Torsten Curdt May 19, 02:04
mlRe: Cocoa et al as HCI usability problem Julius Guzy May 19, 02:41
mlRe: Cocoa et al as HCI usability problem David Wilson May 19, 02:56
mlRe: Cocoa et al as HCI usability problem Julius Guzy May 19, 03:15
mlRe: Cocoa et al as HCI usability problem Jens Alfke May 19, 03:34
mlRe: Cocoa et al as HCI usability problem Julius Guzy May 19, 03:39
mlRe: Cocoa et al as HCI usability problem Scott Anguish May 19, 03:49
mlRe: Cocoa et al as HCI usability problem Scott Anguish May 19, 03:50
mlRe: Cocoa et al as HCI usability problem Julius Guzy May 19, 03:58
mlRe: Cocoa et al as HCI usability problem Michael Vannorsdel May 19, 04:08
mlRe: Cocoa et al as HCI usability problem Jonathan Hendry May 19, 04:24
mlRe: Cocoa et al as HCI usability problem Erik Buck May 19, 04:27
mlRe: Cocoa et al as HCI usability problem ben syverson May 19, 04:39
mlRe: Cocoa et al as HCI usability problem Andreas Mayer May 19, 04:42
mlRe: Cocoa et al as HCI usability problem Stuart Malin May 19, 05:13
mlRe: Cocoa et al as HCI usability problem Michael Vannorsdel May 19, 05:29
mlRe: Cocoa et al as HCI usability problem Nathan Kinsinger May 19, 05:41
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 19, 07:03
mlRe: Cocoa et al as HCI usability problem Graham Cox May 19, 08:20
mlRe: Cocoa et al as HCI usability problem Andrew Merenbach May 19, 08:31
mlRe: Cocoa et al as HCI usability problem Graham Cox May 19, 08:34
mlRe: Cocoa et al as HCI usability problem Erik Buck May 19, 08:35
mlRe: Cocoa et al as HCI usability problem Jean-Daniel Dupas May 19, 09:51
mlRe: Cocoa et al as HCI usability problem ben syverson May 19, 10:19
mlRe: Cocoa et al as HCI usability problem Graham Cox May 19, 10:45
mlRe: Cocoa et al as HCI usability problem Jean-Daniel Dupas May 19, 11:52
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 19, 12:10
mlRe: Cocoa et al as HCI usability problem Jean-Daniel Dupas May 19, 12:26
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 19, 12:58
mlRe: Cocoa et al as HCI usability problem Julius Guzy May 19, 13:09
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 19, 13:11
mlRe: Cocoa et al as HCI usability problem I. Savant May 19, 13:14
mlRe: Cocoa et al as HCI usability problem Torsten Curdt May 19, 13:17
mlRe: Cocoa et al as HCI usability problem Michael Ash May 19, 13:50
mlRe: Cocoa et al as HCI usability problem Jason Stephenson May 19, 14:33
mlRe: Cocoa et al as HCI usability problem Jeff LaMarche May 19, 15:32
mlRe: Cocoa et al as HCI usability problem Lincoln Green May 19, 17:05
mlRe: Cocoa et al as HCI usability problem Alex Kac May 19, 18:42
mlRe: Cocoa et al as HCI usability problem Alex Kac May 19, 18:52
mlRe: Cocoa et al as HCI usability problem Jeff LaMarche May 19, 19:11
mlRe: Cocoa et al as HCI usability problem Gérard Iglesias May 19, 19:21
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 19, 19:33
mlRe: Cocoa et al as HCI usability problem Jeff LaMarche May 19, 19:40
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 19, 19:42
mlRe: Cocoa et al as HCI usability problem Greg Titus May 19, 19:48
mlRe: Cocoa et al as HCI usability problem Jonathan Hendry May 19, 19:49
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 19, 19:51
mlRe: Cocoa et al as HCI usability problem David Wilson May 19, 19:52
mlRe: Cocoa et al as HCI usability problem j o a r May 19, 19:56
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 19, 20:00
mlRe: Cocoa et al as HCI usability problem Greg Titus May 19, 20:10
mlRe: Cocoa et al as HCI usability problem Bill Bumgarner May 19, 20:16
mlRe: Cocoa et al as HCI usability problem Greg Titus May 19, 20:21
mlRe: Cocoa et al as HCI usability problem Jeff LaMarche May 19, 20:22
mlRe: Cocoa et al as HCI usability problem Alex Kac May 19, 20:22
mlRe: Cocoa et al as HCI usability problem Andreas Mayer May 19, 20:31
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 19, 21:08
mlRe: Cocoa et al as HCI usability problem Sherm Pendley May 19, 21:26
mlRe: Cocoa et al as HCI usability problem Andy Lee May 19, 21:51
mlRe: Cocoa et al as HCI usability problem Hamish Allan May 19, 22:04
mlRe: Cocoa et al as HCI usability problem Jayson Adams May 19, 22:29
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 19, 22:36
mlRe: Cocoa et al as HCI usability problem Andy Lee May 19, 22:42
mlRe: Cocoa et al as HCI usability problem Jean-Daniel Dupas May 19, 22:58
mlRe: Cocoa et al as HCI usability problem Greg Titus May 19, 23:05
mlRe: Cocoa et al as HCI usability problem ben syverson May 19, 23:12
mlRe: Cocoa et al as HCI usability problem Jean-Daniel Dupas May 19, 23:20
mlRe: Cocoa et al as HCI usability problem Andy Lee May 19, 23:20
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 19, 23:49
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 19, 23:55
ml[moderator] Re: Cocoa et al as HCI usability problem Scott Anguish May 20, 00:05
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 20, 00:06
mlRe: Cocoa et al as HCI usability problem Philippe Mougin May 20, 01:00
mlRe: Cocoa et al as HCI usability problem Andy Lee May 20, 01:18
mlRe: Cocoa et al as HCI usability problem Alex Kac May 20, 01:33
mlRe: Cocoa et al as HCI usability problem Alex Kac May 20, 01:49
mlRe: Cocoa et al as HCI usability problem Chris Hanson May 20, 02:16
mlRe: Cocoa et al as HCI usability problem Torsten Curdt May 20, 02:17
mlRe: Cocoa et al as HCI usability problem Alex Kac May 20, 04:00
mlRe: Cocoa et al as HCI usability problem Andreas Mayer May 20, 04:26
mlRe: Cocoa et al as HCI usability problem Rua Haszard Morris May 20, 04:27
mlRe: Cocoa et al as HCI usability problem Michael Ash May 20, 04:55
mlRe: Cocoa et al as HCI usability problem Graham Cox May 20, 07:39
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 20, 09:47
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 20, 10:07
mlRe: Cocoa et al as HCI usability problem Gérard Iglesias May 20, 10:34
mlCategories (was Re: Cocoa et al as HCI usability problem) Bill Bumgarner May 20, 10:46
mlRe: Cocoa et al as HCI usability problem Andreas Mayer May 20, 10:52
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 20, 11:19
mlRe: Cocoa et al as HCI usability problem I. Savant May 20, 13:04
mlRe: Cocoa et al as HCI usability problem Mark Roseman May 20, 18:41
mlRe: Cocoa et al as HCI usability problem Erik Buck May 20, 19:54
mlCocoa et al as HCI usability problem Klaus Backert May 21, 00:18
mlRe: Cocoa et al as HCI usability problem Graham Cox May 21, 01:52
mlRe: Cocoa et al as HCI usability problem Michael Ash May 21, 02:33
mlRe: Cocoa et al as HCI usability problem Steve Weller May 21, 04:50
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 21, 06:52
mlRe: Cocoa et al as HCI usability problem Scott Anguish May 21, 08:51
mlRe: Cocoa et al as HCI usability problem j o a r May 21, 09:01
mlRe: Cocoa et al as HCI usability problem Scott Anguish May 21, 09:06
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 21, 09:49
mlRe: Cocoa et al as HCI usability problem Michael Ash May 21, 10:16
mlRe: Cocoa et al as HCI usability problem Torsten Curdt May 21, 10:31
mlRe: Cocoa et al as HCI usability problem Hamish Allan May 21, 10:42
mlRe: Cocoa et al as HCI usability problem j o a r May 21, 10:53
mlRe: Cocoa et al as HCI usability problem Jeff LaMarche May 21, 14:02
mlRe: Cocoa et al as HCI usability problem Jeff LaMarche May 21, 14:14
mlRe: Cocoa et al as HCI usability problem Torsten Curdt May 21, 14:32
mlRe: Cocoa et al as HCI usability problem Jean-Daniel Dupas May 21, 14:59
mlRe: Cocoa et al as HCI usability problem João Pavão May 21, 15:08
mlRe: Cocoa et al as HCI usability problem Joseph Ayers May 21, 15:12
mlRe: Cocoa et al as HCI usability problem Steve Weller May 21, 15:45
mlRe: Cocoa et al as HCI usability problem Jeff LaMarche May 21, 16:00
mlRe: Cocoa et al as HCI usability problem Gérard Iglesias May 21, 16:10
mlRe: Cocoa et al as HCI usability problem Andreas Mayer May 21, 18:01
ml[Moderator] PLEASE READ - Re: Cocoa et al as HCI usability problem Scott Anguish May 21, 19:08
mlRe: Cocoa et al as HCI usability problem Jonathan Hendry May 21, 19:28
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 21, 19:30
mlRe: Cocoa et al as HCI usability problem Jeff LaMarche May 21, 21:14
mlRe: Cocoa et al as HCI usability problem Sherm Pendley May 21, 21:27
mlRe: Cocoa et al as HCI usability problem Scott Ellsworth May 21, 22:18
mlRe: Cocoa et al as HCI usability problem Rua Haszard Morris May 22, 00:58
mlRe: Cocoa et al as HCI usability problem Mike Abdullah May 22, 01:05
mlRe: Cocoa et al as HCI usability problem Kyle Sluder May 22, 01:22
mlRe: Cocoa et al as HCI usability problem William Turner May 22, 01:33
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 22, 01:37
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 22, 01:37
mlRe: Cocoa et al as HCI usability problem Rua Haszard Morris May 22, 01:46
mlRe: Cocoa et al as HCI usability problem Jeff LaMarche May 22, 02:01
mlRe: Cocoa et al as HCI usability problem Peter Duniho May 22, 02:01
mlRe: Cocoa et al as HCI usability problem Scott Anguish May 22, 02:49
mlRe: Cocoa et al as HCI usability problem Andy Lee May 22, 03:58
mlRe: Cocoa et al as HCI usability problem Graham Cox May 22, 04:13
mlRe: Cocoa et al as HCI usability problem Robert Cerny May 22, 12:23
mlRe: Cocoa et al as HCI usability problem Hamish Allan May 22, 12:58
mlRe: Cocoa et al as HCI usability problem Jeff LaMarche May 22, 13:46
mlRe: Cocoa et al as HCI usability problem Sherm Pendley May 22, 17:46
mlRe: Cocoa et al as HCI usability problem Andy Lee May 22, 18:02
mlRe: Cocoa et al as HCI usability problem Sherm Pendley May 22, 18:17
mlRe: Cocoa et al as HCI usability problem Jeff LaMarche May 22, 18:39
mlRe: Cocoa et al as HCI usability problem Andy Lee May 22, 19:20
mlRe: Cocoa et al as HCI usability problem Hamish Allan May 22, 19:23
mlRe: Cocoa et al as HCI usability problem Andy Lee May 22, 19:34
mlRe: Cocoa et al as HCI usability problem Andy Lee May 22, 20:40
mlRe: Cocoa et al as HCI usability problem Thomas Engelmeier May 22, 20:52
mlRe: Cocoa et al as HCI usability problem Hamish Allan May 22, 22:11
mlRe: Cocoa et al as HCI usability problem Scott Anguish May 23, 00:16
mlRe: Cocoa et al as HCI usability problem Graham Cox May 23, 03:36
mlRe: Cocoa et al as HCI usability problem Graham Cox May 23, 03:45
mlProperties & GC (was Re: Cocoa et al as HCI usability problem) Bill Bumgarner May 23, 05:21
mlRe: Properties & GC (was Re: Cocoa et al as HCI usability problem) Sherm Pendley May 23, 10:30
mlRe: Properties & GC (was Re: Cocoa et al as HCI usability problem) Michael Ash May 23, 10:45
mlRe: Properties & GC (was Re: Cocoa et al as HCI usability problem) Sherm Pendley May 23, 11:11
mlRe: Cocoa et al as HCI usability problem Ilan Volow May 23, 15:27
mlRe: Cocoa et al as HCI usability problem Gérard Iglesias May 23, 15:34
mlRe: Cocoa et al as HCI usability problem Andy Lee May 23, 22:33
mlRe: Properties & GC (was Re: Cocoa et al as HCI usability problem) Andy Lee May 23, 22:34
mlRe: Cocoa et al as HCI usability problem Sherm Pendley May 23, 22:49
mlRe: Cocoa et al as HCI usability problem glenn andreas May 23, 23:21
mlRe: Cocoa et al as HCI usability problem Bill Bumgarner May 23, 23:34
mlRe: Cocoa et al as HCI usability problem Sherm Pendley May 23, 23:45
mlRe: Cocoa et al as HCI usability problem has May 23, 23:58
mlRe: Cocoa et al as HCI usability problem Sherm Pendley May 24, 00:03
mlRe: Cocoa et al as HCI usability problem Sherm Pendley May 24, 00:50
mlRe: Cocoa et al as HCI usability problem Michael Ash May 24, 03:41
mlRe: Cocoa et al as HCI usability problem Graham Cox May 24, 06:19
mlRe: Cocoa et al as HCI usability problem Adam R. Maxwell May 24, 06:39
mlRe: Cocoa et al as HCI usability problem Bill Bumgarner May 24, 07:08
mlRe: Cocoa et al as HCI usability problem Bill Bumgarner May 24, 07:11
mlRe: Properties & GC (was Re: Cocoa et al as HCI usability problem) Michael Ash May 24, 12:51
mlRe: Categories (was Re: Cocoa et al as HCI usability problem) Georg Tuparev Jun 2, 22:31