objc2
objc2 copied to clipboard
Handling objects passed as "out parameters"
See Passing to an out parameter by writeback.
Some methods take pointers to an instance, and, if the pointer is not NULL, write some value to the pointer. Examples include:
-
-[NSObject validateValue:forKey:error:]
-
-[NSAppleScript initWithContentsOfURL:error:]
-
-[NSCalendar rangeOfUnit:startDate:interval:forDate:]
-
-[NSNetService getInputStream:outputStream:]
- Any
error: (NSError**) err
method
Note that out parameters work by autoreleasing the value - there may be a design where we embrace this, allowing us to cut down on some unnecessary retains and releases:
// Id<T, O> -> Id<T, O>
{
let mut foo: Option<Id<T, O>>;
// let old_foo = foo;
msg_send![abc validateValue: &mut foo];
// [old_foo release]
// [foo retain]
drop(foo)
}
// &T -> &T (easily possible today, but not very clear that the reference is bound to the pool)
pool {
let mut foo: Option<&T>;
msg_send![abc validateValue: &mut foo];
// `foo` still usable inside the pool
}
// Id<T, O> -> &T
pool {
let mut foo: Option<Id<T, O>>;
// let old_foo = foo;
msg_send![abc validateValue: &mut foo];
// [old_foo release]
let new_foo: &T = foo;
}
// &T -> Id<T, O>
pool {
let mut foo: Option<&T>;
msg_send![abc validateValue: &mut foo];
// [foo retain]
let new_foo: Id<T> = foo;
}
One implementation is the current Id::writeback
(name up for bikeshedding).
But I'm wondering whether we could just allow passing &mut Option<Id<T, O>>
/Option<&mut Option<Id<T, O>>
in msg_send!
, and let it automatically handle the required retain/release?
Also: Can we let it be automatically handled in declare_class!
?