After coming back to Snap! after a while of programming in JS, I've come to notice something:
Data doesn't have types here, instead the type is detected as you use the values.
For example, is interpreted as the number
1
when evaluated in , but as the text
true
when evaluated in . Hence,
, but
.
This means that we can convert numbers to text and back quite effortlessly compared to other languages. Data types don't matter, it just chooses the best data type that will work for the job, and goes with that.
But there's some behaviour that I need clarification on:
I have a sprite with two costumes. The first costume has the name 2
, and the second costume has the name 1
.
can be given input in one of three ways:
- Selecting the costume manually:
- Inputting a costume number:
- Inputting a costume name:
The curious behaviour occurs when the costume names themselves are numbers. This is what happens:
switches to costume #2.
switches to the costume named
2
.
Wait a second, that conflicts with what we discovered earlier. and
, which is the same. If values did not have data types, then
would not be able to tell if that
2
is a number or some text, and it would have to guess. But somehow, it knows whether the 2
came from a math operation or from a text operation.
doesn't seem to want to corroborate this though. I've fed it some questions like so:
says that both
and
are numbers. It is completely oblivious to whether
2
came from a number or a text operation, or it just doesn't care. I would expect at least the last of those four to report , but nope!
Yet from the costume tests, it is clear that these values contain associated information of their "type," or at least, what type of operation was used to make them. This even works through variables with the costume method too!
Clearly is either lying to me or using a different method of finding the value's "type."
So what's up with data types, and can they be controlled? For example, what if I want some text that is 2
?