Comments (14)
For locals, would it be better to "uninitialize" the variable instead of setting it to default(T)
:
var foo = move bar;
Console.WriteLine(bar.ToString()) // Compilation error: 'bar' is uninitialized
from csharplang.
It feels weird to have a keyword dedicated something like this. Maybe some kind of exchange operator?
string s1 = "hello";
string s2 <= s1;
I'm not proposing <=
specifically, I don't even really care for it, but no character combination immediately came to mind.
Could the same feature be used for more general purpose swaps, instead of zeroing out the source?
int x = 1, y = 2;
x = swap y;
Debug.Assert(x == 2);
Debug.Assert(y == 1);
from csharplang.
For types that fit in a machine word, you have this:
string s1 = "hello";
string s2 = Interlocked.Exchange(ref s1, null);
The definition of move s1
could be a straight translation to:
Interlocked.Exchange(ref s1, default({typeof s1})
Obviously this is shorter, but I'm not (yet) sure it's worth the additional complexity.
from csharplang.
Agree that this seems like way too specialized a scenario for language support. It saves only a few keystrokes and is only useful in a very small set of circumstances.
The only real benefit I can see is in contexts where an expression is required. In these situations however, the ;
operator would be a better solution as it allows you to accomplish what you want while being much more general purpose.
from csharplang.
Regarding value, please see the last paragraph of the proposal above:
On its own, 'move' isn't particularly valuable; after all, its functionality can be achieved using a Move method like that previously shown. Its value comes from the compiler understanding the implications of it, which enables additional features to be implemented that rely on the compiler having this knowledge: see dotnet/roslyn#161.
I simply separated it out from dotnet/roslyn#161 since it can stand on its own.
from csharplang.
@dpaoliello I think making the variable not definitely assigned is a great idea. But I think it would still have to be assigned default(T)
, as the variable may have been captured by a lambda or ref variable (see dotnet/roslyn#118) and used elsewhere.
from csharplang.
@gafter, I think it needs to be one way or the other. And O like @dpaoliello 's suggestion better than @stephentoub 's. And this is where the compiler can bring value over simply using an Interlocked
method.
If the variable has already been captured, this might be a problem and the user must choose if she want the variable captured or move its value. And it's not hard to work around it.
This:
var a = 2;
var t = Task.Run(() => { DoSomething(); DoSomethingWith(a); });
var b := a; // compiler error
Would have to be this:
var a = 2;
var aa = a;
var t = Task.Run(() => { DoSomething(); DoSomethingWith(aa); });
var b := a;
By the way, I tried out :=
. Not a good idea, though, beacause it's already used for attribute property initializer.
from csharplang.
@paulomorgado Generally when you have two otherwise orthogonal language features, if there is a special rule for when they are used together that is a bad language design smell. So your suggestion that move() and capture are mutually exclusive smells bad to me.
This feature was carefully designed to be part of a coherent set of features including dotnet/roslyn#161, for which there is some practical experience. Is that feature set still coherent with your suggested change?
from csharplang.
@HaloFour: How about the syntax a <- b
? <=
is already used in comparison expressions π
int a = 5;
int b <- a;
// b has the value 5;
int a = 5, b = 2;
a swap b;
// a has the value 2, and b the value 5
// could also be achieved using the following line:
a ^= b ^= a ^= b;
from csharplang.
b <- a
is already a legal expression, testing whether b
is less than negated a
, or b < -a
.
from csharplang.
@HaloFour: damn, I forgot ... maybe the token <>
or <|>
could be used to indicate a swap and some sort of pipe (e.g. <|
) for a value move...
from csharplang.
I think it would be required to have a dedicated keyword for this operation, otherwise you would not be able to write:
SomeMethod(move omod1);
without
var omod2 ~= omod1;
SomeMethod(omod2);
which is not quite the same.
it also makes sense analogous to the new
keyword, the same way we can 'new something up' we can 'move an existing value'.
If there was a shorthand, then it would make most sense to me to write ~=, as in 'destroy the previous one and assign'.
from csharplang.
I think move
should make the moved identifier invalid for later user, enforced by compiler. But, it should not do anything with the value in the memory. For objects, GC will then just have one less reference to the object in memory.
from csharplang.
Moving to a discussing on csharplang.
from csharplang.
Related Issues (20)
- Extension lowering HOT 46
- More user-friendly Compile-time type checking / ζ΄ε δΊΊζ§εηηΌθ―ζΆη±»εζ£ζ₯ HOT 2
- Feature Request: Syntax Sugar for AddRange in List Initialization
- [Proposal]: request support for the ISO 8601 standard 24:00:00 time format in C# HOT 2
- C#: foreach breaking when the IEnumerator.MoveNext method throws exceptions HOT 2
- [Proposal]: Dictionary expressions HOT 94
- strong typed CallerAttributes like CallerMemberName CallerArgumentExpression HOT 2
- CS8509 - Consider Exhaustiveness For Algebraic Data Types HOT 17
- [Proposal]: Implementation specific documentation HOT 2
- [Proposal]: First-Class Span Types HOT 30
- C# needs some way to transition between generic type restrictions without boxing or reflection HOT 6
- [Proposal]: Collection Expressions Next (C#13 and beyond) HOT 47
- Open issues: Breaking changes HOT 29
- [Proposal]: Field and value as contextual keywords HOT 33
- [Proposal]: Extended identifier syntax
- [API Proposal]: IsNullableAttribute to flow nullability of generic type parameters into methods HOT 4
- Anonymous type optimization HOT 5
- Feature Request: Recursively Foreach
- Compiler can't determine best common type for `switch` expression HOT 8
- [Proposal]: Relax `Add` requirement for collection expression conversions to types implementing `IEnumerable` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from csharplang.