gosukiwi / Blueberry

A beautiful programming language with clean syntax which compiles to PHP
MIT License
63 stars 10 forks source link

Static members for classes aren't supported #30

Closed hikari-no-yume closed 9 years ago

hikari-no-yume commented 10 years ago

Static properties are accessed in PHP like this: FooBar::$bar, while constants work like FooBar::FOO_BAR and methods like FooBar::foo().

One solution for this is uppercase constants and adding :::

class FooBar:
    @FOO_BAR = 3 # const FOO_BAR = 3;
    static @foo = 7 # static public $foo = 3;
    @bar = 4 # public $bar = 4;
    static def foo()
        @bar = @FOO_BAR # $this->bar = self::FOO_BAR;
        @foo = @bar * 2 # self::foo = $this->bar * 2;

But making references implicitly static would cause problems, and the static keyword isn't so nice. How about @@ for self::?

class FooBar:
    @@FOO_BAR = 3 # const FOO_BAR = 3;
    @@foo = 7 # static public $foo = 3;
    @bar = 4 # public $bar = 4;
    def foo() # public function foo() {
        @bar = @@FOO_BAR # $this->bar = self::FOO_BAR;
        @@foo = @bar * 2 # self::foo = $this->bar * 2;
    def @@bar() # static public function bar() {
        # stuff

@@ is a bit weird, though.

We could also choose not to support static members. Or we could take this approach:

class FooBar:
    static FOO_BAR = 3 # const FOO_BAR = 3;
    static foo = 7 # static public $foo = 3;
    @bar = 4 # public $bar = 4;
    static def foo()
        @bar = self::FOO_BAR # $this->bar = self::FOO_BAR;
        self::foo = @bar * 2 # self::foo = $this->bar * 2;

What to do?

gosukiwi commented 9 years ago

Uhm I think static might be easier to see. @@ can get messy pretty quickly. Maybe something like this:

class Foo
  def instance_method
  end

  static def static_method
  end
end

a = new Foo
a.instance_method
Foo::static_method
hikari-no-yume commented 9 years ago

Yeah, I'm really not sure on the syntax

gosukiwi commented 9 years ago

It's hard to decide! I think I'll implement closures for now. Give this some thought. Same with modules.

gosukiwi commented 9 years ago

Well, now that the compiler is smarter, whenever there's a method call and there's a variable in scope we can assume it's an instance, otherwise it's a class. So:

a = new MyClass()
a.method_a()
MyClass.method_b()

Would compile to

$a = new MyClass();
$a->method_a();
MyClass::method_b();
hikari-no-yume commented 9 years ago

That might work, though it seems error-prone

gosukiwi commented 9 years ago

Uhm, can you give an example? I still haven't decided on the syntax but if we could get the last I showed to work it would certainly be awesome.

hikari-no-yume commented 9 years ago

Actually, I can't think of a case where it'd break... that should work.

Though maybe you should only assume things in CamelCase are classes (much like how Haskell requires Types To Look Like This but variables look like this)

gosukiwi commented 9 years ago

Implemented in 72e7528c177ea13f1123eb6bc1840e17ea5ad39e, closing for now.