Author |
Message |
chuckles
Joined: Apr 02, 2007 Posts: 72 Location: San Diego, California
|
Posted: Mon Jun 04, 2007 9:53 am Post subject:
Latest mystery problem Subject description: Bug? |
|
|
OK, here's a simple demonstration of something that gives a compile error that I can't figure out. Mandolin and StifKarp are both StkInstruments, yes?
Then why do these statements compile OK:
Code: | [new StifKarp, new Mandolin ] @=> StkInstrument voice1;
[]; [new Mandolin, new StifKarp ] @=> StkInstrument voice2[]; |
but these give an error message
Code: | [new StifKarp, new StifKarp ] @=> StkInstrument voice3[];
[new Mandolin, new Mandolin ] @=> StkInstrument voice4[]; |
Code: | [unnamed2]:line(7): cannot assign '@=>' on types 'Mandolin[]' @=> 'StkInstrument[]'...
[unnamed2]:line(7): ...(reason: --- incompatible types for assignment) |
This can be extended too to more than two instruments. I found out about it by experimenting with the "In C" project where he defines something like 9 instruments which are a mixture of StifKarp and Mandolin. I wondered why he did that and it's apparently because you can't create an array of StkInstruments that are all the same type.
Comments, anyone?
Thanks!
c. |
|
Back to top
|
|
|
spencer
Joined: Aug 16, 2006 Posts: 53 Location: northern california
|
Posted: Mon Jun 04, 2007 4:45 pm Post subject:
|
|
|
Hey chuckles,
I think ChucK's implicit typing rules force a literal array of objects to be the most specific type/subclass possible while still having them be all the same type at some level. So Code: | [new StifKarp, new Mandolin ] @=> StkInstrument voice1; | is an array of StkInstruments, but Code: | [new StifKarp, new StifKarp ] @=> StkInstrument voice3[]; | is an array of StifKarps. Also, unfortunately, ChucK's typing rules don't allow you to cast an array of a class to an array of its superclass.
Anyways the solution is this: Code: | [new Mandolin $ StkInstrument, new Mandolin ] @=> StkInstrument voice4[]; | This forces ChucK to think that the most specific type that applies to all elements is StkInstrument, so it is an array of StkInstrument rather than Mandolin.
spencer |
|
Back to top
|
|
|
chuckles
Joined: Apr 02, 2007 Posts: 72 Location: San Diego, California
|
Posted: Tue Jun 05, 2007 8:26 am Post subject:
|
|
|
Thanks so much Kassan, that will make life easier for me, and it also educates me about $ casting...
(Another way to get around the problem, apparently, though not very elegant, would be to explicitly assign like:
Code: | StkInstrument voice4[2];
new StifKarp @=> voice4[0];
new Mandolin @=> voice4[1]; |
|
|
Back to top
|
|
|
Kassen
Janitor
Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Tue Jun 05, 2007 9:35 am Post subject:
|
|
|
chuckles wrote: | Thanks so much Kassan, |
I'm happy you are happy but (Spencer != Kassen), in fact I haven't yet had the pleasure of meeting Spencer in person.
There's more then one person trying to answer question in the ChucK-scene(and that's a good thing too!).
:¬) _________________ Kassen |
|
Back to top
|
|
|
chuckles
Joined: Apr 02, 2007 Posts: 72 Location: San Diego, California
|
Posted: Fri Jun 08, 2007 8:34 am Post subject:
|
|
|
My apologies to both Spencer and Kassen; Spencer for not thanking him for his clarification of my problem, and Kassen for spelling his name wrong! Thanks to both of you for helping out in this forum -- the forest of ChucK has some dark areas and mysterious paths for a beginner...
...c... |
|
Back to top
|
|
|
Kassen
Janitor
Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Fri Jun 08, 2007 9:23 am Post subject:
|
|
|
No harm done, Chuckles. I've had a unusual name for quite a while now and I've been dyslexic for a similar period so i can't blame anyone for spelling.
And, yes, it does, but I think that's half the fun. _________________ Kassen |
|
Back to top
|
|
|
|