v icon indicating copy to clipboard operation
v copied to clipboard

nil/None/null check

Open yingshaoxo opened this issue 1 year ago • 3 comments

Describe the feature

I did a ask in golang repo, but the admin is very rude about this idea, like they never use vs-code for coding.

So I was thinking, if vlang could do the same thing, then I don't have to use golang anymore.


Why there doesn't have a nil check in vscode for golang?

For example, if I define a *string variable inside of a class (struct)

Whenever I use it, it will always ask coder to check and handle two different situations, one is it == nil, another is it == a string.

So that in this case, we'll never have to make mistakes with empty values. (Like getting an nil dereference error

You can have a look at flutter in vscode to see how they handle None.


Let me make it clear, for the following case, if we use it directly, there has high possibility it will throw errors, because text can be nil

func Test_nil(text *string) {
	println(len(*text))
}

But if the analyzer did a nil check, it will force you to write code like the following, so it will always run smoothly without error:

func Test_perfect_nil_handling(text *string) {
	if text == nil {
		// do nothing
	}
	if text != nil {
		println(len(*text))
	}
}

The VScode Golang Team said that they can't do it without golang language support: https://github.com/golang/vscode-go/issues/2689#issuecomment-1462792428

The golang team simply ignore questions, which shows their irresponsiblity: https://github.com/golang/go/issues/58957

Use Case

Avoid logic errors.

For example, nil dereference error.

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • [ ] I may be able to implement this feature request
  • [ ] This feature might incur a breaking change

Version used

every version

Environment details (OS name and version, etc.)

Every platform.

yingshaoxo avatar Mar 10 '23 03:03 yingshaoxo

In V string variables are always automatically initialized with an empty string ''. So you don't have to write Test_perfect_nil_handling and can use Test_nil. In this case println(mystring.len) will output 0 if you didn't assign some value to it. The only thing that confuses me in your issue is the *. I don't know golang.

7underlines avatar Mar 10 '23 06:03 7underlines

In V string variables are always automatically initialized with an empty string ''. So you don't have to write Test_perfect_nil_handling and can use Test_nil. In this case it println(mystring.len) will output 0 if you didn't assign some value to it. The only thing that confuses me in your issue is the *. I don't know golang.

Got it, so you are saying that Vlang doesn't have the concept of memory point?

Then how do you guys handle the null/None/nil? By using a function or type called Optional?

yingshaoxo avatar Mar 10 '23 08:03 yingshaoxo

for any pointer types, we use stdlib function isnil to check whether a pointer is null or not, yet there's a better data representation called option, which allows checking it with even conciser syntax.

For your original question, I don't think it's a good idea since force nil check will increase code complexity (consider using if-else), plus it's not always neccesarily required to check it.

ChAoSUnItY avatar Mar 10 '23 08:03 ChAoSUnItY