简体   繁体   中英

Confusing naming convention for package in scala

I'm learning to scala and I'm seeing official document for style guide for scala. But I am confused why same expression is encourage and discourage at the same time.
According to scala style guide first package coolness is wrong but the second package coolness which is in middle of example, is right.

// wrong! this is definitely wrong
package coolness

// right! puts only coolness._ in scope
package com.novell.coolness

// right! puts both novell._ and coolness._ in scope
package com.novell
package coolness //but why is it OK?

// right, for package object com.novell.coolness
package com.novell
/**
 * Provides classes related to coolness
 */
package object coolness {
}

Solved : I didn't know that package declaration that over 2lines is same as one line declaration separate with dot.

Speaking of, package com.novell.coolness is same package com.novell; package coolness package com.novell; package coolness

It says right above the code you quoted that package names should follow the Java package naming conventions. The Java package naming conventions state that package names should be globally unique, and that this is achieved by using an Internet domain name that is under the control of the package author as the prefix for the package name.

For example, if you control the domain example.com , then you can use any package name like com.example._ for your packages, but you can not use, for example, com.microsoft.coolness .

So, com.novell.coolness is okay, because it follows the above rule (assuming you are Ray Noorda), but coolness is not, because it doesn't follow that rule (or, if it were following that rule, then it would imply that you are under the control of the . root domain namespace, which is almost certainly not true, unless you are ICANN).

This is really not a question about Scala at all, this is a question about Java package naming conventions, which the Scala style guide suggests you follow.

I just want to add a little something about the rationale. Everything Jorg said is correct: the following two declare the same package

package com.novell.coolness

package com.novell
package coolness

But there is an advantage to the latter. Let's say that my project structure has these three packages

com.novell.coolness
com.novell.morecoolness
com.novell.utilities.coolness

Now, I'm writing a file in com.novell.coolness that needs to use some stuff from these other two packages. If we use the first convention, we get

package com.novell.coolness

import com.novell.morecoolness.MyClass
import com.novell.utilities.coolness.MyOtherClass

However, if we use the second convention, we get

package com.novell
package coolness

import morecoolness.MyClass
import utilities.coolness.MyOtherClass

So com.novell is actually in scope and we can import relative to it. This can be quite handy if you have several imports that are from your own project but reside in different packages, as it eliminates the com.novell in every import.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM