This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-03-30 06:52:29

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Something beautiful about casting lists in Scratch

So I was writing a Wiki article on casting, and while researching how lists are cast, found out some awesome stuff I though I should share with the advanced community to see how they feel.

It turns out the Scratch team apparently put a lot of thought into the casting rules, as they are simple and make a lot of sense.

If you forgot what casting is, look at the Wiki article I was making: wiki.scratch.mit.edu/wiki/Casting  tongue

Casting to strings
• If all items of the list are one character long, the characters are concatenated
[a, b, c] => "abc"
• If one or more of the items have many letters, they are concatenaed with a space in between:
[a, cool, article] => "a cool article"

Casting to numbers
This bit is even more interesting:
•Empty list casts to 0:
[] => 0
•A list full of single digits and up to 1 decimal point are concatenated to a number:
[3, ., 1, 4] => 3.14
•A list with one or more multicharacter items is cast to 0 if the first item is a string or non-digit character, or the first item if not:
[12, hi] => 12
[hi, there] => 0

Last edited by Hardmath123 (2012-03-30 06:55:53)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#2 2012-03-30 12:29:46

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Something beautiful about casting lists in Scratch

That is amazing, particularly casting a list to a string. I did not know this.  smile  I can tweak the toy shell project I was writing, now...  tongue


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#3 2012-03-31 13:19:06

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Something beautiful about casting lists in Scratch

This is so very interesting!
I can only hope that whoever takes care of 2.0 remembers to add these details back in, because some of my projects already make use of them  big_smile

Offline

 

#4 2012-04-01 15:06:46

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: Something beautiful about casting lists in Scratch

i personally don't like this 'hidden features' in scratch. a lot other 'features' like this are the order of broadcasting (check the wiki) are pretty confusing, nontrivial, and not very beginner friendly.
well, my opinion  hmm

Offline

 

Board footer