- dominic--
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart
As a developer coming from C/C++ to Dart, 'every object is passed by reference' was one of the biggest surprises.
The semantics make it difficult to move code from a language that passes by value by default, for example, C/C++. It's important to note that many game developers will be coming from code bases written in C/C++. Looking at Dominic's proposal, I've been bitten by the vector example multiple times when moving code from my C++ engine to Dart. Has the Dart team considered adding a struct keyword that mirrors the C# semantics? If so, why was it not added?
As a developer coming from C/C++ to Dart, 'every object is passed by reference' was one of the biggest surprises.Sorry to hear that, but Dart is not at all focused on catering to C/C++ programmers, especially when that conflicts with catering to Javascript or Java programmers. C# is somewhere in the middle - it is after all a hybrid of Java and C++ at heart.The semantics make it difficult to move code from a language that passes by value by default, for example, C/C++. It's important to note that many game developers will be coming from code bases written in C/C++. Looking at Dominic's proposal, I've been bitten by the vector example multiple times when moving code from my C++ engine to Dart. Has the Dart team considered adding a struct keyword that mirrors the C# semantics? If so, why was it not added?
Emulating C/C++ or even C# is a non-goal. We are not seeking to build a new systems programming language.
Conceptually, passing a mutable object by value is an oxymoron. If something is mutable, it cannot be passed by value; you are passing some other object, a copy, not the original. It is best for copying to explicit. It makes costs more explicit, and much more importantly, makes the semantics explicit - no hidden copying.
The main pros of structs etc. are performance/optimization, but they come at a cost. Adding parameter passing modes adds complexity to the object model and cognitive load to the user. Generally, this something that high level languages shy away from.
Supporting these features in that form is not particularly important in application programming, but is unattractive to Javascript or Java programmers, who are our main audiences.I'll add that value types as I described in my previous message can really be passed without risk of mutation, whereas structs contain mutable references which could lead to mutations in any case.
--
Cheers, Gilad
The semantics make it difficult to move code from a language that passes by value by default, for example, C/C++. It's important to note that many game developers will be coming from code bases written in C/C++. Looking at Dominic's proposal, I've been bitten by the vector example multiple times when moving code from my C++ engine to Dart. Has the Dart team considered adding a struct keyword that mirrors the C# semantics? If so, why was it not added?
Emulating C/C++ or even C# is a non-goal. We are not seeking to build a new systems programming language.
Conceptually, passing a mutable object by value is an oxymoron. If something is mutable, it cannot be passed by value; you are passing some other object, a copy, not the original. It is best for copying to explicit. It makes costs more explicit, and much more importantly, makes the semantics explicit - no hidden copying.
The main pros of structs etc. are performance/optimization, but they come at a cost. Adding parameter passing modes adds complexity to the object model and cognitive load to the user. Generally, this something that high level languages shy away from.
C/C++, and C# especially, are no longer constrained to the systems programming world.
XNA and .NET have made C# wildly popular in the game dev arena and I know, from discussions with developers at the New Game Conf and Game Developers Conference, that they're trying to break into web development but are repeatedly surprised by these features of JavaScript. If we can find a way to add something to the language to help them without changing the default behaviour that developers coming from JS web development expect, then I think it will only benefit the language.
It's very interesting that you see the copying as hidden, whereas I see the reference passing as hidden.
I do agree with your point - passing by value actually means passing a copy. However, I prefer (due to my background) to have control over that copying.The main pros of structs etc. are performance/optimization, but they come at a cost. Adding parameter passing modes adds complexity to the object model and cognitive load to the user. Generally, this something that high level languages shy away from.My argument is that adding the optional parameter passing mode reduces cognitive load for some users.
In my case, I have to remember to add an explicit copy when I want to pass by value. If, instead, I could do this at the API or type level then that load is reduced.
Supporting these features in that form is not particularly important in application programming, but is unattractive to Javascript or Java programmers, who are our main audiences.I'll add that value types as I described in my previous message can really be passed without risk of mutation, whereas structs contain mutable references which could lead to mutations in any case.That's true, and complicates the notion of what it means to copy a complex user-defined value type. I'm a little unclear on what a deeply immutable object would be like to use. For example, if I had:immutable class vec2 {num x, y;}as an example your deeply immutable type, would I be able to pass this an instance to a method, have that method alter the values x and y, but have the original instance left unchanged? Or does deeply immutable mean that methods wouldn't be able to change the values of x and y?
--
I'm going to echo John and Dominics sentiments here. I have seen a tremendous interest in Dart for game developers as it makes a lot more sense to build something like that in Dart than JavaScript. Having the C# style struct that's passed by value would be a great addition.Honestly I think you're missing who your real audience here, people who have a C/C++/C# background and want to get into web programming but cringe at the thought of working in JavaScript.
On Fri, Aug 10, 2012 at 10:15 AM, John McCutchan <jo...@johnmccutchan.com> wrote:On Fri, Aug 10, 2012 at 9:38 AM, Gilad Bracha <gbr...@google.com> wrote:--The semantics make it difficult to move code from a language that passes by value by default, for example, C/C++. It's important to note that many game developers will be coming from code bases written in C/C++. Looking at Dominic's proposal, I've been bitten by the vector example multiple times when moving code from my C++ engine to Dart. Has the Dart team considered adding a struct keyword that mirrors the C# semantics? If so, why was it not added?
Emulating C/C++ or even C# is a non-goal. We are not seeking to build a new systems programming language.No one has asked for Dart to emulate all of C/C++ or C#. The request is very narrow: Dart to support user defined value struct types whose instances are implicitly copied when passed to a function so that the callee's usage of the instance is side effect free from the perspective of the caller.C/C++ and C# are as much application programming languages as they are systems languages. If we were to measure based on lines of code written, all three are hands down application languages. The vast majority of game _applications_ are written in C/C++. Again, the request is very narrow and not a general request for Dart to become a systems programming language.Conceptually, passing a mutable object by value is an oxymoron. If something is mutable, it cannot be passed by value; you are passing some other object, a copy, not the original. It is best for copying to explicit. It makes costs more explicit, and much more importantly, makes the semantics explicit - no hidden copying.
Introducing struct types whose instances are always copied when passed to functions is not hidden copying- the programmer consciously chose to use a struct versus a class and therefore has explicitly requested that copies be made when passed. Similarly if a "val" keyword was added, the programmer has explicitly requested a copy be made.The main pros of structs etc. are performance/optimization, but they come at a cost. Adding parameter passing modes adds complexity to the object model and cognitive load to the user. Generally, this something that high level languages shy away from.C# is a high level language and it offers both struct types and the ref keyword allowing the programmer to control parameter passing.
Thanks,
John McCutchan <jo...@johnmccutchan.com>
On Fri, Aug 10, 2012 at 9:53 AM, dominic <domi...@google.com> wrote:
C/C++, and C# especially, are no longer constrained to the systems programming world.Wrt C# I agree - but this feature comes from its C++ heritage.
XNA and .NET have made C# wildly popular in the game dev arena and I know, from discussions with developers at the New Game Conf and Game Developers Conference, that they're trying to break into web development but are repeatedly surprised by these features of JavaScript. If we can find a way to add something to the language to help them without changing the default behaviour that developers coming from JS web development expect, then I think it will only benefit the language.Adding a feature always comes with a cost. The fact that some objects are passed by reference and some by value is something *everyone* using the language will need to know - they will encounter your code, for example, and need to understand it (or, more likely, be totally surprised).
It's very interesting that you see the copying as hidden, whereas I see the reference passing as hidden.Yes. Your mental model is different - but I'd much rather the C people learn the pass-by-reference idea than burden the Javascript, Java and Ruby developers with the mechanics of the C world.
I do agree with your point - passing by value actually means passing a copy. However, I prefer (due to my background) to have control over that copying.The main pros of structs etc. are performance/optimization, but they come at a cost. Adding parameter passing modes adds complexity to the object model and cognitive load to the user. Generally, this something that high level languages shy away from.My argument is that adding the optional parameter passing mode reduces cognitive load for some users.And, as I note above, it adds load for others. We believe there are more of them than there are of you. Otherwise, Dart might be totally different.
In my case, I have to remember to add an explicit copy when I want to pass by value. If, instead, I could do this at the API or type level then that load is reduced.Supporting these features in that form is not particularly important in application programming, but is unattractive to Javascript or Java programmers, who are our main audiences.I'll add that value types as I described in my previous message can really be passed without risk of mutation, whereas structs contain mutable references which could lead to mutations in any case.That's true, and complicates the notion of what it means to copy a complex user-defined value type. I'm a little unclear on what a deeply immutable object would be like to use. For example, if I had:immutable class vec2 {num x, y;}as an example your deeply immutable type, would I be able to pass this an instance to a method, have that method alter the values x and y, but have the original instance left unchanged? Or does deeply immutable mean that methods wouldn't be able to change the values of x and y?The latter.
...
Learning the idea isn't the problem. Being frustrated by it and having hard-to-discover bugs caused by it is. I'm sure Javascript and Java developers have made the same mistake and it's something that those developers have just learned to remember to think about. We have an opportunity to not have the issue in the first place.
...
--
Hey Matthew,I'm going to assume you haven't used C#, so let me explain a bit on differences between structs and classes in it. So a struct is allocated on the stack, while a class is allocated to the heap. When passed to a function/method a struct is passed by reference by default, this behavior can be modified if requested, and a class is passed by reference.So a struct allows a potential speedup for some situations. Since my background is gamedev, and because 3D either through visualizations or games is whats really going to task the DartVM in the browser, a 3D vector would be declared as a struct. So if we look at this excellent presentation from Lilli Thompson, http://console-to-chrome.appspot.com/#37, we see this bit of code.function add(vecA, vecB) {return new Vector(vecA.x + vecB.x,vecA.y + vecB.y,vecA.z + vecB.z,);}Which is obviously horrible in JS because its going to create an object in the heap. If you do this enough you'll pay for it later. So instead in JS you do this.function addTo(vecA, vecB) {vecA.x = vecA.x + vecB.x;vecA.y = vecA.y + vecB.y;vecA.z = vecA.z + vecB.z;}Which is ugly. And its even uglier if you have a longer expression than just a + b. So the idea is if you have a struct type within Dart you could doVector3 x = (a * b) + c - (f * a);And it wouldn't create a ton of garbage to deal with on the heap.
Few thoughts about this:1. It's important. It would be a huge change. A lot of JavaScript and Java people could be confused.
2. Let's not forget that one of Dart major goals is compilation to efficient JavaScript.
3. Sometimes, value types would be really great. But as an application programmer, I almost never miss them. (I don't do games, though.)
4. On the other hand, I sorely miss the distinction between passing parameters by value and by reference. Here's the thing: in Dart, there are no value types, everything is a reference (well, we can say that the Smi class in Dart VM is a value type, but that's an implementation detail and it really doesn't matter for deeply immutable types). Also, parameters are always passed by value -- where the value is the value of the reference (i.e., the pointer). Wikipedia explains it rather nicely in http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_value (apparently, it's also called passing by sharing). In my opinion, modern languages should either support returning multiple values, or true passing by reference. Maybe both. Dart does neither.5. I don't like the C# way using the "ref" and "out" keywords. I like how Ada did it: for each parameter, I could say that it's "in", "out" or "in out". If it results in pass by reference or whatever is an implementation detail. (Semantics is the same as in C#, though: out = out, in out = ref.)LT
--
--
I think nobody cares of posting in very old threads here,since the language is envolving as were speaking.