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

? as Boolean type char #148

Open
xieguigang opened this issue Aug 8, 2017 · 6 comments
Open

? as Boolean type char #148

xieguigang opened this issue Aug 8, 2017 · 6 comments

Comments

@xieguigang
Copy link

The ? character was chosen using for the Nullable structure type currently. And all of the stand along ? type char currently was defined as nullable Object type, but the Object is already can be Nothing.

' local flag As Object
Dim flag?

' param flag As Object = True
Sub Test(Optional flag? = True)
End Sub

In my opinion, the ? character have some literal meaning like:

  • yes or no?
  • true or false?
  • success or failure?
  • right or wrong?
  • exists or not?

So proposal, when user define a variable with a stand along ? character, then it should be defined as a Boolean variable:

' boolean, exists or not?
Dim exists? = list.IndexOf("123") > -1
' nullable integer
Dim n As Integer?

Sub Test(path$, Optional ByRef skip? = False, Optional count As Integer? = 0)
End Sub
@AdamSpeight2008
Copy link
Contributor

I like the usage of ? for Boolean type, but have reservations.
How would it work with null member access eg myobject?.mymember

@xieguigang
Copy link
Author

Class Foo

    Public Sub Calls()
    End Sub

End Class

Module Test

    Sub Main()

        ' Boolean
        Dim success? = Test()

        ' If Nothing syntax sugar
        Dim o As New Foo
        Call o?.Calls()

        ' Nullable
        Dim n As Integer? = 5
        Call n?.CompareTo(0)
    End Sub

End Module

@AdamSpeight2008 How would it work with the nullable member access and current If Nothing syntax sugar?

Due to the reason of we just using Boolean its value True/False to indicate the status, function execute result or turn switch on/off, member access of the Boolean type have a very very Very limited scenarios. So that, in my opinion, it wouldn't be a problem to distinguish the member access syntax between the Boolean type char and nullable type.

@AdamSpeight2008
Copy link
Contributor

I see an ambiguity on obj?.Method() is that a null-member access or a member access.
Parsing would be even more complex, likely needing type semantic information.
@xieguigang We not branch the compiler? and try it out.

@AdamSpeight2008
Copy link
Contributor

@xieguigang Initial parsing a type character done. Feature: Boolean TypeCharacter
Loads and loads of unit test failures. Ambiguity. Needs to know identifier's type whilst determining if ? is not a type character, whilst parsing identifier (and said type character.)
I am not sure if it worth the effort to pursuing figuring how to disambiguate.

@xieguigang
Copy link
Author

xieguigang commented Aug 14, 2017

Hi, @AdamSpeight2008 because I'm not so familiar to vbc its operation mechanism, so that I can not give too much help on the code implementation, but I think this issue is very similar to the scenario of dynamics Late-bound member access.

For example, the Late-bound member access and invocation expressions that using candidate ! symbol, which is very similar to this scenario: we use ! symbol for the single type, but it probably have some conflicts between type char and the dynamics calls.

' we not sure this is a method call from a single type
' or dynamics call from an object
Call obj!.method()

Which is very similar to the scenario in current issue:

' we not sure this is a method call from a boolean type
' or method calls from a nullable object
Call obj?.method() 

There are two possible solution for this disambiguate job, as @AnthonyDGreen has pointed out:

there are a couple of solutions to that. 1) decide it's a targeted enough break to accept. 2) quirks it. Basically, on Object expression!.Name will do a late-bound lookup, and on Single expression!.Name will do an early bound lookup for back-compat.

The solution for detects that the statement is a member access from boolean or null-member access, probably could be determined based on the code context: what data type of the parsed identifier it is, this can be solved from the code association between the member access statement and the variable definition.

@AnthonyDGreen
Copy link
Contributor

The problem here is that Dim v? = e already means something in VB. It means infer the type of v to be the nullable form of the type of e. So for the simplest case of assigning a boolean value to a new variable the variable will actually be a nullable bool. That applies most everywhere else one can declare a variable in VB.

I'm not much of a fan of type characters to begin with given that they only apply to the built-in types.

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