Author |
Message |
sjef
Joined: Jan 22, 2007 Posts: 6 Location: Netherlands
|
Posted: Wed Jan 24, 2007 4:57 am Post subject:
Question about Language Syntax |
 |
|
Hi all,
So I've started programming chuck, and found myself typing 1::second, and such a lot. This double colon makes me wonder because I can not find anything mentioned about it in the manuals, or anywhere else. It also doesn't seem to be a dereference operator like in C++
It seems to me I can think of it as: 1::second meaning 1 'of type' second. Further, like in example otf_01.ck:
1::T => now;
I would read as '1 of type T' is chucked to 'now'. So time advances by 1 T.. Wouldn't T => now be equivalent? I'm already confusing the type and value of a variable here, I notice. Please enlighten me.
Regards,
Sjef. |
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Wed Jan 24, 2007 11:51 am Post subject:
Re: Question about Language Syntax |
 |
|
sjef wrote: | Wouldn't T => now be equivalent? |
Yes, it would. More importantly; it is.
is perfectly fine ChucK syntax. So is this;
Code: |
//define a new unit of time
28::day => dur moon;
//notice I'm not using a seperate number for the amount of moons
moon => now; |
The type here is "duration". We have strings, floats, ints, arrays, classes, etc like any other language but we also have our very own type "duration". You can treat durations just like you would expect. For example you can devide them;
Code: | (hour / minute) => float This_should_be_sixty;
|
Is perfectly valid (note that a duration devided by a duration wil yield a float, just like 4 meters devided by 2 meter wil give "2" (without unit) while two meters devided by two will give "two meters".)
Actually if you are uncomfortable with
Code: |
//this is convention
2::second => now;
|
You can also use straight multiplication like this
Code: | //nobody does this but I just checked that it works
2 * second => now; |
Also usefull is this;
Code: | //samples per second
second / samp => float sample_rate; |
Those two double dots are just a syntactic convention. It's slightly more compact then writing it as a multiplication and I use it too but if you don't like it I can see no harm in always using the multiplication.
I asure you; nothing more complicated then the first lessons in highschool physics covering how we deal with units is going on, nothing up our sleeves.
 _________________ Kassen |
|
Back to top
|
|
 |
kijjaz

Joined: Sep 20, 2004 Posts: 765 Location: bangkok, thailand
Audio files: 4
|
Posted: Thu Jan 25, 2007 11:54 am Post subject:
|
 |
|
Kassen, this is very nice & complete example about time and :: indeed. cool!
 |
|
Back to top
|
|
 |
|