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

language feature: native methods #7115

Open
sanderr opened this issue Feb 5, 2024 · 3 comments
Open

language feature: native methods #7115

sanderr opened this issue Feb 5, 2024 · 3 comments
Labels
compiler design Issues with a design aspect suggestion Suggest a change, open to discussion

Comments

@sanderr
Copy link
Contributor

sanderr commented Feb 5, 2024

Description

Add native methods to the language. They have a return value and can be called explicitly (unlike implementations).

entity Int:
    int n
    bool even = self.is_even()
end

method is_even: bool for Int:
    return self.n % 2 == 0  # 1 return statement per method, conventionally lexically last in the block but not strictly required
end

Open questions

  • I only have a vague recollection of wishing for something like this -> come up with a concrete use case. Perhaps only really useful in conjunction with language feature: protocols / traits #7116 and/or language feature: generics #7114.
  • syntax: declaration: return statement, return type annotation, ...
  • should methods be functional? i.e. side effect free with respect to the model? Makes promise tracking a lot easier
  • should we offer non-method functions?
  • should methods be able to have arguments?
  • where can methods be called? e.g. can they be called as a default in entity definitions?
  • Can methods be overridden? Can overriding method call parent?
@sanderr sanderr added compiler design Issues with a design aspect suggestion Suggest a change, open to discussion labels Feb 5, 2024
@wouterdb
Copy link
Contributor

wouterdb commented Feb 6, 2024

I think the bigger change in the example is having a default that is a full dyamic expression.

Everything else could be defined as syntactic sugar on top of existing things?

entity Method:

end

Method.self [1] -- SelfType

Method(self=myself)

implement Method using implementation_for_it

@sanderr
Copy link
Contributor Author

sanderr commented Feb 6, 2024

Interesting suggestion, definitely worth consideration.

@bartv
Copy link
Contributor

bartv commented Feb 7, 2024

I am not entirely sure yet how useful this is and why it is better than:

entity Int:
    int n
    bool even
end

implementation is_even for Int:
    self.even = self.n % 2 == 0 
end

However, there is a pattern that we use more often for which this is a start of an improvement. We often "abuse" entity to calculate certain values, often more than one. Something like:

entity Test:
  string a
  string b
  string c
  string d
  string e
end

implementation x for Test:
  self.d = ... # both use a, b, c to calculate d and e
  self.e = ...
end

In this case a, b and c are the argument and d and e are the return values. I see two main options for this:

  • have the ability to return entities for methods. This then also raises the question: do we need methods or wouldn't functions be much more useful. Something like a plugin call, but defined in the language
  • Introduce a concept of in and out parameters. It seems that some database have this in stored procedures and in the swift language of apple

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler design Issues with a design aspect suggestion Suggest a change, open to discussion
Projects
None yet
Development

No branches or pull requests

3 participants