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

Allow ref assignment in ternary operator and switch expression #2507

Closed
Thaina opened this issue May 7, 2019 · 4 comments
Closed

Allow ref assignment in ternary operator and switch expression #2507

Thaina opened this issue May 7, 2019 · 4 comments

Comments

@Thaina
Copy link

Thaina commented May 7, 2019

bool isRight = false;
SomeObject left,right;
ref var side	= isRight ? ref right : ref left;
side = new SomeObject(); // assign to left

Currently now this code above throw error

A ref or out value must be an assignable variable [Assembly-CSharp]

I think it should be available. Is it possible? It should also work with switch expression too

@CyrusNajmabadi
Copy link
Member

Works fine for me as:

        bool isRight = false;
        string left = null, right = null;
        ref var side = ref isRight ? ref right : ref left;
        side = "whatever";

A couple of notes:

  1. left and right must be assigned first. You can't take a ref to something unassigned because you have no idea if it will be read or not. There's no concept at the statement/expr-level in the language of "this is a ref which must first be assigned before you can read it".
  2. You need to write your assignment as = ref. = ref is the ref-assignment operator. It's necessary even if sub-exprs are 'refs'. That's why you can't do:

image

You still have to write it as:

image

The same holds here. So you need: ref var side = ref isRight ? ref right : ref left;

A bit of a mouthful, but totally supported.

@CyrusNajmabadi
Copy link
Member

It should also work with switch expression too

Can you file a separate issue for that? Compounding multiple requests into one issue just makes it harder to handle them.

@Thaina
Copy link
Author

Thaina commented May 7, 2019

Oh I see, that was unexpected that we need ref for all places. I have tried to place ref in front before

This syntax is not so intuitive but still fine. I think the switch expression should work the same way so I would close this for now

Thank you very much

@Thaina Thaina closed this as completed May 7, 2019
@CyrusNajmabadi
Copy link
Member

You're welcome :) The intuition here is that you can only assign a ref with = ref. That's what ref-assignment is in the language.

I think the switch expression should work the same way so I would close this for now

I would personally agree. :) It might not happen in the initial release due to time constraints. but i def think it could happen later down the line.

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

2 participants