Syntax for access modifiers
Here's what I currently have in mind:
module foo
struct Foo {
private_field int // can only be accessed in the same module (default)
len- int // read-only field that can be accessed from an outside module, but not modified
public_field+ int // public field that can be accessed and modified from outside
}
module main
fn main() {
foo := new_foo()
foo.private_field // will not compile
println(foo.len) // OK
foo.len++ // will not compile
foo.public_field++ //OK
}
What do you think?
edit
Slight improvement:
struct Foo {
private_field int // can only be accessed in the same module (default)
-len int // read-only field that can be accessed from an outside module, but not modified
+public_field int // public field that can be accessed and modified from outside
}
edit 2
Immutable struct fields by default got a lot of support:
struct Foo {
foo1 int // can only be accessed in the same module, but not modified (default)
foo2 mut int // can only be accessed and modified in the same module
+foo3 int // read-only field that can be accessed from an outside module, but not modified
+foo4 mut int // public field that can be accessed and modified from an outside module
}
Perhaps formatting should be different:
struct Foo {
private_field int
len -int
public_field +int
}
most readable for me
struct Foo {
private_field int
-len int
+public_field int
}
Yes, perhaps.
Functions:
fn +foo()
fn foo+()
same with functions when there are dozens or hundreds of them with names of various lengths, it will be difficult for you to look for postfixes. Should be prefixed, Immediately visible.
most readable for me
struct Foo { private_field int -len int +public_field int }
I agree with @whoizit suggestion. more readable for me, since it should be clear that access modifier is related to field name, not to field type.
Good point, let's do it this way.
why not using keywords?
why not using keywords?
I like the approach of Go and Oberon to achieve this without keywords.
Adding more keywords increases complexity of the language, and the definitions would look a lot more verbose:
struct Foo {
private_field int
readonly len int
public public_field int
}
oh, that makes sense
It would be also handy to allow minus as word separator in names. It is more readable than underscore.
-len would mean read only
some-name would be one symbol
-3 would be negative number
x - y would be expression, spaces around the minus required
Yes, that would be nice, and I had precisely the same idea. Unfortunately it's not possible.
I don't want to make a whitespace sensitive language, and I want it to be similar to C/C++ and Go. This would be too big of a change for all these developers.
All these languages suffer of TooLongNamesDisease. Especially C is asking for it. Since it looks that V won't allow function overloading, the pressure would be here too.
Words separated by dash are bit more readable than with underscore. (I noticed this in Lisp code.) Traditional typography employs en dash (Austria-Hungary), underscore is a hack from age of typewriters.
If arithmetic operators always require spaces around, then it won't be confusing for the reader. There's low chance of accidentally joining two words and making valid symbol, which then even compiles.
Since it looks that V won't allow function overloading
I assumed that, but I would like V to allow overloading only on number of arguments. This is a lot simpler than C++ overloading, no complications about fn f(int) vs fn f(i8) etc.
Adding more keywords increases complexity of the language, and the definitions would look a lot more verbose
If we're using symbols for this, I think the same syntax for public and private should apply to functions and types. The default could be public, which for fields means read-only [outside the struct module] (to follow your design). Then -identifier makes a symbol private. The special case would be +field, which means read/write public field:
fn public_func(){}
fn -private_func(){}
struct -private_type{}
struct public_type {
read_only_public_field T
-private_field T
+read_write_public_field T
}
Immutable struct fields by default got a lot of support:
struct Foo {
foo1 int // can only be accessed in the same module, but not modified (default)
foo2 mut int // can only be accessed and modified in the same module
+foo3 int // read-only field that can be accessed from an outside module, but not modified
+foo4 mut int // public field that can be accessed and modified from an outside module
}
Immutable struct fields by default got a lot of support:
Yes, where I wrote T, you can write e.g. mut int. My comment here is about access only, mutability is a separate concern. In your reply it seems there's no way to have a field be mutable in the current module, but read-only outside it. Read-only public fields seems a good default for fields that are mutable in private. Also, what about functions and type access as I mentioned?
In your reply it seems there's no way to have a field be mutable in the current module, but read-only outside it.
Good point, this is the 5th option, and it's not handled.
So perhaps Oberon's readonly - can be brought back for this:
struct Foo {
foo1 int // can only be accessed in the same module, but not modified (default)
foo2 mut int // can only be accessed and modified in the same module
+foo3 int // read-only field that can be accessed from an outside module, but not modified
+foo4 mut int // public field that can be accessed and modified from an outside module
-foo4 int // read-only field that can be accessed from an outside module and modified from an inside module
}
This is getting a bit complicated...
The default could be public
The default is private in all major languages, it's not going to change. The default must always be private.
I assumed that, but I would like V to allow overloading only on number of arguments.
There will never be function overloading:
https://vlang.io/docs#fns
There will never be function overloading
It is straying off topic, but:
There are valid reasons why function overloading got a really bad name:
- C implicit number/pointer conversion rules.
- C++ made the situation much worse by cast operators, two implicit conversions rule, and above all, by the namespace mess with Koenig lookup.
- Primitive C tools like grep and ctags cannot handle it.
If these mistakes are avoided, function overloading wouldn't be seen as a monster.
There are natural uses of overloading - math functions like sin/cos/etc. Even C realized it and attempted to add it into the language with C11. Unfortunately, they had chosen _Generic.
Function name overloading has one not so obvious advantage - it reduces the pressure to use unreadable LongNamesWhichAreFiresureUnique.
One also could take inspiration in Smalltalk, and use function parameter names as part of name resolution.
fn coordinates( x, y) // handles cartesian coordinates
fn coordinates(angle, len) // handles polar coordinates
result = coordinates(x: 10, y:20); // it is absolutely clear which overload is wanted here
The default must always be private.
OK.
-foo4 int // read-only field that can be accessed from an outside module and modified from an inside module
Read-only field syntax without mutable data should be an error, it's correct if mut int is the field type. I don't think your example is that complicated, when thinking of mutability and private/public as two separate concepts.
But should functions and type names be public or private by default? If private then +identifier would be a consistent way to make them public. If public, I don't think we can have -private_function_name as well as -read_only_public_field, it would be confusing.
The plus and minus look like operators.
I find this extremely confusing and noisy - the meaning of plus and minus aren't naturally understood as anything other than operators, so this is a bit too creative for my taste.
Fewer characters do not necessarily make something easier to parse for a person.
We're used to words like "public", "private", "internal" etc. from english language - and if not from there, then from other programming languages.
It seems like you're struggling to make everything as few characters as possible?
I think that's misguided.
In my opinion, readability of code is much more important than how many characters you have to type - simply having having characters does not automatically make a syntax more readable. Think regular expressions. Most people needs weeks to months to learn the basics. Whereas most people learn to read languages like Java (while it does tend to be overly verbose) almost immediately - even if they don't learn the meaning of the keywords, anyone can immediately identify the keywords, which gives them at least a way to automatically associate terminology with keywords, so they can ask questions, do a search or talk to more senior coworkers about them.
Compounding everything down to symbols (worse, ambiguous symbols) increases the density of the code, which isn't automatically a good thing. Keywords aren't just noise - when used properly, they reduce the density and add to the semantics of the language.
Be careful not to compress the syntax to the point of obfuscating.