[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

[coco8] Creating out arguments to native functions

Here it is a sample expression to call a native function with an out argument.

The argument here is used to get error information, and will be filled with a handle to aNSError instance set by the implementor of the function.

First you can browse to the documentation and implementation of the wrapper to NSString class>>#stringWithContentsOfURL:encoding:error: to see that the last argument can be nil, or a pointer to an NSError instance.

In case of the function detects an error situation when called, the outArgument will be used to set the Error instance with information about the failure.

The message implemented by the method, can be used to get the contents of a web page, e.g.:
| url pError result |
	url := NSURL withString: 'http://u8.smalltalking.net'.
	pError := JSCocoa outArgument.
	result := NSString stringWithContentsOfURL: url encoding: 4 error: pError.
	result isNil
		ifTrue: [ 'Error: ' ,(pError basicAt: #localizedDescription ) ]
		ifFalse: [ result ]

In this code, the contents of the file at URL is returned, or a string with a description about the failure.

Uploaded Image: idea.gifYou can try the code in workspace of coco8 App, pointing to a wrong URL... and see the result on failure.