eclipse-archived / ceylon

The Ceylon compiler, language module, and command line tools
http://ceylon-lang.org
Apache License 2.0
399 stars 62 forks source link

Module-wide default aliases for type imports #6296

Open lucono opened 8 years ago

lucono commented 8 years ago

When using annotations in Ceylon, I find that I'm aliasing certain imports (example) consistently using the same alias across many classes, modules, and throughout an entire project.

Some examples:

import javax.persistence { 
    id = id__FIELD, 
    generated = generatedValue__FIELD, 
    GenerationType { autoGen = AUTO }     // to avoid the noise of having GenerationType.\iAUTO
}
import org.springframework.beans.factory.annotation {
    autowiredConstructor = autowired__CONSTRUCTOR,
    autowiredField = autowired__FIELD
}

It would be helpful to have a way of providing a single, module-wide alias for cases like this in order to reduce the repetitiveness of doing it (identically) across many classes in a module. It would also help with consistency of the alias across the entire module or project.

The module-wide import wouldn't be importing the type into the module, but simply specifying a default alias to be used in Ceylon source files that don't explicitly alias the class to something different from its natural name. It would still be possible to use a different alias in imports within individual Ceylon source files when required, just as is currently possible.

ncorai commented 8 years ago

Scala has shown us to be wary of implicits. I vote against.

jvasileff commented 8 years ago

@ncorai I'm not sure I see the parallel here with implicits. There would be no magic, and adding an import would never change the behavior of code that compiles without the import.

I do think it would be very nice to have some solution for this problem. Sometimes when I'm working on code that requires a bunch of import aliases, it feels like I'm spending 1/2 my time managing them. And then, a badly timed 'cmd-shift-O' inevitably clears my hand-written but yet unused aliases!

ncorai commented 8 years ago

If you don't remember/are not aware that a given function or class is imported for the whole module and then assume you're using another (or another version of it), you may not realize your mistake until runtime.

I know it used to bite me when auto-importing Guava's collections (for instance, an old version of Google Collections is included in TestNG), until I configured Eclipse to avoid those.

lucono commented 8 years ago

If you don't remember/are not aware that a given function or class is imported for the whole module...

@ncorai It would not be an import for the whole module - it would not be an import at all.

It would be an _alias_ specified at the module level for the symbol, so that anywhere in the module where the _actual_ import was being made, the alias could then be used instead, in place of the symbol's regular name.

Example module-level alias using a fictional alias syntax (resembling regular import aliasing):

alias org.springframework.beans.factory.annotation {
    autowiredConstructor = autowired__CONSTRUCTOR
}

Then in some class:

// The following would correctly import
// org.springframework.beans.factory.annotation.autowired__CONSTRUCTOR
// class already aliased as "autowiredConstructor" without needing the
// "= autowired__CONSTRUCTOR" local aliasing:

import org.springframework.beans.factory.annotation {
    autowiredConstructor
}

// The following would also still work to import the same 
// symbol with its regular name:

import org.springframework.beans.factory.annotation {
    autowired__CONSTRUCTOR
}

// Even with the module-level alias, you'd still be able to provide a
// different alias at the site of each actual import:

import org.springframework.beans.factory.annotation {
    myDifferentAlias = autowired__CONSTRUCTOR
}

// And I imagine with the module-level alias, an import statement like the 
// following somewhere in the module could generate a quick fix suggestion 
// in the IDE to remove the unnecessary local aliasing:

import org.springframework.beans.factory.annotation {
    autowiredConstructor = autowired__CONSTRUCTOR
}

This makes it easy to consistently alias a symbol across an entire module, _without importing the symbol at all_, except for wherever in the module there exists an actual import statement for the symbol.

ncorai commented 8 years ago

Apologies, I had missed the last part of your suggestion. It's indeed much safer than I had understood and I have no objections to it.