Author |
Message |
djdaphalgan
Joined: Jul 19, 2007 Posts: 20 Location: Belgium
|
Posted: Fri Sep 07, 2007 3:03 am Post subject:
Upcasting of classes? |
|
|
Hi, is upcasting/downcasting of classes possible? Example: I make an array of Instruments, and I put a Moog, a Flute, ect. in there. Chuck will see an Instrument if I get an element out of this array, so I can only use the methods (functions) of Instrument. If I would like to use other methods (like the once of Moog), that I have to upcast my Instrument to a Moog object (don't know for sure if upcast/dawoncast is the correct term). Is this possible in chuck? |
|
Back to top
|
|
|
spencer
Joined: Aug 16, 2006 Posts: 53 Location: northern california
|
Posted: Thu Sep 13, 2007 12:08 am Post subject:
|
|
|
Hi djdaphalgan,
Yep--the $ cast operator works on objects as well as primitive types. E.g.
Code: | UGen ugens[10];
new SinOsc @=> ugens[0];
440 => ( ugens[0] $ SinOsc ).freq;
ugens[0] => dac;
1::day => now; | will do the right thing. But, uh, casting to an class that is not the object's class hierarchy can crash chuck.
spencer |
|
Back to top
|
|
|
djdaphalgan
Joined: Jul 19, 2007 Posts: 20 Location: Belgium
|
Posted: Thu Sep 13, 2007 12:26 am Post subject:
|
|
|
I did'nt not know the cast $ operator.
thanx for the info. |
|
Back to top
|
|
|
robin746
Joined: Sep 02, 2007 Posts: 26 Location: Eire
|
Posted: Wed Sep 19, 2007 4:58 am Post subject:
|
|
|
Can you explain, for us newbies, what that code is supposed to do that couldn't be done without the cast? |
|
Back to top
|
|
|
kijjaz
Joined: Sep 20, 2004 Posts: 765 Location: bangkok, thailand
Audio files: 4
|
Posted: Wed Sep 19, 2007 5:06 am Post subject:
|
|
|
This is something I haven't done before also..
but it's just cool.
I once tried putting different instances of UGens in an array of UGen,
i couldn't access each UGen's individual functions and value
(because it's refered to as only a UGen, not a particular kind of UGen)
so casting like this is just the right way to work out the problem.
thanks! .. I didn't think about this at all hahah ^_^ |
|
Back to top
|
|
|
robin746
Joined: Sep 02, 2007 Posts: 26 Location: Eire
|
Posted: Wed Sep 19, 2007 5:15 am Post subject:
|
|
|
OK, I think I follow that. In my dabbling I had a similar problem. Unfortunately this method still doesn't solve it because different Ugens have different methods that need to be used to, say, play a note.
If I could subclass Ugens then I could create my own PlayNote method for each, which does different things depending on the parent class. Then all I would need to do, no matter which Ugen, is say PlayNote.
This seems like obvious functionality to someone from Python, but I don't think it can be done at all in ChucK. |
|
Back to top
|
|
|
Kassen
Janitor
Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Wed Sep 19, 2007 6:55 am Post subject:
|
|
|
No, it would be good to be able to write our own proper Ugens (or extend existing ones) and it *is* obvious functionality.
ChucK isn't "finished" yet. Personally I hope it never will be, aside from the usefulness the development itself is exciting to me. _________________ Kassen |
|
Back to top
|
|
|
robin746
Joined: Sep 02, 2007 Posts: 26 Location: Eire
|
Posted: Wed Sep 19, 2007 6:59 am Post subject:
|
|
|
I am glad that "no" means we agree.
I still think maybe I'm missing things since I am so completely new to ChucK. I don't mean to be hammering on about things that are missing! |
|
Back to top
|
|
|
kijjaz
Joined: Sep 20, 2004 Posts: 765 Location: bangkok, thailand
Audio files: 4
|
Posted: Wed Sep 19, 2007 7:15 am Post subject:
|
|
|
Or maybe you can use another int array to store "UGen types" in the ugen array..
and use if statement to select the proper casting and note-playing method for each UGen in the UGen array.
I haven't tried this yet, but i'll work it out as one homework and post it,
if i've got more time this week ^_^ |
|
Back to top
|
|
|
robin746
Joined: Sep 02, 2007 Posts: 26 Location: Eire
|
Posted: Wed Sep 19, 2007 7:33 am Post subject:
|
|
|
kijjaz wrote: | Or maybe you can use another int array to store "UGen types" in the ugen array..
and use if statement to select the proper casting and note-playing method for each UGen in the UGen array. |
That gets messy since there is no case statement or equivalent. (Unless I've missed that too!) |
|
Back to top
|
|
|
Kassen
Janitor
Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Wed Sep 19, 2007 7:40 am Post subject:
|
|
|
robin746 wrote: | I am glad that "no" means we agree.
I still think maybe I'm missing things since I am so completely new to ChucK. I don't mean to be hammering on about things that are missing! |
Well, your statement was in a negative form so I thought that was the right way to agree. Maybe this was just me applying Dutch syntax to English, this happens.
The fun thing with a occasionally buggy and incomplete language is that hammering on things you are missing is often very constructive; I do it a lot too but only because Ge inexplicitly asked for it many times. I don't think anybody said "ChucK can be buggy and is incomplete" more times then Ge did.
As long as it's friendly hammering nobody will mind, hammering is cool, constructive hammering will build one a nice house. _________________ Kassen |
|
Back to top
|
|
|
|