How to handle pointers. #1760
YagaoDirac
started this conversation in
Language design
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I just saw a poll asking if we are gonna have pointers in Carbon. I was surprised. If pointer is not allowed in Carbon, then something has to be invented to replace pointer and works almost the same with pointer, the same unsafe.
But, can we do something to handle this? Or, if there is a easy solution which handles both c++like case and new programmer friendly case, then you should consider it.
If you have read my earlier thread, this grammar may look familiar to you.
example:
{
scope.no_pointer_arith();
var i:[i32,3] = (42, 43, 44);
var p:int*;// OK, init to nullptr.
p = i;
p = i[1];// OK
p = p+1;// Error, pointer arithmetic is not allowed in this scope.
p++;// Error, same reason.
}
{
scope.no_pointer_arith();
scope.only_const_pointer();
var i:[i32,3] = (42, 43, 44);
//var p:int*;// Error, p is immutable but init to nullptr which doesn't make sense.
var p:int* = i; // OK
p = i; // Error, p is immutable.
p = i[1];// Error, p is immutable.
p+1;// Error, pointer arithmetic is not allowed in this scope.
p++;// Error, same reason.
}
In the second scope, pointers are only allow to init to a non-null value, and from that on, it's the same as a &(reference) in c++. This is very safe and new programmer friendly. Maybe you can also add this in Carbon. Also, this grammar may handle all the c#, python, js jobs properly. Or simply this feature contributes to some specific parts in codes where pointers are used as ref.
This grammar utilize auto completion, breakpoint. It doesn't introduce any new grammar. It's very regular.
Beta Was this translation helpful? Give feedback.
All reactions