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 TypeOf Many. #93

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

AdamSpeight2008
Copy link
Contributor

@AdamSpeight2008 AdamSpeight2008 commented May 28, 2017

Initial work on the proposal TypeOf Many.
Edit See Issue / Proposal

@AdamSpeight2008
Copy link
Contributor Author

@AnthonyDGreen
I am currently stuck on how to lower TypeOf obj Is {T0,T1} into
(TypeOf obj Is T0) OrElse (TypeOf obj Is T1)

@xieguigang
Copy link

Using Linq Extension for lower it as (TypeOf obj Is T0) OrElse (TypeOf obj Is T1):

For OrElse

If {GetType(T0), GetType(T1)}.Any(Function(t) t Is obj.GetType()) Then
End If

For AndAlso

If {GetType(T0), GetType(T1)}.All(Function(t) t Is obj.GetType()) Then
End If

@AdamSpeight2008
Copy link
Contributor Author

@xieguigang I don't think using LINQ would be the best target for the lowering, as isn't a direct translation of the intended syntactic sugar.

@dotnet dotnet deleted a comment from dnfclas Nov 1, 2017
@AdamSpeight2008 AdamSpeight2008 mentioned this pull request Dec 24, 2017
@bandleader
Copy link

  1. @xieguigang @AdamSpeight2008 Re: the LINQ solution: you also don't want to use GetType(T0) Is obj.GetType() because that doesn't work for derived types; i.e. TypeOf TextBox Is Control should and does return True, so we should match that behavior.

  2. I didn't see the issue/discussion for this proposal, so I'll comment here -- now that pattern matching is a thing under discussion (and has already landed in C#), doesn't it makes sense to do this as part of that? Since both "type checking" (including type guards a la TypeScript) and "multiple options (OR)" are things planned for that.

@AdamSpeight2008
Copy link
Contributor Author

AdamSpeight2008 commented Feb 1, 2018

@bandleader I'll dig out the issue / proposal and update the first post.
Just letting you know I'm not affiliated with Microsoft. I am an external contributor.

Noted, It also not a good implementation to use for this feature proposal. I think there is too much "machinery" involved. The proposal should generate the same code to start with, plus it gives us the option to use different approach for larger number of types to match against. eg Embedded dictionary lookup, like what is used for same Case Selects

As for as I can ascertain pattern-matching may not be part of the road map. Though there ideas / proposals to extend the capabilities of Select and Is which could solve most of the patterns.

This proposal was targeting a very specific use case, simplifying multiple types checks, that follow the pattern
(TypeOf obj Is T0) OrElse (TypeOf obj Is T1) OrElse (TypeOf obj Is T2) etc and
(TypeOf obj IsNot T1) AndAlso (TypeOf obj IsNot T1) AndAlso (TypeOf obj IsNot T2) etc
being proposed to "syntactic sugered" to
( TypeOf obj Is { T0, T1, T2 } ) and
( TypeOf obj IsNot { T0, T1, T2 } ) respectively.
also should be noted this isn't restricted to generic type parameter T0,T1 and T2 are being used placemarkers for types in the examples.

@bandleader
Copy link

bandleader commented Feb 1, 2018

Though there ideas / proposals to extend the capabilities of Select and Is which could solve most of the patterns.'

@AdamSpeight2008 That's what I meant by pattern matching. If we do an Is operator which supports type matching, as well as OR possibilities in a group, then obj Is {TextBox, ListBox} will work out of the box, with no need to extend TypeOf...Is. I personally don't mind if you implement this as part of TypeOf...Is -- it's just that I think the team is against doing things in >1 place...

@gafter
Copy link
Member

gafter commented Feb 14, 2018

@AdamSpeight2008 The first step in designing a language feature is to open an issue on the vblang repository requesting or proposing the feature where it can be discussed by the community and the LDM. If and when the LDM believes it has converged on a language approach to the problem identified (note that the LDM starts by considering the problem to be solved), then (and only then) it makes sense to add a specification draft. This PR is premature.

@zspitz
Copy link

zspitz commented May 8, 2018

@bandleader Do we need pattern matching? Even if all we implemented was composable types, wouldn't that be enough?

@bandleader
Copy link

@zspitz No; there are tons of things that pattern matching can do (and does do in other languages) apart from checking types.

To be clear, your idea is very valuable and the use case is common. I'm just saying that it will anyway be implemented as part of pattern matching, and therefore, I'm not sure if the language team will want to additionally implement it in the TypeOf operator.

@AdamSpeight2008
Copy link
Contributor Author

Prefer Proposal #481

Base automatically changed from master to main March 9, 2021 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants