From mtmorgan at fhcrc.org  Fri Jan 23 15:49:27 2009
From: mtmorgan at fhcrc.org (Martin Morgan)
Date: Fri, 23 Jan 2009 06:49:27 -0800
Subject: [Rd] pass by reference for S4
In-Reply-To: <73B69AE489CA974C817780430D154F0901443833@dkfzex2.ad.dkfz-heidelberg.de>
	(Biczok Rudolf's message of "Mon, 19 Jan 2009 23:00:52 +0100")
References: <73B69AE489CA974C817780430D154F0901443833@dkfzex2.ad.dkfz-heidelberg.de>
Message-ID: <6phljt2qdqg.fsf@gopher4.fhcrc.org>

"Biczok Rudolf" <r.biczok at dkfz-heidelberg.de> writes:

> Hi all;
>
>
> Is it possible to modify the "@" operator that it can handle references
> (or external pointers) 

R has the notion of an external pointer, which is a pointer to C-level
data. Is that what you mean? See ?"externalptr-class" and the 'Writing
R Extensions' manual for details.

More likely you mean that you want to pass references rather than
values. An approach is to create an environment and place objects in
it; environments have pass-by-reference semantics.

  env <- new.env(parent=emptyenv())
  env$myobj <- ...

Environments can be placed in slots of an S4 class, so the contents of
instances of the class will not be copied. You have to pay attention
when creating the S4 instance that each instance gets a new
environment, e.g., defining an 'initialize' method or a class
constructor.

 setClass("MyClass", representation=representation(env="environment"))
 setMethod("initialize", "MyClass", function(.Object, myData, ...) {
     env <- new.env(parent=emptyenv())
     env$myData <- myData
     callNextMethod(.Object, env=env, ...)
 })

and then

> obj <- new("MyClass", myData=1:10)
> obj at env$myData
 [1]  1  2  3  4  5  6  7  8  9 10

R users will be confused by pass-by-reference semantics.  Others might
point you to the R.oo or proto packages.

Martin

> of S4 Object instead of the real Objects?
>
>
> Or is there any technical or compatibility issue which make it
> impossible?
>
>
>  
>
>
> I look forward to hear from you and also hope that you don't hate me for
> my nooby question.
>
>
>  
>
>
> Yours sincerely,
>
>
> Rudolf Biczok
>
>
>  
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M2 B169
Phone: (206) 667-2793



[Rd] pass by reference for S4
Martin Morgan