title:: asTarget
summary:: Convert to a valid Node Target
categories:: Server>Nodes
related:: Classes/Node

method:: asTarget

The classes listed below implement the method code::asTarget::. This is used widely in the link::Classes/Node:: classes ( link::Classes/Group:: and link::Classes/Synth:: ) to convert non-Node objects to an appropriate target.
This allows nil and instances of link::Classes/Server:: to be used as targets. This can be useful when writing classes which create nodes internally, but in most cases there should be little need to call asTarget in normal use.

For an updated list of which classes that implements code::asTarget::, see asTarget in link::Overviews/Methods#asTarget::

definitionlist::
## link::Classes/Node#-asTarget:: || Returns the instance of Node itself. The subclasses of Node (Synth and Group) are valid targets and require no conversion.

## link::Classes/Server#-asTarget:: || Returns a link::Classes/Group:: object representing the link::Reference/default_group:: of this instance of Server. Note that this object may not be identical with other objects representing the default group, but will be equivalent.
code::
s = Server.default;
g = s.asTarget;     // the default group of s
h = s.defaultGroup; // and again
g == h;             // true
g === h;            // false
::

## link::Classes/Nil#-asTarget:: || Returns a link::Classes/Group:: object representing the link::Reference/default_group:: of the current default link::Classes/Server::.
code::
s = Server.default;
g = nil.asTarget;
g == s.defaultGroup; // true
::

## link::Classes/Integer#-asTarget:: || Returns a link::Classes/Group:: object representing a group node on the current default Server with this Integer as its node ID number.
Note:: Although this can be convenient in some cases, it does not create the corresponding node on the default server, nor does it check to make sure that it exists. As well it does not directly access the server's NodeIDAllocator, so duplication of node IDs is possible. For these reasons this method should be used with care. When not dealing with the default Server, Group-basicNew is safer and simpler, as otherwise one needs to set the server instance variable to ensure correct targeting. ::
code::
/////// Showing the problems

s = Server.default;
s.boot;
g = s.nextNodeID.asTarget;
x = Synth.head(g, "default");   // but g doesn't exist on the server
s.sendMsg(*g.addToHeadMsg);     // now it's sent to the default server, in the default group
x = Synth.head(g, "default");   // now this works
x.free; g.free;

// if not using the default Server Integer-asTarget can be problematic

Server.default = Server.local;
Server.default.boot;            // quit the default server
i = Server.internal; i.boot;
g = i.nextNodeID.asTarget;
i.sendMsg(*g.addToHeadMsg);     // seems to work, but...
x = Synth.head(g, "default");   // oops, this goes to the default server, so Group not Found
g.server == Server.default;     // true, so that's the problem
g.server = i;
x = Synth.head(g, "default");   // now to the right place
x.free; g.free;
::
code::
/////// A more practical example

s = Server.default;
s.boot;
s.sendMsg(\g_new, x = s.nextNodeID);
// ...

// now if we need to use Node objects for some reason
y = Synth.head(x.asTarget, "default");

// this is simpler than Group.basicNew(s, x);, providing you're using the default server:
z = Synth.head(Group.basicNew(s, x), "default");

y.free; z.free; x.asTarget.free;
::
::
