inmanta / inmanta-core

Inmanta is an automation and orchestration tool
https://inmanta.com
Apache License 2.0
27 stars 7 forks source link

language feature: native methods #7115

Open sanderr opened 5 months ago

sanderr commented 5 months ago

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

wouterdb commented 5 months ago

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 commented 5 months ago

Interesting suggestion, definitely worth consideration.

bartv commented 5 months ago

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: