Joy Online Manual
Differences between Joy and Objective-C
While Joy syntax is a superset of Objective-C syntax (with only small exceptions), its semantics can be slightly different. Most of the differences arise because the Objective-C compiler performs type checking and insertion of casts at compile time, while the JavaScript interpreter has to postpone these until run time. But with a little care, it is possible to write code that performs the same, regardless of whether it is compiled or interpreted. This chapter summarizes the most important differences, some of which might go away in future releases of Joy. (Some of the differences are actually improvements over Objective-C, though).
Joy keywords that are legal identifiers in Objective-C |
abstract
boolean
byte
catch
class
delete
export
extends
false
final
finally
function
implements
import
in
instanceof
interface
native
new
null
package
private
protected
public
super
synchronized
this
throw
throws
transient
true
try
typeof
var
with
Objective-C statements that do not exist in Joy |
asm
goto
Inline assembly statements are ignored and produce a warning message. (All tokens between asm and the next semicolon will be skipped).
This release of Joy does not support the goto statement. (Due to lack of time, not religious dogmatism). |
Declarations are statements |
Joy treats Objective-C declarations as just another kind of statement (like C++), so you don't have to group declarations together at the beginning of a block.
Also, constructs like @interface...@end, which in Objective-C can only appear at top level, are accepted as statements by Joy. |
Scoping |
Declarations in Joy are always local to the enclosing function or method, while in Objective-C they are local to the enclosing block. |
&& and || |
The Boolean operators && and || have different semantics in Joy than in Objective-C. While Objective-C defines their result as 0 or 1, JavaScript (and Joy) defines it as either the first operand or the second operand, depending on the Boolean interpretation of the first. This has many advantages, such as being able to write: |
var x = expr1 || expr2
instead of |
var tmp = expr1
var x = tmp ? tmp : expr2
If the result of && or || is used as a Boolean value (the typical use), you need not worry about the difference at all, because the semantics are compatible. There is only a problem if your code depends on the exact numerical value of the result. In such cases it is best to write |
expr ? 1 : 0
or shorter |
!!expr
This will work the same in both Objective-C and JavaScript. |
Pointer comparisons |
Comparisons involving pointers (using any relational operator) don't work reliably. In particular, null == 0 returns false. This is a bug and will be fixed in a future Joy release. As a temporary workaround, cast all pointers to int before comparing them. This will work in both Objective-C and JavaScript. |
Automatic semicolon insertion |
As is customary in JavaScript, Joy will automatically insert a semicolon between two adjacent lines if there is no other way to parse them. For example, you can omit the semicolon between two method prototypes of a @protocol definition. |
Static initializers |
Joy does not currently understand static initialization of variables, like |
static NSRect r = {{1,2},{3,4}};
For now, please use JavaScript array or object literals, e.g. |
static NSRect r = [[1,2],[3,4]];
or |
static NSRect r = {size:{height:4,width:3},origin:{x:1,y:2}};
Printf-style functions and methods |
Joy supports functions and methods with a variable number of arguments, but only if the variable arguments are all of the same type as the last fixed argument. This implies that you can call functions and methods with printf-like format strings from Joy only when all arguments are strings. |
Preprocessor |
Joy tries to mimic many features of the C preprocessor, but there are restrictions. See Preprocessor Features for details. |
Index |