Joy Online Manual

Additional Operators

Table of Contents

Introduction
Pointer Operators
Type-Cast Operator
Sizeof Operator


Introduction

JavaScript's operators are "borrowed" from C. Luckily, Joy need add only a few operators to implement the complete Objective-C expression syntax.  Unfortunately, the JavaScript void operator does not exist in Joy, instead you have to use a cast, such as (void) expr.



Pointer Operators

NAME

& - Address-Of Operator

SYNOPSIS

&name
&object.property
&object[index]
...

DESCRIPTION
Joy allows you to take the address of any variable or property (provided its type is known).

If successful, this operation will result in a Pointer object. For the & operator to work on variables or arguments, they have to be declared!


NAME
* - Pointer-Dereference Operator

SYNOPSIS

*object

DESCRIPTION
You can use *object as a shortcut for object[0], like in C. Of course, this operator is most useful for dereferencing pointers, though it will work on any object.


NAME
-> - Arrow Operator

SYNOPSIS

object->property

DESCRIPTION
You can use object->property as a shortcut for object[0].property. This operator is most useful for dereferencing pointers to structures or unions, but you can use it on other objects, too.


Type-Cast Operator



NAME

(type) - Type cast operator

SYNOPSIS

(type) expression

DESCRIPTION
Joy understands C-style type casts. You can even cast to array, struct, and union types (which C would not allow).

This feature can be useful to work around cases where Joy can't know the symbolic field names of a structure: E.g., the runtime type signature of the Objective-C method -[NSView frame] doesn't specify the fact that the return value is a NSRect, just that it is a structure containing two other structures with two float fields each. So, the expression [view frame].origin will produce an error if evaluated by Joy. To get around the problem, write ((NSRect)[view frame]).origin.

Type casts are potentially dangerous, especially when pointers are involved, so watch out! When casting to id or Class, Joy will perform the cast only if the argument is "known" to be a valid Objective-C id. You can bypass this safety check (if you are really sure of what you are doing!) by writing Id(address).

EXAMPLES

(unsigned) -1
(int) 3.14
(int *) null
(void) f()
(NSRect) [[1,2],[3,4]]


Sizeof Operator



NAME

sizeof - Sizeof operator

SYNOPSIS

sizeof (type)
sizeof name
sizeof object.property
sizeof object[index]

DESCRIPTION
The result of this operation is the argument's aligned size in bytes. For the sizeof operator to work on variables or arguments, they have to be declared!

EXAMPLE

js> typedef struct example {
id anObject;
char *aString;
int anInt;
} Example
js> sizeof(Example)
12


Index