Git Product home page Git Product logo

Comments (14)

dpaoliello avatar dpaoliello commented on July 17, 2024 4

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.

HaloFour avatar HaloFour commented on July 17, 2024 1

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.

sharwell avatar sharwell commented on July 17, 2024

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.

MgSam avatar MgSam commented on July 17, 2024

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.

stephentoub avatar stephentoub commented on July 17, 2024

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.

gafter avatar gafter commented on July 17, 2024

@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.

paulomorgado avatar paulomorgado commented on July 17, 2024

@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.

gafter avatar gafter commented on July 17, 2024

@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.

Unknown6656 avatar Unknown6656 commented on July 17, 2024

@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.

HaloFour avatar HaloFour commented on July 17, 2024

@Unknown6656

b <- a is already a legal expression, testing whether b is less than negated a, or b < -a.

from csharplang.

Unknown6656 avatar Unknown6656 commented on July 17, 2024

@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.

sirgru avatar sirgru commented on July 17, 2024

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.

gulshan avatar gulshan commented on July 17, 2024

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.

CyrusNajmabadi avatar CyrusNajmabadi commented on July 17, 2024

Moving to a discussing on csharplang.

from csharplang.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.