admin管理员组文章数量:1434916
I have a function with following signature
State MoveForward(in Path path);
Path
is a ref struct
State
is also a ref struct
with just integer.
Now I have another helper function
bool TryMoveForward(in Path path, ref State state)
{
// which does something like:
state = MoveForward(path); // "line A"
return !state.IsEnd(); // IsEnd determined by integer != -1
}
At the "line A" I am getting the error CS8347: Cannot use a result of 'MoveForward' in this context because it may expose variables referenced by parameter 'path' outside of their declaration scope.
But I think this shouldn't be the case or atleast compiler can be smart enough to avoid throwing this error for few reasons:
- State is created just with integers (Compiler can look into the usage of
path
). There is no reference exposed from "path" - "State" is a ref struct which doesn't carry any "ref" to escape with.
How can I make compiler not complain about this?
I don't understand whether it is a problem of assigning something to ref State state
or passing the path
to in
parameter.
If I remove in
in MoveForward
the error goes away. I want to keep the "in"
If I split the line, I don't have any way to avoid the error. eg:
var result = MoveForward(path); // "line A"
state = result;
return !state.IsEnd(); // IsEnd determined by integer != -1
Getting CS8352: Cannot use variable 'result' in this context because it may expose referenced variables outside of their declaration scope.
state
has its own memory in stack and result
should be able to be copied to state
similar to single line statement.
I have a function with following signature
State MoveForward(in Path path);
Path
is a ref struct
State
is also a ref struct
with just integer.
Now I have another helper function
bool TryMoveForward(in Path path, ref State state)
{
// which does something like:
state = MoveForward(path); // "line A"
return !state.IsEnd(); // IsEnd determined by integer != -1
}
At the "line A" I am getting the error CS8347: Cannot use a result of 'MoveForward' in this context because it may expose variables referenced by parameter 'path' outside of their declaration scope.
But I think this shouldn't be the case or atleast compiler can be smart enough to avoid throwing this error for few reasons:
- State is created just with integers (Compiler can look into the usage of
path
). There is no reference exposed from "path" - "State" is a ref struct which doesn't carry any "ref" to escape with.
How can I make compiler not complain about this?
I don't understand whether it is a problem of assigning something to ref State state
or passing the path
to in
parameter.
If I remove in
in MoveForward
the error goes away. I want to keep the "in"
If I split the line, I don't have any way to avoid the error. eg:
var result = MoveForward(path); // "line A"
state = result;
return !state.IsEnd(); // IsEnd determined by integer != -1
Getting CS8352: Cannot use variable 'result' in this context because it may expose referenced variables outside of their declaration scope.
state
has its own memory in stack and result
should be able to be copied to state
similar to single line statement.
1 Answer
Reset to default 2Based on this explanations https://github/dotnet/csharplang/blob/main/proposals/csharp-11.0/low-level-struct-improvements.md#keywords-vs-attributes
Opt out
is a keyword (scoped) but the opt in
is an attribute ([UnscopedRef]). So in your case you must defined it as scoped:
State MoveForward(scoped in Path path)
本文标签: cCS8347CS8352 with quotref structquot and quotinquotquotrefquot parameterStack Overflow
版权声明:本文标题:c# - CS8347CS8352 with "ref struct" and "in""ref" parameter - Stack Overfl 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745628674a2667133.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
State
looks like anout
rather than aref
inTryMoveForward()
– Jimi Commented Nov 18, 2024 at 11:20TryMoveForward
also uses input fromstate
– Vivek MVK Commented Nov 18, 2024 at 12:14MoveForward()
is then what? Based on the code posted here, I assumebool TryMoveForward(in Path path, out State state)
would compile. Maybe something actually relevant is missing? – Jimi Commented Nov 18, 2024 at 13:36