Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snippets which include @main #879

Open
adamw opened this issue Jul 5, 2024 · 11 comments
Open

Snippets which include @main #879

adamw opened this issue Jul 5, 2024 · 11 comments

Comments

@adamw
Copy link

adamw commented Jul 5, 2024

Currently, such snippets cause the following error:

error: x.md:51:1:
method xyz cannot be a main method since it cannot be accessed statically
@main def xyz(): Unit =
^^^^^

maybe it would make sense to include an option to filter out @main from the snippets text, so that example code could still be verified, but also copy-pasteable for anybody wanting to try on their own?

@tgodzik
Copy link
Contributor

tgodzik commented Jul 5, 2024

You should be able to use https://scalameta.org/mdoc/docs/modifiers.html#reset-object so that the code is inside an object. I don't think we should filter anything as it might become a complex heuristic easily

@adamw
Copy link
Author

adamw commented Jul 5, 2024

Awesome! This works - thanks :)

@adamw adamw closed this as completed Jul 5, 2024
@adamw
Copy link
Author

adamw commented Jul 5, 2024

Well, almost ;) If I have multiple mdoc:reset-object:silent clauses, I get:

error: 01_hello_world.md:80:1:
helloWorldTapir is already defined as class helloWorldTapir in 01_hello_world.md
@main def helloWorldTapir(): Unit =
^^^^^

It works with a single snippet.

@adamw adamw reopened this Jul 5, 2024
@tgodzik
Copy link
Contributor

tgodzik commented Jul 5, 2024

Does it work with reset also? I think ideally resetObject should always be a different scope. If that doesn't work the only other workaround for now would be changing the main's name.

@adamw
Copy link
Author

adamw commented Jul 5, 2024

no, reset fails with the original error message. Shouldn't resetObject generate a new "fresh" name every time?

@tgodzik
Copy link
Contributor

tgodzik commented Jul 5, 2024

no, reset fails with the original error message. Shouldn't resetObject generate a new "fresh" name every time?

I think so, though I wonder if that doesn't play nice with @main annotation

@adamw
Copy link
Author

adamw commented Jul 5, 2024

ah, might be ... the compiler might be generating the top-level method then

@tgodzik
Copy link
Contributor

tgodzik commented Jul 5, 2024

Looks like it:

image

It can't even be in the same package, so would need to be separate compilation unit altogether. I don't think we want to work around that problem. Filtering @main might work, but on the other hand we should not modify the code, this can be misleading

@tgodzik
Copy link
Contributor

tgodzik commented Jul 5, 2024

Or we would need a resetPackage option:

image

@adamw
Copy link
Author

adamw commented Jul 5, 2024

ah yes, a :resetPackage:resetObject:silent could work :)

@tgodzik
Copy link
Contributor

tgodzik commented Jul 5, 2024

For now my recommendation would be to just name it differently. If anyone want to take a look I did a quick stab at it (https://github.com/tgodzik/mdoc/tree/reset-package) , but then forgot I need more than just a package, so we would need to make sure packages are sensibly handled if defined and even if not defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants