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

Range 1 To 10 Step 2 expressions #25

Open
AdamSpeight2008 opened this issue Feb 22, 2017 · 9 comments
Open

Range 1 To 10 Step 2 expressions #25

AdamSpeight2008 opened this issue Feb 22, 2017 · 9 comments

Comments

@AdamSpeight2008
Copy link
Contributor

AdamSpeight2008 commented Feb 22, 2017

(Ported from Roslyn repo)

Range Query Syntax

Dim odd = From x In 1 To 100 Step 2
Dim HundredToZero = From x In 100 To 0 Step -2

Updated (04 Dec 2017)
The range expression will produce a range object, that has the capability of being enumerated.

Dim r As Range(of Byte, Sbyte) = Byte.MaxValue To Byte.MInValue Step -1

Compatibility rules of types, will be the same as those used for addition and subtract.
This to allow simple unsigned type to be used with signed types.
We may want a code advisory, that can detect possible failures at compile-time. Eg unsigned beyond the limits of signed.

The range used will be (inclusive, inclusive), so that expressing the full range of values is permitted eg.

Dim byteValues = From Byte.MinValue To Byte.MaxValue ' 256 values (not 255)` 

Production of the values from the enumerator isnot allowed to overflow / underflow, regardless of any options eg Option Checked Off This is to allow an alternative form of For Loop

For Each value In Byte.MaxValue To Byte.MinValue Step -1
 '...
Next
@AdamSpeight2008 AdamSpeight2008 changed the title [Proposal, LINQ) Range Query Syntax. [Proposal, LINQ] Range Query Syntax. Feb 22, 2017
@AnthonyDGreen
Copy link
Contributor

LOVE IT!

@AdamSpeight2008
Copy link
Contributor Author

@AnthonyDGreen I've updated this proposal to include the feature discuss in #180

@AdamSpeight2008
Copy link
Contributor Author

@AnthonyDGreen
What are your thoughts of altering the parsing (etc) to use range expression parsing?
eg For x = rangeExpression

Then in binding and lowering detect this and convert to use For Loop semantics, rather than Range semantics. This would allow us to reuse the existing parsing code (note changed to return a RangeSyntax ).

@AdamSpeight2008
Copy link
Contributor Author

@AnthonyDGreen
There already are two forms in the grammar a RangeArgumentSyntax and RangeCaseClauseSyntax
So it maybe beneficial to extract the Range out as separate node. eg RangeStatementSyntax then extend it to include the step.

@AnthonyDGreen
Copy link
Contributor

Changing existing nodes is incredibly tricky due to back compat problems. The syntax nodes are a public API and changing/removing nodes can easily break consumers, including analyzers. Plus it's not usually desirable to have a single node in multiple places just because of visual similarity.

@zspitz
Copy link

zspitz commented Feb 14, 2018

@AdamSpeight2008 Would the iteration variable be Integer? Long?

@AdamSpeight2008
Copy link
Contributor Author

@zspitz The type used for the iteration variable, will follow the same type rules used by For Loop
I just used type inference in the example to simplify the code, it will still be possible to include it.

@pricerc
Copy link

pricerc commented Apr 30, 2019

For a slightly different scenario. I want to use a standalone range inside an XML literal.

e.g. given this today:

	Dim qx = 
	<div>
		<%= From i In {1, 10} Select <item ref=<%= $"{i}" %> /> %>
	</div>

I want to replace {1, 10} with a range 1 To 10. Is this sufficient, or is there a case for more decoration (like braces to contain the range)

	Dim qx = 
	<div>
		<%= From i In 1 To 10 Select <item ref=<%= $"{i}" %> /> %>
	</div>

	Dim qxB = 
	<div>
		<%= From i In {1 To 10} Select <item ref=<%= $"{i}" %> /> %>
	</div>

(I'm not a fan of squiggles in VB, but ranges are sets and {} are set delimiters, and we're already using them like this for arrays, a different flavour of set.)

@AdamSpeight2008
Copy link
Contributor Author

AdamSpeight2008 commented Apr 30, 2019

If we get the the operations ordering correctly we shouldn't need parenthesis, for example
For i In 0 To 10 but you can add them if you required From i In (0 To 10)
The range would be effectively be performing as an IEnumerable of Integer in the examples.

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

No branches or pull requests

5 participants