Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

package member naming rules unclear? #473

Open
loldrup opened this issue Jun 13, 2017 · 6 comments
Open

package member naming rules unclear? #473

loldrup opened this issue Jun 13, 2017 · 6 comments

Comments

@loldrup
Copy link

loldrup commented Jun 13, 2017

On this page
https://ceylon-lang.org/documentation/1.3/tour/modules/

under the headline "Source files and packages"

it is said that:
"There's no package statement in Ceylon source files. The compiler determines the package and module to which a toplevel program element belongs by the location of the source file in which it is declared."

Then an (interesting!) example is given:
"For example, if source is a source directory, and if a class named Hello is defined in the file source/org/jboss/hello/Hello.ceylon, then Hello belongs to the package org.jboss.hello."

This example leaves one question open:
Why was the package member named Hello?
Was it because the file was named Hello.ceylon?
Or because a class in the file was named Hello?
And what happens if there is no correspondence between file name and class name?

Now that I'm thinking more about it, I can kinda figure out that it must be the file name that determines the name of the package member. But it would still be nice having it stated explicit :)

@bjansen
Copy link
Contributor

bjansen commented Jun 13, 2017

The file name does not affect declaration names. If a class named Hello is put in a file named World.ceylon, then the declaration name will be your.package::Hello.

To anwser your last sentence: package members are named after what's declared in the file, not after the file name. You can have multiple shared declarations in the same file, unlike in Java, that's why the file name is purely informal and is not used to determine member names.

@loldrup
Copy link
Author

loldrup commented Jun 13, 2017 via email

@lucaswerkmeister
Copy link
Contributor

I can now conclude that a class cannot be a 'program element' (unless classes can be embedded in other classes - then the embedded class could be a 'program element').

Classes can be embedded in other classes, just like other declarations. Ceylon allows you to embed class, interface or function declarations inside regular blocks and bodies.

I conclude this because top level classes in a source file becomes their own separate package, and the name of each of these classes becomes embedded in their respective package names.

No, classes don’t become a separate package. A class Hello defined in the file source/org/jboss/hello/_______.ceylon (where the ______ is arbitrary) belongs to the org.jboss.hello package. A toplevel function defined in the same file also belongs to the same package.

@loldrup
Copy link
Author

loldrup commented Jun 13, 2017 via email

@lucaswerkmeister
Copy link
Contributor

Change the unfortunate example where the file name confusingly overlaps with the class name.

I disagree that it’s an unfortunate example – it’s conventional in Ceylon to have classes in a file of their own, and to name the file after the class (i. e., put Hello in Hello.ceylon). I don’t like the idea of deviating from that convention just to drive home the fact that it’s a convention, not a requirement. I’d prefer to address only bullet point two, with something like:

Explicitly state that file names has no influence on package names.

Isn’t this what the next paragraph does?

Note that the name of the source file itself is not significant, as long as it has the extension .ceylon. It's only the directory in which the source file is located that matters to the compiler.

Explicitly state the syntax for import declarations.

How explicitly do you want it? A full grammar, in my opinion, belongs in the specification, not in the tour. On the other hand, the thrice repeated “we write” isn’t great either, and would perhaps be a good opportunity to explain the syntax non-formally: ”we write the package name and the imported declaration separately” (or something like that); “we list the program elements, separated by commas”; and “we use an ellipsis”, for example.

Adding an example for multiple imports would probably be a good idea too, so what if we added org.jboss.hello without removing com.redhat.polar.core?

import org.jboss.hello { Hello }
import com.redhat.polar.core { Polar }

@loldrup
Copy link
Author

loldrup commented Jun 14, 2017 via email

lucaswerkmeister added a commit that referenced this issue Jun 14, 2017
The introductory paragraph of the “Packages and imports” section dealt
mostly with packages, not with imports, and went into a lot of detail.
It has been updated to give a more high-level overview of both aspects,
and leave the detail to its subsections.

The introduction to packages did not make it clear that the file name
has no significance despite the convention to name files after their
content. It has been rewritten to make the relation between
declarations, source files, and packages more clear. The example has
been moved to a separate paragraph and expanded.

The section on imports now explains the import syntax a bit more.
The import examples have been expanded to include the example from the
previous section, partially merged, and comments have been added.

Fixes #473.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants