Strongly typed instance variables?

Discussion and feedback on Construct 2

Post » Sun Mar 06, 2011 3:39 pm

Here's something I thought of recently: right now in C2, like with 0.x, you can store any type in any instance variable. By type I mean boolean, number or string. So you can assign "hello" to a number instance variable.

As a result the editor can't assume that instance variables have any particular type, in case you change it at runtime. For example, Sprite: Set X to Sprite.MyStringInstanceVariable has to be accepted, in case you set that instance variable to a number at another time.

What I think would be a good idea is to strong-type instance variables. This means you can't assign a string to a number instance variable - if it is initially set to a number, it always stores a number, and can never store a string. Likewise string instance variables will always store a string, and never a number (although they can store numbers as character strings).

This slightly reduces their flexibility, but improves the editor checking - now the editor can reject setting X to Sprite.MyStringInstanceVariable, which was probably a mistake anyway.

Does anyone use the weak-typing (storing strings in number variables etc.)? I think any time you need to do that an easy workaround is just to add another instance variable. It makes sense to me. Is there general agreement?
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Sun Mar 06, 2011 4:03 pm

I didn't even realize it worked that way, as I always use the appropriate variable type for what I'm doing. I vote for strong typing.
Moderator
B
97
S
36
G
19
Posts: 1,584
Reputation: 17,692

Post » Sun Mar 06, 2011 4:18 pm

I kind of liked being able to store data where ever I liked, and using str(), or int() when ever a specific type was needed.
What about a 4th data type? One that lets you store anything.
Then again I guess you could do that with string anyways......
Meh, what ever floats your boat I guess.

One thing, hows this going to play with data storage objects?
Wont there need to be some sort of preference set?
Image Image
B
161
S
48
G
89
Posts: 7,347
Reputation: 66,249

Post » Sun Mar 06, 2011 4:27 pm

What will happen if you try to assign a string to a number var? Will it cause an error, or variable just reset?
B
2
S
2
G
2
Posts: 158
Reputation: 1,366

Post » Sun Mar 06, 2011 4:28 pm

So in 0.x, it would change the variable type to whatever I set it to at runtime? I had no idea. :/

Newt's suggestion of a fourth type is a good one, I think. Also, please leave hash and array the way they are - having to manually set variable types for those would be a lot of extra steps.
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Sun Mar 06, 2011 4:41 pm

Oh, this won't affect anything other than instance variables. I suppose if you try to set a string to a number variable, it could either ignore it completely, or do an automatic number conversion on the string.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Sun Mar 06, 2011 4:47 pm

I say yes as long as you can easily try to cast a string to a number. Would numbers have different data types like floats doubles and ints as well? What about unsigned stuff?
B
5
S
2
G
4
Posts: 632
Reputation: 2,829

Post » Sun Mar 06, 2011 5:27 pm

What would be nice would be to have type hierarchy.
Like in OOP.

And on top of that, some kind of constraints based on families. (to simulate tagging interfaces or multiple inheritance)
B
18
S
4
G
6
Posts: 17
Reputation: 4,353

Post » Sun Mar 06, 2011 6:38 pm

.... and then there's array's in instance variables. Lots of potential there.
What would be nice is something similar to Lucid's "S".
Sprite.variable(2) would give you the second value indexed in sprite's instance variable "variable".
A stack for each variable... oh heck yeah!
Image Image
B
161
S
48
G
89
Posts: 7,347
Reputation: 66,249

Post » Sun Mar 06, 2011 7:36 pm

I vote yes. Like linkman2004 I've always used the proper type anyways. I think having it be strongly typed would help keep things ordered.
B
73
S
20
G
10
Posts: 524
Reputation: 9,896

Next

Return to Construct 2 General

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests