1
General Discussion / Re: Light-weight classes
« on: May 03, 2016, 06:11:08 PM »
I've thought about this issue.
Implementing "lightweight classes" by just sugaring syntax seems doable. Only obstacle seems to be struct field shadowing, as demonstrated in the following;
In my opinion, this is trivial and it could be easily detected at compile-time.
About inheritance though, it implies resolving method calls at runtime which I think is unacceptable.
My final conclusion is, we can proceed with lightweight class syntax I described in the previous post. But for the time being, let's not implement inheritance.
Implementing "lightweight classes" by just sugaring syntax seems doable. Only obstacle seems to be struct field shadowing, as demonstrated in the following;
Code: [Select]
type MyStruct struct {
int foo;
}
func void foo(MyStruct* mstr {
printf("I have calleth");
}
MyStruct mstr = { 10 };
mstr.foo() // foo resolves to both the method and the field.
In my opinion, this is trivial and it could be easily detected at compile-time.
About inheritance though, it implies resolving method calls at runtime which I think is unacceptable.
Code: [Select]
type IMyInterface struct {
}
func void foo(*IMyInterface myint) {
// show message and terminate program
}
type MyStruct_1 struct {
int bar;
}
func void foo(MyStruct_1 *mystr1 {
printf("Number ONE reporting");
}
type MyStruct_2 struct {
int barrer;
}
func void foo(MyStruct_2 *mystr2) {
printf("Number TWO reporting");
}
void test_1() {
IMyInterface *myint;
MyStruct_1 mystr = { 10 };
myint = (*MyInterface) &mystr;
myint.foo(); // This call can be resolved at compile-
}
void test_2(int flag) {
IMyInterface *myint;
if (flag) {
MyStruct_1 mystr = { 10 };
myint = (*IMyInterface) &mystr;
} else {
MyStruct_2 mystr = { 10 };
myint = (*IMyInterface) &mystr;
}
myint.foo(); // This call CANNOT be resolved at compile time. Implicit code must run on runtime.
}
My final conclusion is, we can proceed with lightweight class syntax I described in the previous post. But for the time being, let's not implement inheritance.