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

Is ... OrIs #519

Open
VBAndCs opened this issue Mar 30, 2020 · 26 comments
Open

Is ... OrIs #519

VBAndCs opened this issue Mar 30, 2020 · 26 comments

Comments

@VBAndCs
Copy link

VBAndCs commented Mar 30, 2020

I propose to add a new OrIs Keyword. It will help to rewrite this:

If TypeOf item Is XmlSchemaElement OrElse TypeOf item Is XmlSchemaComplexType Then

as:

If TypeOf item Is XmlSchemaElement OrIs  XmlSchemaComplexType Then

This will imply adding AndIsNot keyword to work with IsNot.

@tverweij
Copy link

tverweij commented Mar 30, 2020

My syntax would be:

If TypeOf item In {XmlSchemaElement, XmlSchemaComplexType} Then

@Happypig375
Copy link
Member

Pattern matching is a better solution than this. #337

@zspitz
Copy link

zspitz commented Mar 30, 2020

I would prefer composable types:

If item Is XmlSchemaElement Or XmlSchemaComplexType Then

because it's something that can be used generally, and not just in the context of type-checking.

@VBAndCs
Copy link
Author

VBAndCs commented Mar 30, 2020

@tverweij
Yours contains {} which annoys some VBers (me included) as more C# symbolic style, and it will be a bit difficult to parse where other {} in the scope are involved.
So, this makes mine more Vbish, more readable and easier to parse.

@zspitz
Copy link

zspitz commented Mar 30, 2020

@Happypig375 There's still a problem, even with pattern matching: how can you refer to the item in question:

Select item
    Case XmlSchemaElement, XmlSchemaComplexType
        ' What should the programmer do here?
        ' Cast item to XmlSchemaElement? to XmlSchemaComplexType? It might be the other one.
End Select

Without composable types, VB has no way of describing something that "might be either XmlSchemaElement or XmlSchemaComplexType".

@VBAndCs
Copy link
Author

VBAndCs commented Mar 30, 2020

@Happypig375
I don't need casting here, at least in this stage (as in passing the object to a function).
So, no confliction.

@VBAndCs
Copy link
Author

VBAndCs commented Mar 30, 2020

If item Is XmlSchemaElement Or XmlSchemaComplexType Then

Or already has two functions (logical and bitwise), and adding a third one will complicate things if the three ORs appeared in the same context, which will need some {} and more complication and less readability.

@tverweij
Copy link

I compare this proposal to string handling:

If aString = "abc" orelse aString = "bcd" Then

I normally write this as:

If {"abc", "bcd"}.Contains(aString) Then

But it could also be written as:

Select aString
    Case "abc", "def"

Composable types require some magic Boolean operations as far as I can see.
The OrIs solution has no other comparable VB Syntax (as far as I know)

@Padanian
Copy link

If {"abc", "bcd"}.Contains(aString) Then

This is an abomination and wrong, if for some reason aString = "ab".
Subsets and equality don't match

@tverweij
Copy link

tverweij commented Mar 30, 2020

Explain I am willing to learn.
(Ps: I learned this trick from Lucian)

@AdamSpeight2008
Copy link
Contributor

AdamSpeight2008 commented Mar 30, 2020

There is a prototype of #481 TypeOf expr Is (Of T0 ..., Tn), in the pull requests.

@zspitz
Copy link

zspitz commented Mar 30, 2020

@VBAndCs

Or already has two functions (logical and bitwise)

Using Or for composing types would have a similar meaning to its current usage as a bitwise operator.

@AdamSpeight2008
Copy link
Contributor

AdamSpeight2008 commented Mar 30, 2020

@zspitz I also implemented a version that used operators like TypeOf expr Is T0 Or T1, but it involved change a lot more code that then TypeOf expr Is (Of T0 ..., Tn)

Note: T0 ... , Tn are just representing placeholders for a type, don't think that is just restricted to generic types. Itsn't.

@tverweij
Copy link

but it involved change a lot more code that then TypeOf expr Is (Of T0 ..., Tn)

I like this solution.

@VBAndCs
Copy link
Author

VBAndCs commented Mar 30, 2020

Is (Of) is OK. It doesn't need new keywords, but have more symbols than using OrIs, which will be more readable when splitting long lines.
So, I like both evevn.

@AdamSpeight2008
Copy link
Contributor

@VBAndCs It also preserve the meaning of old albeit broken code.

@AdamSpeight2008
Copy link
Contributor

@VBAndCs
Your original post example would be:-
If TypeOf item Is (Of XmlSchemaElement, XmlSchemaComplexType) Then

@VBAndCs
Copy link
Author

VBAndCs commented Apr 1, 2020

This looks like chicking a generic type params, which would be nice if done instead of using reflection. So I advic to modify your proposal to do that.

@WolvenRA
Copy link

WolvenRA commented May 2, 2020

@VBAndCs
Your original post example would be:-
If TypeOf item Is (Of XmlSchemaElement, XmlSchemaComplexType) Then

I don't like the syntax of this one. Trending toward C# type syntax. Much like tverweij's version.

VBAndCs original suggestion or zspitsz simplified version "read" much better.

@AdamSpeight2008
Copy link
Contributor

@WolvenRA @VBAndCs's proposed syntax involves implementing 8 new operators,

AndIsOf
AndIsNotOf
AndAlsoIsOf
AndAlsoIsNotOf
OrIsOf
OrIsNotOf
OrElseIsOf
OrElseIsNotOf

Whereas @AdamSpeight2008's involved zero new operators, just expanding the capability of the two existing operator Is and IsNot. Whos right-hand-side now can operate over a set of types. The lowering (or being rewritten) to an expression consisting of existing operators / expressions, that already has preexisting unit-test coverage.

object Is (Of these types / objects)
object IsNot (Of these types / objects)

@WolvenRA
Copy link

WolvenRA commented May 2, 2020

@WolvenRA @VBAndCs's proposed syntax involves implementing 8 new operators,

AndIsOf
AndIsNotOf
AndAlsoIsOf
AndAlsoIsNotOf
OrIsOf
OrIsNotOf
OrElseIsOf
OrElseIsNotOf

No... I didn't say I wanted any of those. Adding the "Of" portion seems redundant if you're starting with "If TypeOf". The suggestion I like is zspitz's composable types; 'If item Is XmlSchemaElement Or XmlSchemaComplexType Then' Seems much cleaner and consistent with current VB syntax. Also, it seems like the compiler could "translate" (lower, rewrite) the "TypeOf Is ... Or (OrIs) ..." to the;
object Is (Of these types / objects)
object IsNot (Of these types / objects)
if it wanted\needed to. Just out of curiousity (and maybe ignorance), why wouldn't it be "If Type Is.." instead of "If TypeOf Is.." i.e. what's the difference between Type and TypeOf?

@AdamSpeight2008
Copy link
Contributor

TypeExpressions

@VBAndCs
Copy link
Author

VBAndCs commented May 2, 2020

I only suggested OrIs and AndIsNot. No need for anymore operators as this is just type chicking and no need for shortcircuiting operator (which willbe the default, since we will not break anything). Any object can have only one type, so we can't use AndIs nor OrIsNot.
As I said, using (Of ) has another meaning and should be used for. We can't give the same operator different meanings.

@AdamSpeight2008
Copy link
Contributor

AdamSpeight2008 commented May 2, 2020

@VBAndCs Don't fall for the fallacy of "We can't give the same operator different meanings."
The English language has numerous words the have different meanings, given the context they are used in. The key is *context, the same is true for VB.net.

In insolation and without context what does the character < or . mean?

The definition of what an operator does is defined by the object that defines them.
False Assumption Comparision Operators Return Booleans.

Edit Found the hyperlinked grammar for VB.net (version 11)

@zspitz
Copy link

zspitz commented May 2, 2020

Any object can have only one type, so we can't use AndIs nor OrIsNot.

That's not quite accurate. A given object has one type (the result of the call to .GetType()), and each type can have only one base type; but a given type can implement multiple interfaces, or implement an interface in addition to its base type.

@gilfusion
Copy link

I suppose I have been on a bit of a libraries over new language features trend lately, but would these functions be helpful for this scenario?

Function IsAny(Of T1, T2)(obj As Object)
    Return TypeOf obj Is T1 OrElse TypeOf obj Is T2
End Function

Function IsAny(Of T1, T2, T3)(obj As Object)
    Return TypeOf obj Is T1 OrElse TypeOf obj Is T2 OrElse TypeOf obj Is T3
End Function

Function IsAll(Of T1, T2)(obj As Object)
    Return TypeOf obj Is T1 AndAlso TypeOf obj Is T2
End Function

Function IsAll(Of T1, T2, T3)(obj As Object)
    Return TypeOf obj Is T1 AndAlso TypeOf obj Is T2 AndAlso TypeOf obj Is T3
End Function

Here's the original example written using one of them:

If IsAny(Of XmlSchemaElement, XmlSchemaComplexType)(item) Then

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

8 participants