rsc3/doc-schelp/HelpSource/Reference/asTarget.scrbl

97 lines
3.5 KiB
Text
Raw Permalink Normal View History

2022-08-24 13:53:18 +00:00
#lang scribble/manual
@(require (for-label racket))
@title{asTarget}
Convert to a valid Node Target@section{categories}
Server>Nodes
@section{related}
Classes/Node
@section{method}
asTarget
The classes listed below implement the method
@racketblock[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 ]
@racketblock[asTarget::, see asTarget in link::Overviews/Methods#asTarget::
]
@section{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.
@racketblock[
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::.
]
@racketblock[
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.
]
@section{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. ::
@racketblock[
/////// 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;
::
]
@racketblock[
/////// 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;
::
::
]