6

I am using JRuby.

I have many classes implemented in Java, and I want to create objects off of them in my Ruby scripts.

Suppose that I have a class Sprite in Java. In Ruby, to refer to it, I use the following:

Java::ComMyWebsiteMyProjectName::Sprite

Because Sprite is defined in the package com.myWebsite.myProjectName.

Now then, to simplify things, I like to do this:

Sprite = Java::ComMyWebsiteMyProjectName::Sprite

Which sets the constant Sprite to the Sprite class defined in Java. Now my Ruby code can refer to it by just writing Sprite.

I have a file full of that kind of declarations. Something like:

Sprite   = Java::ComMyWebsiteMyProjectName::Sprite
Texture  = Java::ComMyWebsiteMyProjectName::Texture
Bitmap   = Java::ComMyWebsiteMyProjectName::Bitmap
Math     = Java::ComMyWebsiteMyProjectName::Math
Geometry = Java::ComMyWebsiteMyProjectName::Geometry

Now, I am not exactly very familiar with programming concepts. I am having problems giving this file a name, because I don't even know if there is a name for this kind of thing. Does this kind of "practice" have an actual name?

4
  • 4
    "What is the name of this thing" questions are off-topic. These are poor questions for the same reasons that "identify this obscure TV show, film or book by its characters or story" are bad questions: you can't Google them, they aren't practical in any way, they don't help anyone else, and allowing them opens the door for the asking of other types of marginal questions. See Also Let's Play the Guessing Game
    – gnat
    Commented Nov 20, 2013 at 11:00
  • 4
    @gnat: I'm not sure, that blog post says that a guessing game is when you have a vague or broad description, but in my case, I am giving an accurate and exact one: what practice is it when you reference large namespaces to shorter constants? It's exact and to the point without loose ends. I'm not giving "hints" - I am giving the full description.
    – Saturn
    Commented Nov 20, 2013 at 11:27
  • @gnat: Additionally, why would this question be wrong if questions like this other one are fine? The asker describes perfectly fine the concept (so did I), and he is asking what is it called like (so am I). In fact, it seems to me like the answers to that one question are guesses. Unless I am missing the bigger picture here, I feel like this question is as appropriate (or more) than that one.
    – Saturn
    Commented Nov 20, 2013 at 11:27
  • 2
    @GlenH7: Thanks, I ended up choosing the Meta one. You can find it here: meta.programmers.stackexchange.com/q/6296/13833
    – Saturn
    Commented Nov 21, 2013 at 21:32

3 Answers 3

7

Generically, this sort of thing is called Type Aliasing. When you're doing it specifically for a namespace, you're importing the namespace (or more accurately, importing the types within the namespace).

6

This is known by many different names - and probably called even more. And sometimes what you think is type aliasing in one language is something completely different in another.


In Perl, you've got code that looks like:

#!/usr/bin/perl

package Foo::Bar::Qux::Bletch;
$var = 42;

package main;
print $Foo::Bar::Qux::Bletch::var, "\n";
*A = *Foo::Bar::Qux::Bletch::var;
print $A,"\n";

Which is known by various things such as a typeglob alias or just glob aliasing - though it should be realized that a glob is something else.


In Clojure, this is simply an alias:

user=> (require 'clojure.string)
nil

user=> (alias 'string 'clojure.string)
nil

user=> (string/capitalize "hONdURas")
"Honduras"

In Ruby it appears to have the name of namespace alias as described in ruby-forum: Namespaces too looooooong

However it occurred to me that there is a difference between namespace aliases in PHP and the closest thing in Ruby (as described above): Autoloading.


In php, this is again, a namespace alias - php.net: Using namespaces: Aliasing/Importing

The ability to refer to an external fully qualified name with an alias, or importing, is an important feature of namespaces. This is similar to the ability of unix-based filesystems to create symbolic links to a file or to a directory.

PHP namespaces support three kinds of aliasing or importing: aliasing a class name, aliasing an interface name, and aliasing a namespace name. Note that importing a function or constant is not supported.


In Lisp, this isn't part of the standard language but there libraries which do it and call themselves 'package-local alias' or 'nicknames'. One such library is cl-package-aliases which provides some additional functionality:

* (defpackage :foo (:use :cl) (:alias (:common-lisp :bar)))

# <PACKAGE "FOO">
* (in-package :foo)

# <PACKAGE "FOO">
* (bar:format t "Hello World~%")
Hello World
NIL

In python, this is a namespace alias. (Examples from Can you define aliases for imported modules in Python?)

import a_ridiculously_long_module_name as short_name

Though you can also do it with an assigment

import long_module_name
lmn = long_module_name

In C# this is a namespace alias.

From Java equivalent to C# using alias

namespace TheirNamespace
{
    public class ContestedClassName
    {
    }
}

namespace MyNamespace
{
    public class ContestedClassName
    {
    }
}

namespace MyBoundaryNamespace
{
    using MyRenamedClass = MyNamespace.ContestedClassName;
    using TheirRenamedClass = TheirNamespace.ContestedClassName;

    public class Translator
    {
        public TheirRenamedClass Translate(MyRenamedClass value)
        {
            return null;
        }
    }
}

In Java, one doesn't alias a namespace, but rather imports specific items into the current namespace, though it does serve the question asked (how to reduce the amount of typing):

The code

double r = cos(Math.PI * theta);

can be reduced with a static import to:

import static java.lang.Math.PI;
...
double r = cos(PI * theta);

One often sees this usage with Junit to avoid having to specify each static method.

import static org.junit.Assert.*;

When people speak of type aliasing in Java it is more along the lines of:

A problem I often encounter in Java is that I want to say “these two things are the same”, but Java won’t let me. Suppose I want to maintain an int[] array which is always sorted in my program.

which gets to more of the type aliasing rather than namespace aliasing, because these are different things.


Type aliasing in Scala appears to be something quite different based on Beginner: Scala type alias in Scala 2.10? and Scala type alias including companion object

More about what type aliasing in Scala is can be found at Effective Scala


In C++11 type aliases are a type of typedef which does more things other than namespace juggling.

using identifier = type_name;   

An example of this would be:

// type alias, identical to
// typedef void (*func)(int, int);
using func = void (*) (int,int);
// the name 'func' now denotes a pointer to function:
void example(int, int) {}
func fn = example;

Though realize, there are some differences between these concepts as descried in Difference between typedef and C++11 type alias

The playing of namespaces however, is a namespace alias as described in In C++, what is a "namespace alias"?

namespace ublas = boost::numeric::ublas;

ublas::vector<double> v;

More on this at msdn namespace Alias

namespace a_very_long_namespace_name { ... }
namespace AVLNN = a_very_long_namespace_name;
// AVLNN is now a namespace-alias for a_very_long_namespace_name.

http://hyperpolyglot.org/ml shows a few languages with various forms of namespace aliasing.

OCaml:

module Gr = Graphics;;

Haskell:

import qualified Data.Bytestring as B

Namespaces aren't unique to programming languages, but also extend to other structured data such as XSL which has namespace aliases. These tend to be rather terse transformations (converting 'alt' to 'xsl' or something like that - but they serve the same purpose.


So, as noted, these concepts go by different names in different languages and what is known as 'type aliasing' in one language may mean something completely different in another language.

There is no standard name

0

This is usually called "bindings". For example, there are many node.js modules that are just bindings with C++ libraries. Or Python libraries that provide bindings for C code. And there are probably some JRuby libraries that provide bindings for Java code.

Not the answer you're looking for? Browse other questions tagged or ask your own question.