Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify method and field symbol spaces on objects #574

Closed
jclark opened this issue Jul 26, 2020 · 1 comment
Closed

Unify method and field symbol spaces on objects #574

jclark opened this issue Jul 26, 2020 · 1 comment
Assignees
Labels
enhancement Enhancement to language design implementation/inprogress Implementation inprogress incompatible Resolving issue may not be backwards compatible lang Relates to the Ballerina language specification

Comments

@jclark
Copy link
Collaborator

jclark commented Jul 26, 2020

Currently objects have separate symbol spaces for methods and fields.

AFAIR the logic for this was to make the field symbol space similar to records and not have it be polluted with double underscore methods. But

Overall it therefore seems better to have a unified symbol space.

With this, #98 is solved simply by allowing field access to be applied to a method: the result will be a closure of the function with self bound to the particular object.

var obj = object {
  int n = 0;
  function get() returns int { n += 1; return n; }
};

var f = obj.get;

Similarly, #257 is solved by making method call syntax when applied to a field of function type call the function.

@jclark jclark added enhancement Enhancement to language design lang Relates to the Ballerina language specification incompatible Resolving issue may not be backwards compatible labels Jul 26, 2020
@jclark jclark added this to the Swan Lake preview 2 milestone Jul 26, 2020
@jclark jclark self-assigned this Jul 26, 2020
@jclark
Copy link
Collaborator Author

jclark commented Jul 26, 2020

There are two possible concepts of a method call.

  1. A method is that it is a function with a special parameter. The method call syntax is way of providing this parameter.
  2. Initialization stores in the object a closure in which self is bound to the object being initialized. Method call is just a field access that retrieves the closure and calls it. From a type perspective, an object method is just a readonly field that contains a function. A method definition is special in a class in that it automatically takes care of initializing the field to a closure.

Concept 2 is attractive because it makes the solutions to #98 and #257 work automatically, without any special rules.

However the approach to isolated in #145 does not work with concept 2 without some additional complexity. We want isolated methods to have access to self in the same way as any other parameter, otherwise it becomes much more burdensome to write isolated methods than isolated functions. This has the following consequence (regardless of whether we use concept 1 or 2). Given:

var obj = object {
  int n = 0;
  isolated function get() returns int { n += 1; return n; }
};

var f = obj.get;

the bound method f is not isolated.

I think this means we do also need the concept of isolated objects as well as isolated functions. For example:

var obj = isolated object {
  private int n = 0;
  isolated function get() returns int { 
    lock {
      n += 1;
      return n; 
    }
  }
};

var f = obj.get;

This would make the type of the bound method f be isolated. We need to figure out exactly what the rules are for isolated objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement to language design implementation/inprogress Implementation inprogress incompatible Resolving issue may not be backwards compatible lang Relates to the Ballerina language specification
Projects
None yet
Development

No branches or pull requests

2 participants