ccrma / chuck

ChucK Music Programming Language
http://chuck.stanford.edu/
GNU General Public License v2.0
799 stars 127 forks source link

the time is now: fix class static variables #424

Open gewang opened 7 months ago

gewang commented 7 months ago
class Foo
{
    5 => static int x;
    static SinOsc y;
    new SndBuf @=> SndBuf @ z;
}

possible semantics and process: 1) identify lines with static variables 2) disallow both static and non-static variables in the same line 3) disallow time-advance (could be a compiler error or a runtime exception for immediate mode violation) (e.g., 1::second => static dur a => now;) 4) run the static lines a) at compile-time if public class OR b) at run-time if non-public class

int x;

fun int bar()
{
    return x+5;
}

class Foo
{
    bar() => static int z;
}
gewang commented 4 months ago

more thinking from PR Lab hackathon edition @nshaheed @AndrewAday @gewang

  1. disallow time via run-time time police
  2. possibly disallow all function calls (except for constructors of static variable being declared
  3. OR use dependency system to sort it out
    
    class X
    {
    fun static int go() { <<< "go" >>>; return 1; }
    }

X x;

class Y { fun Y(float val) { X.go(); // this should be okay x.go(); // this would not be okay, due to dependency <<< val >>>; } }

class Z { fun int nooooo() { return 2; }

// this probably can't work, as static init happens
// after compile but before run code
x.go() => static int z;

// calling a member function is not allowed
nooooo() => static int a;

// this is okay because it would just be part of pre-ctor    
nooooo() => a;

// constructor
// could throw runtime exception if time-advance detect
// compile time if dependencies don't work out
// this line needs to be marked as being run before this file
// need to generate a VM-code-block to run static stuff immediate
// after compile, before file code is run
static Y why(440);

}

nshaheed commented 4 months ago

Java allows static function calls and constructors in static variable declarations, but not member functions. Static variables can also be modified in static functions,

public class MyClass {
    static int p = 0;
    static MyClass fajioe = new MyClass();
    public static void main(String args[]) {
      int x=10;
      int y=25;
      int z=x+y;

      System.out.println("Sum of x+y = " + z);
    }

    public static int test() {

        System.out.println("jfioesjfioe");
        System.out.println(p);
        return 11;
    }

    public static int foo = test();

    public MyClass() {
        System.out.println("ssssss");
        p = 4;
    }
}
gewang commented 4 months ago

for class static UGen connect or constructors in general, detect for time-advance and throw exception if detected

class DEELAY extends UGen
{
    fun DEELAY()
    {
        2::second => now;
        <<< "constructor" >>>;
    }
}

DEELAY dl => dac;

<<< "after connect" >>>;