SnipyJulmy / bc-truffle

bc (basic calculator) running on Truffle
Universal Permissive License v1.0
3 stars 0 forks source link

Minutes from meeting #1

Open maenu opened 5 years ago

maenu commented 5 years ago

TODO:

SnipyJulmy commented 5 years ago

I have solved the problem with the dependency cycley but not how we discuss it.

The point is that if we want to initialize the global static variable "parser" (of type IBcParser) in the BcLanguage class, we have to initialize this variable at runtime (when the object is created basically), but we can't create or access it since we don't know it exists.

To solve this, I use reflection to call the static method in a Init class on the scala side of the project with a static block initialization (see under src/main/scala/ch/snipy/bc/Init.java).

This way, the compiler doesn't know anything about the scala part and the parser is correctly initialized.

maenu commented 5 years ago

Is a class initializer not enough?

class Thing {
  static IParser parser;
  {
    parser = new Parser();
  }
}
boris-spas commented 5 years ago

No reflection please, it complicates things a lot (especially for native image) and it's not necessary. I'm looking at your code and I'm unclear why you have your scala object inherit from IBcParser ? I would expect the class to inherit from IBcParser and the object to have a line like BcLanguage.parser = new BcParser ();

This would mean that the BcLanguage.parser in initialized during static init and is available when needed. No reflection needed.

boris-spas commented 5 years ago

Why am I not a collaborator? I'm not getting notifications o_O

SnipyJulmy commented 5 years ago

The problem to do it the way Boris propose is that the BcLanguage.parser field is not initialized because the static assignment never occurs. That's from the behaviour of the Scala object and the static block in Java.

The static block and the object have to be accessed in order to execute the static part.

I think you did not accept the invitation as a collaborator :) I can resend it if you want.

Edit : the same problem occurs with Manuel solution too, if I have the code on the java part, it just don't see the scala code at compile time. If I have it on the scala part, the class is never "required" and the initialization would never occur.

boris-spas commented 5 years ago

accepted, thanks!

Now, I still don't get why that does not work. Are you saying that the static initializer equivalent in scala (the companion object constructor) does not get executed unless the class is referenced?

SnipyJulmy commented 5 years ago

Yes, that's the point. I have also try to put a Java class on the scala part with a static block, but it is the same problem, the static block is only executed when the class is initialized.

Reference : https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.7

boris-spas commented 5 years ago

That's unfortunate... can you create a global variable in scala which holds an instance of the class to force the init? #hacky

SnipyJulmy commented 5 years ago

The problem with this solution is the same, since the scala class is not referenced, the initialization would never occurs.

According to the documentation : https://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.2, I don't see how to do it (maybe I am just stupid...).

At this point, I think the best solution would be to stay for the moment with the package script (yeah it's ugly) and then I would rewrite the parser part in java, using another parser like ANTLR or whatever, if it is ok for you.

boris-spas commented 5 years ago

Unfortunate.... Thanks for trying.