chuck icon indicating copy to clipboard operation
chuck copied to clipboard

the time is now: fix class static variables

Open gewang opened this issue 2 years ago • 3 comments

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 avatar Jan 11 '24 23:01 gewang

more thinking from PR Lab hackathon edition @nshaheed @AndrewAday @gewang 5. disallow time via run-time time police 6. possibly disallow all function calls (except for constructors of static variable being declared 7. 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);
}

gewang avatar Apr 14 '24 21:04 gewang

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;
    }
}

nshaheed avatar Apr 14 '24 21:04 nshaheed

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" >>>;

gewang avatar Apr 14 '24 22:04 gewang

pretty much all of the design points @AndrewAday @nshaheed have been addressed and implemented in https://github.com/ccrma/chuck/commit/6b8acda214c0b0b4c0a3dcc8f3202515c58c6bac #2024-static-init (also #2024-Thanksgiving lol), and will be introduced the next chuck release (1.5.4.3)

gewang avatar Dec 01 '24 00:12 gewang