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: multiple variables declared in a 'let' query clause #13919

Closed
gafter opened this issue Sep 19, 2016 · 4 comments
Closed

Proposal: multiple variables declared in a 'let' query clause #13919

gafter opened this issue Sep 19, 2016 · 4 comments

Comments

@gafter
Copy link
Member

gafter commented Sep 19, 2016

I propose to allow multiple variables in a query expression's let clause.

The specification would be changed to support this as follows:

A query expression with a let clause

from x in e
let y = f, z = g, …
…

is translated into

from * in ( e ) . Select ( x => new { x , y = f, z = g,} )

this would facilitate the use of out var arguments in query expressions with no additional machinery:

from x in e
select y = M(out var z0), z = z0

It may also have some synergy with tuples and pattern-matching.

/cc @jcouv @VSadov @AlekseyTs @MadsTorgersen

@HaloFour
Copy link

HaloFour commented Sep 19, 2016

Please also consider the select+filter scenario of using pattern matching with LINQ which I think will become and incredibly common use case.

Would be nice if out variables in LINQ clauses could automatically be promoted to range variables. I know that would drastically change how the LINQ clauses are translated.

IEnumerable<string> words = ...;
var numeric = from word in words
    where int.TryParse(word, out int number)
    select number;

// translated into

var number = words.Select(word => {
        int number;
        bool success = int.TryParse(word, out number);
        return (word, success, number);
    })
    .Where(tuple => tuple.Item2)
    .Select(tuple => tuple.Item3);

@AlekseyTs
Copy link
Contributor

I would like to point out that the proposed translation looks inconsistent with VB translation. In VB, comma inside Let clause is causes an extra Select call and all range variables declared in the Let clause prior to the comma are in scope after the comma. I.e. Let a = <expr1>, b = <expr2> is equivalent to Let a = <expr1> Let b = <expr2> and a is in scope in expr2.

@gafter
Copy link
Member Author

gafter commented Dec 9, 2016

We considered this yesterday in the LDM and we believe we are more likely to support

A query expression with a let clause

from x in e
let (y, z) = e2
…

is translated into

from * in ( e ) . Select ( x => new { x , y = (var (y, z) = e2).Item1, z = z } )

/cc @dotnet/ldm

@gafter
Copy link
Member Author

gafter commented Jan 10, 2017

Dup of #13964

@gafter gafter closed this as completed Jan 10, 2017
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

3 participants