eo icon indicating copy to clipboard operation
eo copied to clipboard

Java objects inside EO code

Open yegor256 opened this issue 3 years ago • 9 comments

How about we create a new EO atom, which will represent Java objects. For example:

+alias org.eolang.java

[] > main
  stdout
    sprintf
      "Today is %s"
      (java.new "java.util.Date").toString

Here, java.new will make a new Java object java.util.Date and wrap it the right way, so that it becomes EO object.

We can also make it possible to make static calls:

+alias org.eolang.java

[] > main
  stdout
    sprintf
      "Now is %d msec"
      java.static "java.lang.System" "currentTimeMillis"

yegor256 avatar Feb 08 '21 13:02 yegor256

@yegor256 Ready to take this.

andreoss avatar Dec 19 '21 05:12 andreoss

@andreoss go ahead. But I suggest you start slowly, it's a complex task and it would be great to discuss design decisions before implementing them.

yegor256 avatar Dec 19 '21 16:12 yegor256

@yegor256 Instead of dividing methods into static or instance, we could treat new as a method.

((java "String").call "new" 65-66-67).as-string
(((java "Random").call "new).call "nextBoolean").as-bool

Same would go for fields,

((java "Integer").field "MAX_VALUE").as-int # Static field
((java "byte[]").new 1 2 3).field "length))  # Instance field

Over all the object would look like this

# Java interop.
[class] > java
    [method args..] > call  /java
    [name] > field /java
    [] > as-int    /int
    [] > as-float  /float
    [] > as-bool   /bool
    [] > as-string /string

could be more readble, if instead of quoted string, some symbol type would be used.

((java :System).call :currentMillis).as-int
((java :String).call :new 65-66-67).as-string
(((java :Random).call :new).call :nextBoolean).as-bool

andreoss avatar Dec 29 '21 11:12 andreoss

@andreoss I think would be convenient if we make EO objects already with "methods" as in Java:

(java "Random").new.nextBoolean.as-bool

Isn't it possible?

yegor256 avatar Dec 30 '21 04:12 yegor256

@yegor256 Should there be some mechanism which intersects calls to missing attributes? Like method_missing

[] > foo
   [attr] > missing

foo.bar # calls `foo.missing "bar"` '

andreoss avatar Dec 31 '21 17:12 andreoss

@andreoss but you know what attributes are there in Java, you have this information when you construct an object at new.φ:

@XmirObject(oname = "new")
public class EOnew extends PhDefault {
    public EOnew(final Phi sigma) {
        super(sigma);
        this.add("φ", new AtComposite(this, rho -> {
          // Here you know what is in Java and can build the object
          // exactly as it should be, in EO
        }
    }
}

yegor256 avatar Jan 01 '22 08:01 yegor256

@yegor256 Is it still in scope?

andreoss avatar Aug 29 '22 23:08 andreoss

@yegor256 @Graur I want to continue to work on this issue, any objections?

andreoss avatar Sep 03 '22 01:09 andreoss

@andreoss sure, but 1) we should move this feature to a separate package/repository, I believe, and 2) how will it work if EO is compiled to Rust and then to binaries, for example?

yegor256 avatar Sep 03 '22 03:09 yegor256