Why Can’t You Import Packages?

Discussion in 'Java' started by Lawrence D'Oliveiro, Feb 18, 2011.

  1. Consider a piece of code which wants to use two different classes,
    com.example.android.sqlite.Useful and com.example.android.mysql.Useful. Your
    only reasonable options are:

    1) Choose one to specify in an import statement so you can use its name
    unqualified. The other class will have to be referred to by its fully
    qualified name
    2) Don’t bother importing either, use the full name for both.

    There is no way you can do something like

    import com.example.android.sqlite;
    import com.example.android.mysql;

    and then be able to refer to the classes as sqlite.Useful and mysql.Useful.
    Why not?

    Another possibility would be the option of renaming them, Python-style, e.g.

    import com.example.android.sqlite.Useful as SQLiteUseful;
    import com.example.android.mysql.Useful as MySQLUseful;

    This lets the client code manage its own namespace, independently of the
    providers’ namespaces.
     
    Lawrence D'Oliveiro, Feb 18, 2011
    #1
    1. Advertising

  2. "Lawrence D'Oliveiro" <_zealand> wrote in message
    news:ijl6ti$chs$...
    > Consider a piece of code which wants to use two different classes,
    > com.example.android.sqlite.Useful and com.example.android.mysql.Useful.
    > Your
    > only reasonable options are:
    >
    > 1) Choose one to specify in an import statement so you can use its name
    > unqualified. The other class will have to be referred to by its fully
    > qualified name
    > 2) Don’t bother importing either, use the full name for both.
    >
    > There is no way you can do something like
    >
    > import com.example.android.sqlite;
    > import com.example.android.mysql;
    >
    > and then be able to refer to the classes as sqlite.Useful and
    > mysql.Useful.
    > Why not?
    >
    > Another possibility would be the option of renaming them, Python-style,
    > e.g.
    >
    > import com.example.android.sqlite.Useful as SQLiteUseful;
    > import com.example.android.mysql.Useful as MySQLUseful;
    >
    > This lets the client code manage its own namespace, independently of the
    > providers’ namespaces.


    I agree that it would be useful. On the other hand, C# allows something
    almost exactly like your second suggestion, but it's almost never used. In
    fact, when I asked about it on a C# newsgroup, most people assumed it was
    something I was proposing as an extension.
     
    Mike Schilling, Feb 18, 2011
    #2
    1. Advertising

  3. On 02/17/2011 11:25 PM, Lawrence D'Oliveiro wrote:
    > Consider a piece of code which wants to use two different classes,
    > com.example.android.sqlite.Useful and com.example.android.mysql.Useful. Your
    > only reasonable options are:
    >
    > 1) Choose one to specify in an import statement so you can use its name
    > unqualified. The other class will have to be referred to by its fully
    > qualified name
    > 2) Don’t bother importing either, use the full name for both.
    >
    > There is no way you can do something like
    >
    > import com.example.android.sqlite;
    > import com.example.android.mysql;
    >
    > and then be able to refer to the classes as sqlite.Useful and mysql.Useful.
    > Why not?
    >
    > Another possibility would be the option of renaming them, Python-style, e.g.
    >
    > import com.example.android.sqlite.Useful as SQLiteUseful;
    > import com.example.android.mysql.Useful as MySQLUseful;
    >
    > This lets the client code manage its own namespace, independently of the
    > providers’ namespaces.
    >


    You could always add two classes to your program:

    class SQLiteUseful extends com.example.android.sqlite.Useful {
    }

    class MySQLUseful extends com.example.android.mysql.Useful {
    }

    --

    Knute Johnson
    s/nospam/knute2011/
     
    Knute Johnson, Feb 18, 2011
    #3
  4. Mike Schilling <> wrote:
    > "Lawrence D'Oliveiro" <_zealand> wrote in message
    > news:ijl6ti$chs$...
    >> Consider a piece of code which wants to use two different classes,
    >> com.example.android.sqlite.Useful and com.example.android.mysql.Useful.
    >> [suggested way to enhance syntax of "import"]

    > I agree that it would be useful.


    So do I.
    I once proposed something along "import ... as ...;" also as a
    means to create local shortcuts for parameterized generic types.
    Such as: import HashMap<String,MyFoo<This,That>> as MyFooMap;
    (here assuming HashMap was already imported earlier.)

    Of, course, proposing something *here* has, at most, the psychological
    effect of getting it off one's chest, and maybe read one or two agrees
    or "noone-needs-it"s. The likeliness of getting anything rolling, here,
    even if it is a sound proposal, is roughly 0.000%.
    To be fair, sometimes one also gets enlightened about flaws in one's
    proposal.
     
    Andreas Leitgeb, Feb 18, 2011
    #4
  5. Knute Johnson <> wrote:
    > You could always add two classes to your program:
    >
    > class SQLiteUseful extends com.example.android.sqlite.Useful {
    > }


    And how, exactly, would you cast an instance of
    com.example.android.sqlite.Useful (returned by
    some library fuction) to SQLiteUseful?
     
    Andreas Leitgeb, Feb 18, 2011
    #5
  6. Re: Why Can’t You Import Packages?

    On 02/18/2011 09:23 AM, Andreas Leitgeb wrote:
    > Knute Johnson<> wrote:
    >> You could always add two classes to your program:
    >>
    >> class SQLiteUseful extends com.example.android.sqlite.Useful {
    >> }

    >
    > And how, exactly, would you cast an instance of
    > com.example.android.sqlite.Useful (returned by
    > some library fuction) to SQLiteUseful?
    >


    I'm sure there are plenty of cases where it wouldn't work.

    --

    Knute Johnson
    s/nospam/knute2011/
     
    Knute Johnson, Feb 18, 2011
    #6
  7. Knute Johnson <> wrote:
    > On 02/18/2011 09:23 AM, Andreas Leitgeb wrote:
    >> Knute Johnson<> wrote:
    >>> You could always add two classes to your program:
    >>> class SQLiteUseful extends com.example.android.sqlite.Useful {
    >>> }

    >> And how, exactly, would you cast an instance of
    >> com.example.android.sqlite.Useful (returned by
    >> some library fuction) to SQLiteUseful?

    > I'm sure there are plenty of cases where it wouldn't work.


    So, he could always add those classes, but there are plenty of
    cases where they wouldn't help. Back to the proposal?
     
    Andreas Leitgeb, Feb 18, 2011
    #7
  8. Re: Why Can’t You Import Packages?

    On 18/02/2011 08:25, Lawrence D'Oliveiro allegedly wrote:
    > Consider a piece of code which wants to use two different classes,
    > com.example.android.sqlite.Useful and
    > com.example.android.mysql.Useful. Your only reasonable options are:
    >
    > 1) Choose one to specify in an import statement so you can use its
    > name unqualified. The other class will have to be referred to by its
    > fully qualified name 2) Don’t bother importing either, use the full
    > name for both.
    >
    > There is no way you can do something like
    >
    > import com.example.android.sqlite; import com.example.android.mysql;
    >
    > and then be able to refer to the classes as sqlite.Useful and
    > mysql.Useful. Why not?
    >
    > Another possibility would be the option of renaming them,
    > Python-style, e.g.
    >
    > import com.example.android.sqlite.Useful as SQLiteUseful; import
    > com.example.android.mysql.Useful as MySQLUseful;
    >
    > This lets the client code manage its own namespace, independently of
    > the providers’ namespaces.
    >


    Out of curiosity: have you spent months compiling your "questions" and
    are posting them in a batch, or do you just have too much time on your
    hands and make them up on the fly?

    --
    DF.
     
    Daniele Futtorovic, Feb 18, 2011
    #8
  9. Re: Why Can’t You Import Packages?

    On 02/18/2011 09:50 AM, Andreas Leitgeb wrote:
    > Knute Johnson<> wrote:
    >> On 02/18/2011 09:23 AM, Andreas Leitgeb wrote:
    >>> Knute Johnson<> wrote:
    >>>> You could always add two classes to your program:
    >>>> class SQLiteUseful extends com.example.android.sqlite.Useful {
    >>>> }
    >>> And how, exactly, would you cast an instance of
    >>> com.example.android.sqlite.Useful (returned by
    >>> some library fuction) to SQLiteUseful?

    >> I'm sure there are plenty of cases where it wouldn't work.

    >
    > So, he could always add those classes, but there are plenty of
    > cases where they wouldn't help. Back to the proposal?


    It's been a long time but didn't we have macros in C that would do
    similar things? Maybe we need macros.

    --

    Knute Johnson
    s/nospam/knute2011/
     
    Knute Johnson, Feb 18, 2011
    #9
  10. Lawrence D'Oliveiro

    Lew Guest

    Re: Why Can’t You Import Packages?

    On Feb 18, 1:53 pm, Knute Johnson <> wrote:
    > On 02/18/2011 09:50 AM, Andreas Leitgeb wrote:
    >
    > > Knute Johnson<>  wrote:
    > >> On 02/18/2011 09:23 AM, Andreas Leitgeb wrote:
    > >>> Knute Johnson<>   wrote:
    > >>>> You could always add two classes to your program:
    > >>>> class SQLiteUseful extends com.example.android.sqlite.Useful {
    > >>>> }
    > >>> And how, exactly, would you cast an instance of
    > >>> com.example.android.sqlite.Useful (returned by
    > >>> some library fuction) to SQLiteUseful?
    > >> I'm sure there are plenty of cases where it wouldn't work.

    >
    > > So, he could always add those classes, but there are plenty of
    > > cases where they wouldn't help.  Back to the proposal?

    >
    > It's been a long time but didn't we have macros in C that would do
    > similar things?  Maybe we need macros.
    >


    Maybe we just need to write out the FQN when there are more than one
    class with the same simple name in scope.

    The situation is rare enough that sprained typing fingers should not
    be grave risk, whiners.

    --
    Lew
     
    Lew, Feb 18, 2011
    #10
  11. Re: Why Can’t You Import Packages?

    On 02/18/2011 11:16 AM, Lew wrote:
    > On Feb 18, 1:53 pm, Knute Johnson<> wrote:
    >> On 02/18/2011 09:50 AM, Andreas Leitgeb wrote:
    >>
    >>> Knute Johnson<> wrote:
    >>>> On 02/18/2011 09:23 AM, Andreas Leitgeb wrote:
    >>>>> Knute Johnson<> wrote:
    >>>>>> You could always add two classes to your program:
    >>>>>> class SQLiteUseful extends com.example.android.sqlite.Useful {
    >>>>>> }
    >>>>> And how, exactly, would you cast an instance of
    >>>>> com.example.android.sqlite.Useful (returned by
    >>>>> some library fuction) to SQLiteUseful?
    >>>> I'm sure there are plenty of cases where it wouldn't work.

    >>
    >>> So, he could always add those classes, but there are plenty of
    >>> cases where they wouldn't help. Back to the proposal?

    >>
    >> It's been a long time but didn't we have macros in C that would do
    >> similar things? Maybe we need macros.
    >>

    >
    > Maybe we just need to write out the FQN when there are more than one
    > class with the same simple name in scope.
    >
    > The situation is rare enough that sprained typing fingers should not
    > be grave risk, whiners.
    >
    > --
    > Lew


    I wondered where you were Lew :).

    --

    Knute Johnson
    s/nospam/knute2011/
     
    Knute Johnson, Feb 18, 2011
    #11
  12. Lawrence D'Oliveiro

    Arne Vajhøj Guest

    Re: Why Can’t You Import Packages?

    On 18-02-2011 12:12, Andreas Leitgeb wrote:
    > Of, course, proposing something *here* has, at most, the psychological
    > effect of getting it off one's chest, and maybe read one or two agrees
    > or "noone-needs-it"s. The likeliness of getting anything rolling, here,
    > even if it is a sound proposal, is roughly 0.000%.
    > To be fair, sometimes one also gets enlightened about flaws in one's
    > proposal.


    If you want things changed in Java then join the JCP.

    Feedback on ideas are usually pretty negative. But that is
    to be expected. Java is more than 15 years old. Any ideas
    that are really good for many without any compatibility problems
    are most likely already implemented. The easy problems
    get fixed first - what is left us all the hard problems.

    Arne
     
    Arne Vajhøj, Feb 18, 2011
    #12
  13. Lawrence D'Oliveiro

    Arne Vajhøj Guest

    Re: Why Can’t You Import Packages?

    On 18-02-2011 14:16, Lew wrote:
    > On Feb 18, 1:53 pm, Knute Johnson<> wrote:
    >> On 02/18/2011 09:50 AM, Andreas Leitgeb wrote:
    >>> Knute Johnson<> wrote:
    >>>> On 02/18/2011 09:23 AM, Andreas Leitgeb wrote:
    >>>>> Knute Johnson<> wrote:
    >>>>>> You could always add two classes to your program:
    >>>>>> class SQLiteUseful extends com.example.android.sqlite.Useful {
    >>>>>> }
    >>>>> And how, exactly, would you cast an instance of
    >>>>> com.example.android.sqlite.Useful (returned by
    >>>>> some library fuction) to SQLiteUseful?
    >>>> I'm sure there are plenty of cases where it wouldn't work.

    >>
    >>> So, he could always add those classes, but there are plenty of
    >>> cases where they wouldn't help. Back to the proposal?

    >>
    >> It's been a long time but didn't we have macros in C that would do
    >> similar things? Maybe we need macros.
    >>

    >
    > Maybe we just need to write out the FQN when there are more than one
    > class with the same simple name in scope.
    >
    > The situation is rare enough that sprained typing fingers should not
    > be grave risk, whiners.


    With a decent package and class structure and naming, then it
    should happen so rare that it a complete no problem.

    Arne
     
    Arne Vajhøj, Feb 18, 2011
    #13
  14. On 18-02-2011 02:25, Lawrence D'Oliveiro wrote:
    > Consider a piece of code which wants to use two different classes,
    > com.example.android.sqlite.Useful and com.example.android.mysql.Useful. Your
    > only reasonable options are:
    >
    > 1) Choose one to specify in an import statement so you can use its name
    > unqualified. The other class will have to be referred to by its fully
    > qualified name
    > 2) Don’t bother importing either, use the full name for both.
    >
    > There is no way you can do something like
    >
    > import com.example.android.sqlite;
    > import com.example.android.mysql;
    >
    > and then be able to refer to the classes as sqlite.Useful and mysql.Useful.
    > Why not?
    >
    > Another possibility would be the option of renaming them, Python-style, e.g.
    >
    > import com.example.android.sqlite.Useful as SQLiteUseful;
    > import com.example.android.mysql.Useful as MySQLUseful;
    >
    > This lets the client code manage its own namespace, independently of the
    > providers’ namespaces.


    I agree completely with Mike.

    C# got the last feature.

    Practically none is using it.

    That seems a pretty good indication that it is not necessary.

    Arne
     
    Arne Vajhøj, Feb 18, 2011
    #14
  15. In message <ijm794$q8u$-september.org>, Mike Schilling wrote:

    > "Lawrence D'Oliveiro" <_zealand> wrote in message
    > news:ijl6ti$chs$...
    >
    >> Another possibility would be the option of renaming them, Python-style,
    >> e.g.
    >>
    >> import com.example.android.sqlite.Useful as SQLiteUseful;
    >> import com.example.android.mysql.Useful as MySQLUseful;
    >>
    >> This lets the client code manage its own namespace, independently of the
    >> providers’ namespaces.

    >
    > ... C# allows something almost exactly like your second suggestion, but
    > it's almost never used.


    Maybe certain languages attract certain kinds of programmer. I have used
    such renaming in Python several times. Of course, it also helps that, in
    Python, everything is a value accessible at run-time, so you can rename
    anything just by assigning it to a name of your choice.
     
    Lawrence D'Oliveiro, Feb 18, 2011
    #15
  16. In message <4d5edb39$0$23754$>, Arne Vajhøj wrote:

    > With a decent package and class structure and naming, then it
    > should happen so rare that it a complete no problem.


    Going back to my example, with com.example.android.sqlite.Useful and
    com.example.android.mysql.Useful, would you class that as “a decent package
    and class structure and naming� If not, why not?
     
    Lawrence D'Oliveiro, Feb 18, 2011
    #16
  17. In message <4d5edafd$0$23754$>, Arne Vajhøj wrote:

    > If you want things changed in Java then join the JCP.


    You mean, the one that Oracle ignores?
     
    Lawrence D'Oliveiro, Feb 18, 2011
    #17
  18. Lawrence D'Oliveiro

    Lew Guest

    Arne Vajhøj wrote:
    > With a decent package and class structure and naming, then it
    > should happen so rare that it a complete no problem.


    Unfortunately there is a somewhat significant conflict between java.util.Date
    and java.sql.Date and a few other types that too often appear near each other.
    Thus the question of how to handle conflicts in simple names is worth
    consideration.

    I just don't see the harm in typing FQNs for those cases to where we must
    change the language. It's a little extra typing, handled largely by our
    favorite IDEs anyway. BFD.

    --
    Lew
    Honi soit qui mal y pense.
     
    Lew, Feb 19, 2011
    #18
  19. On 18-02-2011 18:51, Lawrence D'Oliveiro wrote:
    > In message<4d5edafd$0$23754$>, Arne Vajhøj wrote:
    >
    >> If you want things changed in Java then join the JCP.

    >
    > You mean, the one that Oracle ignores?


    The one that all changes to Java goes through.

    Arne
     
    Arne Vajhøj, Feb 19, 2011
    #19
  20. On 18-02-2011 18:50, Lawrence D'Oliveiro wrote:
    > In message<4d5edb39$0$23754$>, Arne Vajhøj wrote:
    >> With a decent package and class structure and naming, then it
    >> should happen so rare that it a complete no problem.

    >
    > Going back to my example, with com.example.android.sqlite.Useful and
    > com.example.android.mysql.Useful, would you class that as “a decent package
    > and class structure and naming� If not, why not?


    No I would not.

    Database specific code is not good in general.

    And having a single class dealing with both SQLite and MySQL
    is not good either.

    Arne
     
    Arne Vajhøj, Feb 19, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul  Smith
    Replies:
    0
    Views:
    734
    Paul Smith
    Nov 18, 2003
  2. Paul Opal
    Replies:
    12
    Views:
    961
    Paul Opal
    Oct 11, 2004
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    909
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,994
    Smokey Grindel
    Dec 2, 2006
  5. Radek Simcik

    can’t get pingfm class work

    Radek Simcik, Mar 9, 2010, in forum: Ruby
    Replies:
    0
    Views:
    106
    Radek Simcik
    Mar 9, 2010
Loading...

Share This Page