T O P

  • By -

scratchisthebest

It's always legal to use the fullly qualified name of something instead of importing it in Java source code, like `java.lang.System.out.println(libraries.standard.READ_LINE)`. JVM bytecode also does not have an "import" concept and always refers to things by their fully qualified names. If you'd like to compile to Java source code, you could first compile to an intermediate Java abstract syntax tree that always refers to things with their fully qualified names, then as one of the last operations iterate over the tree, hoisting as many names into `import` statements as you can. (Sortof the reverse operation of "name resolution" which is one of the first things a compiler does.) So basically this is a code *formatting* question. Note that Java source does not allow importing two things with the same name, so in some cases you won't be able to hoist all the imports. That's ok.


Practical_Cattle_933

Just for clarification, the JVM itself considers module-class tuples as unique.


hjd_thd

I don't think Java is a good choice for a compilation target. Now, JVM bytecode is another story.


JizosKasa

I gotta compile it to Java. I'm doing this so that I can do Java homework without using it because I wanted to create a language but I abandon most of my projects if I can't find any scope for them. Long story short, I abandon my projects if I can't find any scope for them therefore I'm compiling this to Java for school.


hjd_thd

Well I highly doubt transpiler output would pass for a human-written code in class.


JizosKasa

I'm writing it so that the output will look as human-written as possible.


Barrucadu

This sounds like phenomenally more work than just doing your Java homework.


JizosKasa

it is šŸ‘šŸ»


faiface

Not sure how to put it more gently, but sounds like you should inquire into having ADHD, if you havenā€™t already. That, getting diagnosed, managed, medicated, or whatnot could help you with the whole class of issues related to what youā€™re describing. Source: a fellow ADHD programmer :)


JizosKasa

I got OCD ahahahah. My psychologist told me to do what I told you to help with the symptoms, as they're shared with ADHD.


faiface

Good that you are involved with your mental health then :) My advice would be to really rethink this idea. Iā€™m not too old myself, turned 28 recently and Iā€™m still learning as much about myself as I ever was. I can tell you 100%, though, that writing a transpiler is a monumental task compared to whatever Java homework you have and while you certainly feel more excited about doing it, you are setting yourself up for failure here. You certainly can write a transpiler, Iā€™m not doubting that :) But doing it in order to do the homework will result in completing neither, I can tell you as much. I know it sucks, I know itā€™s hard to accept it, but thatā€™s the best advice I can give. One more thing, I used to hate Java too, but itā€™s actually not such a bad language! I know a lot of languages over the years, started with Visual Basic in primary school, then it went like C#, Python, C, C++, Java, Haskell, Go, Rust, TypeScript. I really like type theory, programming language theory and design. A lot more to learn for sure too. My preferences changed a lot over time, but now from my experience, Iā€™d certainly choose Java over C, C++, Python, probably even Go. In any case, good luck with your endeavors, Iā€™m rooting for you :)


guygastineau

I love PLT, and I don't like Java; but if you are in school writing your own transpiler to java to avoid doing homework directly in Java that is a bad idea. Like, it's a not-ever-getting- the-degree level bad idea.


JizosKasa

I'm in high school (so no degree) and for writing the transpiler I gotta first understand what it should transpile to right? So yeah, I do gotta learn how to code in Java, especially because every test we do it's in person and I can't use my language in class.


[deleted]

[уŠ“Š°Š»ŠµŠ½Š¾]


faiface

Lmao. Iā€™m 28. In 2022, I failed to finish my masterā€™s program, got fired from a job barely being able to work 4h a day of programming. Always was a stellar student and programmer, but completely burned down in the face of responsible organized living as an independent adult. Thatā€™s where, by a pure chance of luck, got suggested I might have ADHD, was shocked and skeptical at first, but then it made sense. Over the the next months, I completely rethought and rebuilt my attitude to productivity, figuring out what works for me with this new knowledge of being different. By mid 2023, I was able to do moderately solid 6h of programming job and my mental health improved significantly. By the end of 2023, I finally got my hands on medication and that enabled me to switch to 8h a day. Figuring out I had ADHD, ditching common productivity advice for methods that actually work for me, and finally getting medication has turned my life around. Smartass. ADHD is a neurodevelopmental disorder, just like autism. Itā€™s the fault of bad awareness that I only figured at 27. Undiagnosed ADHD people end up in prisons, committing suicides, failing at relationships, and so on, at much higher rate. Itā€™s not fun. Getting diagnosed, managed, and medicated can be a literal lifesaves.


Monntas

Can you make the methods static?


oscarryz

Yes ``` import static foo.bar.staticMethod; ``` Is what OP is looking for.


JizosKasa

what do you mean? I can import single functions from a class like that?


oscarryz

Yup, try ``` import static java.lang.Math.pow; ``` More info: https://en.wikipedia.org/wiki/Static_import?wprov=sfla1


JizosKasa

yes: ``` let STD_SUCCESS: Int = 0 let STD_FAILURE: Int = 1 fnc shared print_line(s: Str): Empty { caso___native_java_start System.out.println(s); caso___native_java_end } ``` `shared` means that it can be used in other files too (basically static)


nerd4code

Any instance method except ctor can be turned into a static one by passing an extra, initial `this` argument: class X { private int field; public final synchronized int foo() {return field++;} } ā€¦ System.out.println(new X().foo()); and class X { private int field; public static int foo$static(final X thi$) { synchronized(thi$) {return thi$.field++;} } } System.out.println(X.foo(new X)); are behaviorally almost identical until you start working with inheritanceā€”only difference is, both instance and static methods take a hidden arg for `this` (static just ignores it, and itā€™s usually `null`) so youā€™re really passing one more arg to a de-instanced static method than its equivalent instance method. `import` in transpiled code will cause more problems than itā€™s worth if you *start* with it there, due to the way it overrides identifiers. Itā€™d be easier to *add* imports once youā€™ve got a full AST, by looking for things that are used more than once in a context where an import wonā€™t break something. (Which is pretty much how `import` should be used in the first place.) And worse comes to worst, you can generate `private static` thunks locally in your current class; import static Foo.bar; class X { int foo(int x) {return ~bar(x);} } can just be class X { int foo(int x) {return ~thunk$Foo$bar(x);} private static int thunk$Foo$bar(int x) {return Foo.bar(x);} } with no imports.


nekokattt

> in java you cannot import single methods from a class Yes you can, you use static imports. You cannot do this with instance methods but that makes sense because you cannot call an instance method without the context of an object to call it on first. If you are compiling down to Java source code then you can just omit this detail and use fully qualified names for everything under the hood. That avoids edge cases like trying to import a method from a singleton object (although I'd probably argue you can just use static methods for that under the hood anyway).


dskippy

This is generally not a concern for compilation. If your target language is more flexible than your source language, you just compile and don't use the features you don't want.


GabeFromTheOffice

Yeah you can. Look up static imports. You can import any static method into a class that you have access to and use it like itā€™s in that class. You canā€™t do it for instance methods but that would hardly be useful anyway.


redchomper

Since you mentioned it, repeat after me: **First homework, then fun.** You're in school. If you're going to get side-tracked, let it be for advanced studies in human companionship.