0 votes

What's the difference between

var data :Dictionary= JSON.parse(text).result

and

var data := JSON.parse(text).result as Dictionary

Or are they essentially the same? Any performance difference? Etc

asked Oct 7, 2019 in Engine by sprite-1 (58 points)

1 Answer

0 votes

First one specifies that data must be a Dictionary regardless of the assignment.

Second one infers the type of data from the expression on the right of =, if it can be determined. That also means as is not always necessary, I think.

answered Oct 9, 2019 by Zylann (26,055 points)

The reason why I added as is because Godot was complaining that it can't infer the type from JSON.parse() but yeah I'm mostly concerned about data:Dictionary = ___ and data := ____ as Dictionary, at the end of the day,they both essentially have the same end result right?

Kind of, they both tell the editor that this variable should contain a Dictionary. But it doesn't prevent it from getting something else.

Note that the type of JSON.parse(text).result is not known in advance, that's why you'd need to write the type. Because JSON data can either be a dictionary or an array.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.