What is difference between using a command $classmethod rather than just invoking them straight away?

What is difference between using a command $classmethod rather than just invoking them straight away?

For example if i need to call a class and its method i can just use like

do ##class(circle).radius()

rather than using

do $classmethod("circle",radius)

(I suppose both of them doing the same function i am not aware of it)

Please help me understand what is different and is there any specific usage.

Correct me if i have made mistake.

  • 0
  • 0
  • 252
  • 3
  • 4

Answers

How to call a classmethod?

If you are inside the class Circle call it with:

do ..Radius()

If you call this method from another class use:

do ##class(Circle).Radius()

But there are cases when you do not know either name of a class or name of a method on runtime. So $classmethod is your friend here:

do $classmethod("Circle","Radius")

HTH

the key difference is that you can pass the classname by a variable.
So you are prepared to get the classname passed by some other method.

In your example with a constant string"circle"you miss the key advantage and It's of o added value.

BTW. Correct is   $classmethod("circle.radius","%New")

http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=...

So can this is be used in a parent-child class format if how will this be of use. And by using $classmethod this is there any advantage, if so can you mention some.

Thanks

Kishan R

The key difference of $classmethod is runtime:

$CLASSMETHOD permits a ObjectScript program to invoke an arbitrary class method in an arbitrary class. Both the class name and the method name may be computed at runtime or supplied as string constants.proof

If you can, always invoke code directly:

do ##class(circle).radius()

Use classmethod only if you can't invoke method directly.

Comments

I think in case you need to call a lot of methods in programatic mode, you can do it this way, btw is easier.