cross-referenced classes?
Assume I have the following: ===== file: scratch.ck ===== public class Scratch { Sniff _sniff; } ===== EOF ===== ===== file: sniff.ck ===== public class Sniff { Scratch @ _scratch; } ===== EOF ===== How does one compile cross-referenced classes? Neither "chuck scratch.ck sniff.ck" nor "chuck sniff.ck scratch.ck" bring joy. (There's a similar question for any class that has a reference to itself as a member, such as a linked list. But I'll save that question for the next time I need to flaunt my ignorance.) - Rob
2009/3/12 Robert Poor
Assume I have the following:
===== file: scratch.ck ===== public class Scratch { Sniff _sniff; } ===== EOF =====
===== file: sniff.ck ===== public class Sniff { Scratch @ _scratch; } ===== EOF =====
How does one compile cross-referenced classes? Neither "chuck scratch.ck sniff.ck" nor "chuck sniff.ck scratch.ck" bring joy. (There's a similar question for any class that has a reference to itself as a member, such as a linked list.
Why do you keep trying to do this to poor ChucK?? -- Tom Lieber http://AllTom.com/
The best solution I've come up with is to put Scratch and Sniff in the same file. It works, but the ghosts of abstraction are whispering profanities in my ear. On 12 Mar 2009, at 20:37, Tom Lieber wrote:
2009/3/12 Robert Poor
: Assume I have the following:
===== file: scratch.ck ===== public class Scratch { Sniff _sniff; } ===== EOF =====
===== file: sniff.ck ===== public class Sniff { Scratch @ _scratch; } ===== EOF =====
How does one compile cross-referenced classes? Neither "chuck scratch.ck sniff.ck" nor "chuck sniff.ck scratch.ck" bring joy. (There's a similar question for any class that has a reference to itself as a member, such as a linked list.
Why do you keep trying to do this to poor ChucK??
-- Tom Lieber http://AllTom.com/ _______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Robert Poor wrote:
How does one compile cross-referenced classes? Neither "chuck scratch.ck sniff.ck" nor "chuck sniff.ck scratch.ck" bring joy.
Did you try Machine.add()? atte@vestbjerg:~/music/chuck/modlys$ more lib_include_all.ck //Machine.add("../lib_sampler.ck"); Machine.add("../lib_sampler_nu.ck"); Machine.add("../lib_midiout.ck"); Machine.add("../lib_livekeyboardevent.ck"); Machine.add("../lib_time_event.ck"); Machine.add("../lib_time.ck"); Machine.add("../lib_global.ck"); Machine.add("../lib_mixer.ck"); Machine.add("../lib_sporker.ck"); Machine.add("../lib_instruments.ck"); //Machine.add("../lib_keyboard_event.ck"); Machine.add("../lib_interface.ck"); Machine.add("../lib_midiin.ck"); Machine.add("../click.ck"); Machine.add("../click_send.ck"); Machine.add("load.ck"); -- Atte http://atte.dk http://modlys.dk
Rob;
How does one compile cross-referenced classes?
Wait. Let me get this straight; you want a "scratch" to contain a "sniff" and a "sniff" should in turn contain a "scratch"? Yet that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and this will go on forever? Is that correct? I think I'm going to have to agree with Tom here; you shouldn't do that. Let's take a step back and look at what you are actually trying to accomplish, practically speaking, and see how we could go about that. Yours, Kas.
Hi Kas: My fault for not pointing this out: one contained a reference to the other, not an instance. But even when both are references, ChucK still won't compile it. Imagine you had a "controller" object that controls an "engine" object, but the engine needs a reference back to the controller, e.g. to query its state. This is a standard programming pattern. Here's how I would express it (and lose): ================= [file: controller.ck] public class Controller { Engine @ engine; } [file: engine.ck] public class Engine { Controller @ controller; } % chuck controller.ck engine.ck [controller.ck]:line(2): undefined type 'Engine' ================= There must be some ChucK-ish idiom that I don't yet understand. - Rob On 13 Mar 2009, at 04:35, Kassen wrote:
Rob;
How does one compile cross-referenced classes?
Wait. Let me get this straight; you want a "scratch" to contain a "sniff" and a "sniff" should in turn contain a "scratch"? Yet that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and that "scratch" will contain another "sniff" and that "sniff" will contain a "scratch" and this will go on forever? Is that correct?
I think I'm going to have to agree with Tom here; you shouldn't do that.
Let's take a step back and look at what you are actually trying to accomplish, practically speaking, and see how we could go about that.
Yours, Kas.
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
2009/3/13 Robert Poor
Hi Kas:
My fault for not pointing this out: one contained a reference to the other, not an instance.
You are saying it merely contains a reference to the class itself, names that and doesn't actually instantiate it? This would be useful for public classes with static data? I didn't know you could do that but I suppose it could make sense.
But even when both are references, ChucK still won't compile it.
I think it's quite clear why not; both files depend on the other file to be useful; basically both need to be run first yet in practice ChucK can only do one thing at a time. I fear this simply won't fly. Either the chicken or the egg will need to come first.
Imagine you had a "controller" object that controls an "engine" object, but the engine needs a reference back to the controller, e.g. to query its state. This is a standard programming pattern.
Ah, ok. Erm.... maybe define a class "Foo" that has both the controling and querying functions (at least functions of that type and name) and extend it twice, once for the engine and once for the controller? That way you could run the file containing the Foo class and refer to that type in the engine and controller code, you'd then instantiate the engine and controller and asign the engine to the Foo in the controller and the controller to the Foo in the engine from a fourth file. That way the classes can be defined in any order as long as Foo goes first and we'd instantiate and asign after that. I think that might work... Not especially pleasant, I admit, and you better not need arrays or there will be "Clemow's bane" type trouble. Sigh, this is now looking quite tricky. Yours, Kas.
Here we go; this works; the controller sends commands to the engine, the
engine executes those but depends on the state of the controller for the
final result. there is a issue where the classes need to be defined above
the actual code. This is no real issue when working with multiple files and
public classes but I still think it's against the language specs. This is
probably another example of the bug that creates a need for classes that
extend Event to be defined above the spot where we use broadcast or signal
on them.
Cheers,
Kas.
==========example code below================
//we need to define the classes at the top or the casting will fail
//I considder this to be a bug.
class Foo
{
fun void control( int message)
{
}
fun int querry ()
{
return false;
}
}
class Engine extends Foo
{
Foo controller;
fun void control ( int message)
{
<<
How absolute the knave is! We must speak by the card, or equivocation will undo us! Thanks. - Rob On 13 Mar 2009, at 09:01, Kassen wrote:
Here we go; this works; the controller sends commands to the engine, the engine executes those but depends on the state of the controller for the final result. there is a issue where the classes need to be defined above the actual code. This is no real issue when working with multiple files and public classes but I still think it's against the language specs. This is probably another example of the bug that creates a need for classes that extend Event to be defined above the spot where we use broadcast or signal on them.
Cheers, Kas.
==========example code below================
//we need to define the classes at the top or the casting will fail //I considder this to be a bug.
class Foo { fun void control( int message) { }
fun int querry () { return false; } }
class Engine extends Foo { Foo controller;
fun void control ( int message) { <<
>>; } } class Controller extends Foo { Foo engine; int state;
spork ~ work();
fun int querry() { return state; }
fun void work() { while(1) { second => now; !state => state; engine.control( maybe ); } } }
Controller boss; Engine motor; boss $ Foo @=> motor.controller; motor $ Foo @=> boss.engine;
hour => now;
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Rob; How absolute the knave is! We must speak by the card, or equivocation will
undo us!
I don't know, equivocation may well still undo us...
Thanks.
That's ok, I had good fun. Cleaner would likely be defining a class
"proto_engine" and one "proto_controller", adding those, then extending
both, this avoids needing a rather clumsy class that has some members of
both Engine and Conttroller; that's bound to lead to confusion later on.
It turns out you are right, BTW and that you can create references to
classes without instantiating them, from there on you can only use static
members of those;
class Foo
{
static int bar;
fun static void printA() {<<<"bang">>>;}
fun void printB() {<<<"bang">>>;}
}
Foo @ baz;
<<
participants (4)
-
Atte André Jensen
-
Kassen
-
Robert Poor
-
Tom Lieber