V: Type declaration looks like an assignment? `mut s := []string`

Created on 28 Apr 2019  路  18Comments  路  Source: vlang/v

https://vlang.io/docs#cpp

        mut s := []string 

https://vlang.io/docs#recvs

mut user := User{} 

https://vlang.io/docs#arrays

['a', 'b'] is an array of strings ([]string). 

A type in some programming languages (such as Idris) is also a value.
This may cause confusion

Most helpful comment

It will not slow down the compiler. It's the same check that's performed when detecting unused variables.

All 18 comments

Yes, I don't like this either.

I want the creation of an empty string array to be similar to the creation of a non-empty one:

s := ['a', 'b']

That's why I don't like s := []string{}. If we need to add an element to the initialization, we have to completely change it.

Another option is s := ['' ; 0]

Or we could just do s := []string([])

Or s: Array<string> := []
fn rgb(r: int, g: int, b: int): Color { return Color{r: r, g: g, b: b} }
I think this is more readable.

The declaration syntax is not going to change. There will only be one way to declare variables.

: is not needed, just like in Go.

s := []string([]) may be the best in this style

Why not simply s := []? In all cases I can think of, the type of the array could be inferred.

That's literally what I thought a couple of hours ago :)

s := [] can be allowed. Then V looks at the first << and sets the type.

The only problem is that it's not clear for the reader. They don't know the type when they see the declaration.

On the other hand the array should be declared close to the first append. And it's always possible to specify the type with []string([]).

Good point about clarity. In real life, though, variable names are usually more than enough for a reader to be able to tell the type of a list. And if not, as you said, then yeah it would often be a good idea to explicitly write the type.

I'll just mention that the type inference won't be quite as simple as just looking for the first <<, because there are numerous other cases, for example:

n := []
for x in n {
    println(x + 1)
}

Which would imply that n is a numeric list, but << wasn't used.

I think this example shouldn't compile. n is empty, the loop will run 0 times.

Empty arrays that are not filled with << should not be allowed, just like unused variables.

Why should it not compile just because it won't do anything? But all I'm saying is << isn't the only way in which an empty array could be used. For example, n := []; println(n.contains("foo")) should print false.

For the same reason unused variables are not allowed: if it's not used, it shouldn't be in the program.

In my most recent example, it is used - just, it has no elements in it. I think it would be very unexpected to most people to find that it doesn鈥檛 compile. It could easily arise naturally, for example if you鈥檙e testing out some code by changing an array somewhere in your program manually and seeing what happens if it鈥檚 empty.

Why not simply s := []?

This is essentially Hindley鈥揗ilner type inference, we discussed this:
https://github.com/vlang/v/issues/147#issuecomment-485255549

I pointed out that HM will slow down compilation and cause less precise error messages.

Empty arrays that are not filled with << should not be allowed

An empty array is a valid value, it can't be disallowed. Or rather it could require using an optional array, but this would mean the programmer has to keep checking the array isn't empty, I expect this to be inefficient even after optimisations.

Yep, it is. And I think that if it affects compilation speed too much then the type should have to be specified. But to be honest I don't think it will make too much of a difference in the compilation speed. It's worth a try, at least, because it would make some programs a lot easier to write.

I hope looking ahead to infer the type does not increase compilation time.

It will not slow down the compiler. It's the same check that's performed when detecting unused variables.

V now has fixed width arrays: buf := [100]byte

I think having the same syntax for declarations and assignment is ok.

@medvednikov

A type can be a value.

@medvednikov

But if V is just better Go or C, this is ok.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

XVilka picture XVilka  路  3Comments

jtkirkpatrick picture jtkirkpatrick  路  3Comments

PavelVozenilek picture PavelVozenilek  路  3Comments

shouji-kazuo picture shouji-kazuo  路  3Comments

medvednikov picture medvednikov  路  3Comments