|
|
|
Concrete differences between Gont and C
|
|
3 Concrete differences between Gont and C
3.1 Empty instruction
There is no empty instruction (written as `;' in C). skip
keyword is
introduced instead. For example:
C:
while (f(b++));
Gont:
while (f(b++))
skip;
This is to disallow common C's mistake:
while (cond);
do_sth;
(note `;' after while).
3.2 Control structures
-
if
cond stmt
-
if
cond block else
block
-
while
cond stmt
-
do
stmt while
cond
cond is expression of type bool. (therefore
int i=10; while (i--) f();
is not allowed, one needs to write
while (i-- != 0)
). stmt might be single statement or
a block. block is zero or more stmt's surrounded
with { }
. One might note that if's version with else
keyword is required to have { }
around each part. This is to
solve dangling-else problem, for example:
C:
if (b1)
if (b2)
f1();
else
f2();
This if of course parsed as:
if (b1) {
if (b2) {
f1();
} else {
f2();
}
}
In C else
is associated with nearest else-free if
.
As you have seen this can be source of problems.
However, as there is no danger with using if
without { }
and
else
, (as in if (x == 0) x++;
), it can be used without
{ }
.
3.3 Pointers
There is no explicit notation of pointers. Structures and unions are
always treated as pointers (as in Java). Strings are built in type.
Arrays are implemented as abstract type. Pointers to functions are
discussed below.
3.4 Structures
They are defined with struct
and opt_struct
keywords.
struct s {
int a;
string b;
};
is roughly equivalent of C's:
typedef struct {
int a;
char *b;
} *s;
So, name s
can be used (only) without any struct
or
opt_struct
, as type. You should note, that structures are passed
by pointer (or by reference if you prefer C++ naming style), therefore
changes made to struct inside function are reflected in state of it on
the caller side.
You can access fields of structure with `.' operator.
Now, what opt_struct
is all about. struct
value is always
valid, i.e. it has to be initialized with object instance, and you
cannot assign null
pointer to it (BTW: null
is keyword).
opt_struct
's are not. They can be initialized with null
, as
well as assigned null
. This involves runtime check on each access
to opt_struct
value. When you try to access opt_struct
value, that is null
, Null_access
exception is raised.
If you do:
void f(s1 x)
{
s1 y;
y = x;
y.fld = 0; // here you also modify x.fld
}
In general assigning values other then int
's, bool
's and
float
's copies pointer, not content, i.e. makes an alias of object.
[[structures are not yet implemented]]
|
|
|
Concrete differences between Gont and C
|
|