Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Make the Dim keyword optional #177

Open
erlebo opened this issue Oct 3, 2017 · 8 comments
Open

[Proposal] Make the Dim keyword optional #177

erlebo opened this issue Oct 3, 2017 · 8 comments

Comments

@erlebo
Copy link

erlebo commented Oct 3, 2017

For example, as an alternative to:

Dim a As Integer

also recognise:

a As Integer

Possibly the compiler/parser could use the As keyword to recognise that a variable is being declared? This would help to make VB.NET more concise.

@AnthonyDGreen
Copy link
Contributor

The parser certainly could do this, but how would type-inference work?

a = 1

is an assignment and we couldn't treat every assignment as a declaration. And I think the conciseness gained from omitting the 'As Type' is greater than that from omitting the Dim keyword, no?

Out of curiosity, is it the requirement for any keyword there that bothers you or is it the Dim keyword in particular? If it were some other keyword like Var, Val, or Let would you still find that too verbose?

@AnthonyDGreen
Copy link
Contributor

Also, for what's it's worth, the Dim keyword is technically optional already. The syntax for a variable declaration in the spec is

LocalDeclarationStatement
    : LocalModifier VariableDeclarators StatementTerminator
    ;

LocalModifier
    : 'Static' | 'Dim' | 'Const'
    ;

It just so happens that Dim is a modifier. It doesn't actually do anything and is never defined further in the spec. Its only purpose is to be a modifier and if we added another local modifier it could be used in lieu of Dim as Static and Const are today, not that we have any cause to do so.

@erlebo
Copy link
Author

erlebo commented Oct 3, 2017

I was anticipating that if Dim was omitted, As Type would be used to avoid the type inference problem. a = 1 would be treated as the equivalent of Dim a As Integer = 1 (via type inference), unless a had already been declared, in which case a=1 would be a simple assignment.

Static and Const keywords would still be used when declaring static variables and constants as they actually modify the basic variable declaration but, as Dim doesn't, I was hoping it could be omitted, mainly to save some keystrokes when writing code.

I have no problem with the Dim keyword itself, and still think of it as Dimension when i use it,

@AnthonyDGreen
Copy link
Contributor

Ah! So you're thinking more like what Python does and how VB works when Option Explicit is Off. If Python gets away with it I don't see why we can't.

@ericmutta
Copy link

@AnthonyDGreen just out of curiosity, do you have any telemetry stats about how many projects have Option Explicit Off? Ever since Option Infer was added, I have personally never found a need for Option Explicit being Off...and along those lines having Dim made it clear that a variable is being declared without it being much of burden in terms of keyboard typing effort. Also keeping Dim required helps with #159.

@erlebo
Copy link
Author

erlebo commented Oct 4, 2017

@ericmutta, with respect to your last sentence, just in case this proposal is being interpreted as removing the Dim keyword, it only asks to make it optional. Dim would still be able to be used for clarity when desired. More than just typing, I think removing the need for Dim makes the code look 'cleaner' and sometimes (albeit infrequently) having a shorter line reduces the need for scrolling or line continuations in the editor.

@ericmutta
Copy link

@erlebo More that just typing, I think removing the need for Dim makes the code look 'cleaner'

I realise concepts like 'cleaner' can be quite subjective when it comes to syntax, so I am curious: do you have an explicit example where removing Dim makes it cleaner in your opinion?

I ask because at the moment Dim can only ever appear at the beginning of a line and being just three letters long, making it optional seems to be a request to reduce what is already minimal typing effort.

Even worse, is that if you make Dim optional you end up typing more to add the As TypeXXX clause to make it clear this is a declaration. If you don't add the As portion and rely on type inference, now it is ambiguous whether you are doing an assignment to an already declared variable or declaring a new variable and assigning it (something that is possible only with the old school Option Explicit Off which I don't know if any one uses in production code anymore).

From a parsing perspective keeping Dim required is actually helpful, both for humans and machines (compilers, intellisense, pretty printers, etc). When you see Dim you know immediately (i.e without look-ahead) a variable is being declared on that line.

Without Dim you have an identifier the meaning of which is not clear until you lookahead to see what follows (i.e if followed by As then its a declaration; by ( then its a function call or array indexing operation; by : then its a label declaration; ...etc).

For humans, lookahead is not a big deal since we read entire words and sequences of words "in parallel" but lexers/parsers "read" character by character, and then it matters a big deal (see Wikipedia for the parsing theory).

In summary: let's keep Dim the way it is. Making it optional introduces a potential host of (potential) complexities that far outweight the 3 characters of typing that are saved.

@erlebo
Copy link
Author

erlebo commented Oct 5, 2017

@ericmutta I'm not that keen to argue further the merits or otherwise of this change, as it was mainly intended as a suggestion to improve the flexibility of the language to support those who prefer to omit Dim, for whatever reasons. If whoever would be tasked with recommending, approving and/or implementing the change considers that it's not worth the effort, I can accept that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants