Sign in to follow this  

language design: assignment, copying, swapping and ?

This topic is 3865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was thinking about the language that I am implementing and I wondered what a full set of data movement/replication operations would be. In C++ we have: assignment operator copy constructor std::swap (you can partially specialize it, though I don't and I doubt that many do) C++ will also be getting a move from what I understand. So given two variables A and B, with starting values of a, b or - (uninitialized) what are the various operations? Here is what I have identified: assignment (a b -> b b) copy construction (- b -> b b) swap (a b -> b a) move (a b -> b -) move construction (- b -> b -) There could be more operators such as an assignment operator that changes it right operand instead of its left, but since they are symetrical to the above cases I ommited them. You could also make a case that a conditional swap would be a sixth operator, called sort. Then the concept can be extended to more arguments, and swap becomes rotate left and rotate right. However I am not going to worry about that kind of thing for a long time if ever.

Share this post


Link to post
Share on other sites
Ignoring language and compiler specific issues, such as memory allocation, or references, or something more obscure...

You need assignment.

Everything else is defined from there:
Swap(x, y): T = X; X = Y, Y = T;
Move(X, Y): Y = X;
CopyC(X): this = X;
MoveC(X): this = X;

The reason things are so complex in C++ is because C++ is complex language. In most way, far too complex. All of these operations are needed just because of all the quirks in allocation, subclassing, various reference semantics, and so on...

Your best bet is to solve these problems, rather than copying the C++ mess.

First and by far one of the worst possible situations that C++ gives you is the unitialized state. Every language since then has dealt with that once and for all, especially when dealing with classes.

Share this post


Link to post
Share on other sites
What Antheus said.

Actually, you don't strictly even need assignment; just a copy operation is sufficient, if you're willing to program in a functional style with no mutable state. (Arguably, minimization if mutable state is a good thing regardless of what paradigm you code in, but I'll try not to derail the topic too much [smile])

I strongly second the sentiment that C++ is very much the wrong language to look at for inspiration when creating a new one.

Share this post


Link to post
Share on other sites
plenty of people are making languages that aren't like C++. I like having references, pointers, and values all be distinct. So I am making a language with a lot in common to C++ semantically. Syntactically my language is somewhat like C++ but java and C# are both more closer to C++ than my language. I have added a lot more stuff too, first class functions, multiple dispatch, and tuples. What separates my language from C++ is that I have brutally elegant and nearly orthagonal syntax. My parser is 650 lines of easy to read C++.

Oh and the domain is general purpose.

Share this post


Link to post
Share on other sites

This topic is 3865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this