I
Ioannis Vranos
Proposal:
We can increase type safety in C by adding a single keyword. The current proposal uses the keyword "only".
Example 1:
only int x= 4;
x= 5; // OK
x= 5.0; // Error
x= 5U // Error
Example 2:
int i= 5;
only unsigned x= 4U;
x= 5; // Error
x= 5LU; // Error
x= i; // Error
Example 3:
only float f= 4.0F;
f= 4; // Error
f= 5.0; // Error
f= 4.0F; // OK
// It accepts any built in type
void somefunc(const int x);
// It accepts only an int and a const int object
void somefunc(only const int x);
More cases:
only double d1 = 1F; // That would be an error. Type mismatch (float assigned to double).
only double d2 = 1; // That would be an error. Type mismatch (int assigned to double).
d1 = 1F; // That would be an error. Type mismatch (float assigned to double).
d2 = 1; // That would be an error. Type mismatch (int assigned to double).
short s = 1; // That would be correct, since the keyword "only" is missing.
In the case of
only short s= 1;
it could be an error (type mismatch), if a postfix for short constants was provided, for example H/h.
short s= 1H; could work. And (UH/uh for unsigned short constants).
only int i = s; // That would be an error. Type mismatch (short assigned to int).
i = s; // That would be an error. Type mismatch (short assigned to int).
Another use of "only":
#include <stdio.h>
void somefunc1(only int x)
{
printf("somefunc(int) was called\n");
}
void somefunc2(only double x)
{
printf("somefunc(double) was called\n");
}
int main(void)
{
/* OK, int is passed */
somefunc1(4);
/* Error */
somefunc1(4U);
/* OK, double is passed */
somefunc2(4.0);
/* Error */
somefunc2(4.0F);
return 0;
}
It is simple like that, and the concept is backwards compatible.
We can increase type safety in C by adding a single keyword. The current proposal uses the keyword "only".
Example 1:
only int x= 4;
x= 5; // OK
x= 5.0; // Error
x= 5U // Error
Example 2:
int i= 5;
only unsigned x= 4U;
x= 5; // Error
x= 5LU; // Error
x= i; // Error
Example 3:
only float f= 4.0F;
f= 4; // Error
f= 5.0; // Error
f= 4.0F; // OK
// It accepts any built in type
void somefunc(const int x);
// It accepts only an int and a const int object
void somefunc(only const int x);
More cases:
only double d1 = 1F; // That would be an error. Type mismatch (float assigned to double).
only double d2 = 1; // That would be an error. Type mismatch (int assigned to double).
d1 = 1F; // That would be an error. Type mismatch (float assigned to double).
d2 = 1; // That would be an error. Type mismatch (int assigned to double).
short s = 1; // That would be correct, since the keyword "only" is missing.
In the case of
only short s= 1;
it could be an error (type mismatch), if a postfix for short constants was provided, for example H/h.
short s= 1H; could work. And (UH/uh for unsigned short constants).
only int i = s; // That would be an error. Type mismatch (short assigned to int).
i = s; // That would be an error. Type mismatch (short assigned to int).
Another use of "only":
#include <stdio.h>
void somefunc1(only int x)
{
printf("somefunc(int) was called\n");
}
void somefunc2(only double x)
{
printf("somefunc(double) was called\n");
}
int main(void)
{
/* OK, int is passed */
somefunc1(4);
/* Error */
somefunc1(4U);
/* OK, double is passed */
somefunc2(4.0);
/* Error */
somefunc2(4.0F);
return 0;
}
It is simple like that, and the concept is backwards compatible.