Search code examples
ponylang

RAII constructor without arguments?


Why isn't the form Class? valid when the no args constructor can error, while with args form is valid? Could it be a bug or rough edge that hasn't been considered yet?

class A
  new create(x : I32) ? =>
    if (x % 2) == 0 then
      error
    end

class B
  new create() ? =>
    error
    
actor Main
  new create(env: Env) =>
    try
      let x = A(1)? // legal
      let y = B.create()? // why not `B?`? `B()?` seems to expand to `create().apply()?` 
    else
      env.out.print("exception")
    end

Solution

  • This appears to be a syntactic limitation in current Pony. For a total zero-argument constructor call, you would use the type name without parentheses, like this:

    let y = B
    

    For a partial call, you would expect you can use this (still without parentheses):

    let y = B?
    

    But this is currently not syntactically valid. It seems like a logical extension (assuming that it does not conflict with anything else syntactically).