Code Monkey home page Code Monkey logo

Comments (7)

gallandarakhneorg avatar gallandarakhneorg commented on June 20, 2024

If the proposed syntax is allowed to return a value from a function, the compiler must avoid local definition of variables of the same name.

from sarl.

gallandarakhneorg avatar gallandarakhneorg commented on June 20, 2024

The work on this issue should be done at the same time as the one for the issue #55.

from sarl.

gallandarakhneorg avatar gallandarakhneorg commented on June 20, 2024

We may introduce invariant conditions also:

agent A1 {
    var int i
    with invariant: i>0
}

from sarl.

gallandarakhneorg avatar gallandarakhneorg commented on June 20, 2024

Currently, the with keyword is already defined. It is used for specifying generic types:

def push(s: A, i: B) with A extends Type1, B extends Type2 {
}

Currently, the generic type definitions could be assimilated to invariants.

from sarl.

gallandarakhneorg avatar gallandarakhneorg commented on June 20, 2024

Considering the precondition example given by @ngaud. It could be written with the current SARL syntax:

def push(s: String, i: Integer) : String with !Strings::isEmpty(s), i>0 {
   // The code
  null
}

Of course, the SARL compiler must be updated for detecting XExpression associated to the with keyword, and for generating the Java code that corresponds to the pre-condition tests.
I propose to generate the following Java code:

public String push(final String s, final Integer i) {
   assert !Strings.isEmpty(s) : "Invalid pre-condition \"!Strings::isEmpty(s)\"'";
   assert i > 0 : "Invalid pre-condition \"i>0\"'";
   // The code
  return null;
}

Regarding the postcondition, we may define a keyword "post ".

def push(s: String, i: Integer) : String with post Strings::isEmpty(it) {
   // The code
}

The it keyword will be mapped to the returned value in the context of the postcondition.
The generated Java code may be:

public String push(final String s, final Integer i) {
  String $$returnedValue;
   try {
       // The code
       $$returnedValue = null;
      return $$returnedValue;
   } finally {
        final String it = $$returnedValue;
        assert Strings::isEmpty(it) : "Invalid postcondition \"Strings::isEmpty(it)\"";
   }
}

from sarl.

gallandarakhneorg avatar gallandarakhneorg commented on June 20, 2024

Pre and post conditions should also be used for static validation by the SARL compiler.

from sarl.

gallandarakhneorg avatar gallandarakhneorg commented on June 20, 2024

This issue in now part of the PhD thesis of @tpiotrow.

from sarl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.