What is best practice for typing variables in QML?
This code:
"for (var j in chords){
var z = j+1"
gives z = 01
so that console.log ("chords[z].ticks = ", chords[z].ticks) gives a type error!!!
Very confusing!!
var z = ++j
or
var z = 0;
z = j+1
works
Any suggestions?
EDIT: ++j is no good: will advance j twice in the loop
EDIT2: now
var z = 0;
z = j+1
also gives z = 01
but
z = ++j
followed by j-- is OK
Comments
maybe coz the javascript inside qml is said to be loosely typed
I keep it that way most of the time so that I can code quick and dirty, I saw coders use prefix
var boolTest = true
var uText = 'foobar'
In reply to maybe coz the javascript… by msfp
Thanks for the String() & Number() tricks. Did not know about those
If
var z = j+1
gives 01, it means j is string, not number
just do this instead:
var z = (+j)+1
and that will work as you want
In reply to If var z = j+1 gives 01, it… by frfancha
And when you say
var z = 0;
z = j+1
"works", that just impossible.
If
var z = j+1
gives 01, so will the other code
In reply to And when you say var z = 0;… by frfancha
Thanks for your comment. You are right. I ended up using the ++j; j-- trick
In reply to Thanks for your comment. You… by elsewhere
I wouldn't do ++j followed by --j, at least not if any other dev is supposed to read your code.
+j
Is the universal way to convert the string in number and should be used instead
In reply to If var z = j+1 gives 01, it… by frfancha
Thanks for this trick. It works, but why?
In reply to Thanks for this trick. It… by elsewhere
(+j) makes clear that j is to be taken as a number, not a string
In reply to (+j) makes clear that j is… by Jojo-Schmitz
It doesn't "make clear", it is actually converting the string in number
In reply to It doesn't "make clear", it… by frfancha
Basically what I meant
In reply to Basically what I meant by Jojo-Schmitz
@Jojo this is not for you as I know you know, but I disagree with "converts" is the same as "makes clear it is"
"Makes clear it is" a number implies it was already one and the interpreter wasn't able to detect it.
This is not true, j is not a number but a string and and a real action (convert) is necessary to get a number.
It doesn't convert j itself by the way, j stays a string, only the expression value +j is a number.
Converting j itself would require to replace its value e.g. by
j = +j;
In reply to (+j) makes clear that j is… by Jojo-Schmitz
I don't want to harp on this , but after
for (var j in chords){
j cannot possibly be a string (very counter intuitive)
In reply to I don't want to harp on this… by elsewhere
chords is a JavaScript object (an array, I know, but basically an object)
The 'in' operator will iterate on the attributes of that object, therefore string items.
If you want to iterate on numbers do this :
for (let j = 0; j < chords.length; j += 1) {
In reply to chords is a JavaScript… by frfancha
FYI: in the above example
"for (var j in chords){
var z = j+1”
replacing z = j+1 with z = j gives no type error. So j is not a string per se.
The default value of a variable in JavaScript is null or undefined.
https://stackoverflow.com/questions/14603106/default-value-of-a-type-in….
To make z a string after j+1 is an outright error in my eyes.
EDIT: and it probably occurs because you are allowed to concatenate strings with +
EDIT2: And Stack Overflow agrees with me:
“Why is + so bad for concatenation?”
https://softwareengineering.stackexchange.com/questions/90203/why-is-so…
Per:
https://www.c-sharpcorner.com/article/avascript-type-coercion-explained….
“Javascript is a dynamically typed language, meaning that variable types are not declared, but rather are inferred by the interpreter at runtime.”
In reply to FYI: in the above example … by elsewhere
"replacing z = j+1 with z = j gives no type error. So j is not a string per se."
JavaScript variables can hold anything. Being able to assign X to Y doesn't prove anything about the previous type of Y.
In reply to FYI: in the above example … by elsewhere
Yes we know all this.
You are just repeating what I said in my first post.
In reply to FYI: in the above example … by elsewhere
If you need information about JavaScript online, avoid stackoverflow (at least as starting point), use mozilla developer network instead, their content is of super high quality.
If you google something you can just add MDN to your search to guide google (or site:... But usually just MDN is enough).
If books are your thing, read 2 of them : the first 10 chapters of "JavaScript the definitive guide" and "JavaScript the good parts".
In reply to If you need information… by frfancha
It'd be beneficial for readers to also know that @elsewhere pointed out a "known" confusing / bug-ish behavior of the musescore 3 plugin coding system. The "for ... in" sometimes returns index number as string unexpectedly, like your mother-in-law paying you surprise visits, forcing you search best practice online. Could be a "Quit My Life" situation, aka QML: might be originated from qml javascript engine's implementation of list / dictionary.
In reply to It'd be beneficial for… by msfp
"sometimes returns string"
No, it always returns string.
And this has nothing to do with 'the QML plugin system'. It is just pure JavaScript.
Hopefully this helps:
In reply to "sometimes returns" Do not… by frfancha
You are correct, thank you! I meant the other way round
edit: replaced bad example
In reply to You are correct, thank you!… by msfp
In reply to You are correct, thank you!… by msfp
Why are you writing " i in dict expect number"???
'in' is an operator looping on the keys of the object.
These keys are string.
In reply to Why are you writing " i in… by frfancha
no point pondering what I just don't know (1) by chinadoll