You're right that anonymous types could be useful and would play well with the fact that Dart doesn't require type annotations for variables. We may add them at some point, but our goal isn't to have all possible features in 1.0. We want to focus on the core features that programmers really need and leave ourselves room to grow the language over time.
I think the most accurate answer (sadly) lies over here: http://dartbug.com/1089
LT
i.e. The fact that Dart can convert and run as javascript I guess had me thinking that I'd still be able to do object literals and in line JSON objects just like I would in javascript. So that Dart might have made a similar claim to javascript programmers that groovy made to java programmers - start with something basically javascript and add types to enhance it (it's funny groovy of course is the opposite you start with types in java and start removing them as the code becomes more concise and more dynamic whereas here you'd be adding types not removing them).
Leading to the giant love-fest of javascript and java programmers I'd originally envisioned. :)
> i.e. The fact that Dart can convert and run as javascript I guess had me thinking that I'd still be able to do object literals and in line JSON objects just like I would in javascript.
You know, JSON is still a subset of Dart. And guess what, you can build a map-as-object facility in Dart yourself. But I personally think it isn't worth it (even if it would be like 10 or 20 lines of code).
LT
With JavaScript this is differently. JavaScript is a horrible abomination of a language. Chose that as your basis and you won't get any better, only worse.
You know, JSON is still a subset of Dart. And guess what, you can build a map-as-object facility in Dart yourself. But I personally think it isn't worth it (even if it would be like 10 or 20 lines of code).
BTW, Dart can sure have JSON to object conversion in a later stage when it got advanced reflection and maybe dynamic code generation (the feature I hope for). I am not sure about object literals though.
Yeah I played a little with JsonObject and it seems to solve what to me is the main issue simply that Dart dropped support for using the dot notation for accessing the values in Maps.
Which of course breaks the ability to substitute a map for an object.
Even when I was doing groovy this was wonderful for mocking objects when testing.
And as I said my current project is *all* about JSON RESTful services. For me having to always declare and parse JSON as a string rather than just in -line in the code would feel a step backwards.
So I do see these things as important, but the more I read about Dart the more I'm feeling the language designers (the Dart community as a whole?) are coming more from the static programming language tradition. i.e. that being able to know things at compile time, and being able to improve performance using simple offsets to fields in a pre-defined object layout, is felt to be more valuable than dynamic run time stuff afforded by using maps as objects.
That basically Dart is coming more from the point of view of what it would be like for Java/C# style language to run in the browser. And that the optional types are only for when you declare variables referring to objects not when you declare objects themselves.
In a way I'm fine with that I just didn't get that in the beginning I wish this was stated more clearly e.g. on dartlang.org or something.
e.g. If Dart were presented/described as "GWT++" or something and had actually just used an enhanced java syntax directly I might have started with different expectations.
I'll still consider using Dart (e.g. if they get some "location transparency" going with local/remote isolates feeling like a simpler erlang I think that could be really exciting).
But anyway yeah I'm sure it wouldn't be hard to add what JsonObject does to Dart's default Map.
But maybe that's not the direction the language designers want to take.
But to make trouble, I wonder am I allowed to suggest this as an enhancement in that Dart issues list? :)
(I notice I can click the New Issue but the dropdown implies it's really for submitting defects I don't see a choice for "Enhancement")
> Which of course breaks the ability to substitute a map for an object.
>
> Even when I was doing groovy this was wonderful for mocking objects when testing.
I'd say that Dart has you covered here. You can mock simply by passing arbitrary objects, they only have to have the needed methods. And if you want to have some static typing here, you can implement the mocked class's interface. It isn't that convenient, but it isn't that bad either.
> And as I said my current project is *all* about JSON RESTful services. For me having to always declare and parse JSON as a string rather than just in -line in the code would feel a step backwards.
You can still use Dart in a JavaScripty way -- just Maps and Lists, then you have the same Json.parse/stringify. You gain something, and you lose something else.
In the future, I expect some sophisticated JSON/objects mappers, but we are not there yet. Note that Dart isn't finished, especially the libraries will get a lot of love after M1 is out.
> So I do see these things as important, but the more I read about Dart the more I'm feeling the language designers (the Dart community as a whole?) are coming more from the static programming language tradition.
Not really. All the Dart Masters (Lars, Kasper and Gilad) have strong SmallTalk background and Dart is fundamentally dynamically typed. You're right that there is no "free love" in Dart unlike JavaScript, it really has more of a static structure. It has reason, though: Dart is designed to be performant (in not so distant future, Dart should be significantly faster than JavaScript) and toolable (I'd say that Dart IDEs are already way better than IDEs for JavaScript).
> But anyway yeah I'm sure it wouldn't be hard to add what JsonObject does to Dart's default Map.
>
> But maybe that's not the direction the language designers want to take.
That's probably correct. Dart is all about _structure_, which is something that JavaScript severely lacks.
> But to make trouble, I wonder am I allowed to suggest this as an enhancement in that Dart issues list? :)
>
> (I notice I can click the New Issue but the dropdown implies it's really for submitting defects I don't see a choice for "Enhancement")
Sure you are allowed! Just delete all the text that the defect template suggests and go ahead!
LT
> Which of course breaks the ability to substitute a map for an object.
>
> Even when I was doing groovy this was wonderful for mocking objects when testing.I'd say that Dart has you covered here. You can mock simply by passing arbitrary objects, they only have to have the needed methods. And if you want to have some static typing here, you can implement the mocked class's interface. It isn't that convenient, but it isn't that bad either.
#import("package:JsonObject/JsonObject.dart");
class RealGuy {
String name;
int age;
RealGuy(this.name, this.age);
void incrementAge() { age++; }
}
class MockGuy {
String name = "fake";
int age = 25;
MockGuy(name, age);
void incrementAge() { age++; }
}
void happyBirthday(RealGuy g) {
// the dots below are a problem if g is a Map not JsonObject:
g.incrementAge();
print("${g.name} had a birthday: ${g.age} years old");
}
void main() {
RealGuy g = new RealGuy("derek", 42);
happyBirthday(g);
MockGuy m = new MockGuy("",0);
happyBirthday(m);
// dot notation will fail without JsonObject
// but JsonObject blows up if a closure is in the Map
var mockMap = new JsonObject.fromMap({
"name": "map",
"age": 26,
"incrementAge": () { /* also can't do e.g.: this["age"]++;*/ }
});
happyBirthday(mockMap);
}
Not really. All the Dart Masters (Lars, Kasper and Gilad) have strong SmallTalk background and Dart is fundamentally dynamically typed. You're right that there is no "free love" in Dart unlike JavaScript, it really has more of a static structure. It has reason, though: Dart is designed to be performant (in not so distant future, Dart should be significantly faster than JavaScript) and toolable (I'd say that Dart IDEs are already way better than IDEs for JavaScript).
What I feel after reading more and playing with Dart more is that Dart is dynamically typed but it doesn't qualify as a Dynamic Programming Language in the way described if you follow that link (on wikipedia which describes it the way I'd more or less come to think of it).
And any arguments over semantics aside, if somebody asks me I would tell someone starting with Dart that they should expect to think more like java, to start by defining their classes and interfaces and that's the expectation in Dart.
i.e. To convey this idea that in Dart types are optional for variable declarations but *types are not optional* in the sense that you need to define types (i.e. classes) for any/every object you're dealing with.
Which as I said for me means Dart is far closer to Java or C# than javascript.
And I think the reason traces back to the JSON RESTful services, which I think also relates to the rise of these schema-less NoSQL databases that are so good at storing "semi-structured" data.
Which is to say I think today a lot of these modern service oriented systems would fall into this category.