R method for a function - r

I have a constructor built function(Bond.Valuation). The result of which is a superclass of other classes, BondAnalytics. Now, from what I have read, I need to create methods on the class BondAnalytics. Reading Chambers book I have done the following:
When I type Bond.Valuation it says its a standard generic. So, show will work on it?
method.skeleton("show", c("BondAnalytics")).
So, I think this uses the generic function "show" on any object of BondAnalytics.
The command to method.skeleton produces:
setMethod("show",
signature(object = "BondAnalytics"),
function (object)
{
cat("Bond Valuation\n")
cat(BondID:);print(object#ID)
}
)
So, I think that show is a generic function that is now associated with this method and signature is class BondAnalytics, so this method will match itself to anything the matches class BondAnalytics.
So basically, I just have to define what I want to show from Bond.Valuation. What if I wanted a combination of text and a plot? Is that one method of methods in methods

That is correct. The method "show" you just declared will be associated with the BondAnalytics class. In this method you can do anything you want to. If you want to you can display summary statistics or plot some information about the object or do both.
This is from the manual for "show":
Display the object, by printing, plotting or whatever suits its
class. This function exists to be specialized by methods. The
default method calls ‘showDefault’.
It basically says you can do whatever you want to.

Related

In functional programming, Is it 'bad' to access a partial function's parameter values?

I would like to create a function that converts one partial function into another. To do that, it must access the values of the partial function's parameters.
Is that considered anti-fp?
I'm thinking it is ok, as I'm not mutating anything, I'm only using data within the functions scope, and it returns a partial function with the same input type and output type.
I'm asking this generally, but can provide some code if needed. This is my first question here.

How to get the form name in class?

I have a form which is used for automatic journal postings.
On that form I have a Ok command button and in closeOk method of the form I call the method from my datasource table.
In the JournalCheckPost class's infoResult() method I want to determine if the method is called from my form. I know that it can be done with caller methods but I don't know how exactly it should be done technically.
It is bad practice to make a method depend on where it is called from.
What you can do is to pass an extra parameter to the LedgerJournalCheckPost and infoResult can then check that. This can be done by introducing a boolean flag and a parm method.
I think, there can be many situations:
You want to pass some parameters from form
You want to manipulate the form (for example refresh datasource after action is complete)
Something other
But in all the cases depending on particular form is not a very good idea.
In first case you can set parameters from code using parm methods, or, better pass parameters using the Args class
In the second you can cast Args.caller to some interface that contain all the methods you want and manipulate the form using that methods (see \Classes\SysFormRun_doRe usages for example)

Ecore decorator

I have a generated Ecore model - works perfectly fine.
what I now do is, create an instance of the model programmatically and load it:
EARepository repository = EaadapterFactory.eINSTANCE.createEARepository();
repository.setFile(f);
repository.load();
Now I can call the methods like
repository.getName();
works fine!
My Problem: I want to customize the behavior of getName() now!. I would like to set a decorator here, like the genmodel does. E.g. the getName() method should return "no value set" if it has no value set.
Is it possible to customize the getName()'s behavior method here, like setting a decorator ?!
Reason: I want to keep the original behavior of the model. But in one of my use cases, the model should behave a little bit different.
thanks
Generally you should be using the generated item providers for producing the labels you see in the UI. I.e., there is a generated EARepositoryItemProvider with a getText method that you'd specialize for this purpose.

newInstance with arguments

Is there any way to 'dynamically'/reflectively/etc create a new instance of a class with arguments in Scala?
For example, something like:
class C(x: String)
manifest[C].erasure.newInstance("string")
But that compiles. (This is also, rest assured, being used in a context that makes much more sense than this simplified example!)
erasure is of type java.lang.Class, so you can use constructors (anyway you don't need manifest in this simple case - you can just use classOf[C]). Instead of calling newinstance directly, you can at first find correspondent constructor with getConstructor method (with correspondent argument types), and then just call newInstance on it:
classOf[C].getConstructor(classOf[String]).newInstance("string")

VTL evaluate or define an object reference

I would like to create a macro that takes a string as a parameter and evaluates that to an object. Something like:
#macro( valueTest $objRef)
#define( $obj )#evaluate("$${objRef}")#end
$obj.foo ## This would have to be translated to $obj.getFoo()
#end
Unfortunately the $obj variable does not point to object reference that could be used to call methods. $obj is a String and $obj.foo does not try to execute getFoo.
Somewhy I have a feeling that this is the nature of evaluate and it is not possible to do what I want to.
the reason why I want to do smth like this is because we have quite few macros that take both command bind path and command itself as a parameter and I am hoping the latter could be derived from first.
Unfortunately, Velocity does not have a mechanism to define functions which return object references. Macros are really intended to be a shortcut to display text.
In cases like this, the way to proceed is generally to create a "tool" in Java and put an instance in the context. A tool is just an ordinary class with a method that returns what you are looking for
e.g.
create an object with an "eval" method, then put it in the context as "referenceEvaluator".
#set($obj = $referenceEvaluator.eval($objRef))
You might find that your code is clearer if you avoid the double evaluation and just insert an object into the context named $obj that does what you want. (better performance, too).

Resources