Basic question about methods

This forum is meant for questions and discussions about the X# language and tools
Post Reply
Anonymous

Basic question about methods

Post by Anonymous »

Hi, I'm working with X# for the first time and had a simple question. Is there a difference between METHOD and FUNCTION? Also, I understand the difference between ASSIGN and ACCESS but are these just an alternative to a classic method, or is there another reason for these? I feel like I'm seeing four different ways of doing the same thing. Thanks!
User avatar
lumberjack
Posts: 723
Joined: Fri Sep 25, 2015 3:11 pm

Basic question about methods

Post by lumberjack »

Hi Jeff,
Jeff wrote:Is there a difference between METHOD and FUNCTION? Also, I understand the difference between ASSIGN and ACCESS but are these just an alternative to a classic method, or is there another reason for these? I feel like I'm seeing four different ways of doing the same thing.
Yes there is a big difference.
Functions are STATIC METHODS so they are class methods and are not part of objects created from the class.

Code: Select all

CLASS MyClass
    METHOD MyMethod() AS VOID
         ? "Blah blahh"
    RETURN
    STATIC METHOD StatMethod() AS VOID
         ? "Static method"
    RETURN
END CLASS

VAR o := MyClass{}
?o:MyMethod()
?o:StatMethod() // Compile error does not contain a method StatMethod()
?MyClass.StatMethod() // Correct way, it is a class method not an object method
Access/Assign is the VO way of properties. Robert did an implementation to have that wrapped in a PROPERTY, property is the preferred way of doing it.

Code: Select all

PROPERTY MyProp AS STRING
    GET // Access
        RETURN SELF:_myprop
    END GET
    SET // Assign
        SELF:_myprop := VALUE
    END SET
END PROPERTY
See the documentation for the short versions of PROPERTY / END PROPERTY
User avatar
SHirsch
Posts: 281
Joined: Tue Jan 30, 2018 8:23 am

Basic question about methods

Post by SHirsch »

Hi Jeff,

METHOD belongs to a CLASS.
FUNCTION can be declared outside any CLASS definition. Internally it is a STATIC METHOD of a STATIC CLASS Functions.

Code: Select all

//enhanced sample from lumberjack, to show ACCESS, ASSIGN and FUNCTION
CLASS MyClass
    METHOD MyMethod() AS VOID
         ? "Blah blahh"
    RETURN
    STATIC METHOD StatMethod() AS VOID
         ? "Static method"
    RETURN
    PRIVATE _prop:=23 AS INT
    ACCESS MyProp as INT
    RETURN SELF:_prop
    ASSIGN MyProp(n as INT) VOID
        SELF:_prop := n 
    RETURN
END CLASS
FUNCTION MyFunc() AS VOID
   ?"Function"
RETURN

VAR o := MyClass{}
?o:MyMethod()
?o:StatMethod() // Compile error does not contain a method StatMethod()
?MyClass.StatMethod() // Correct way, it is a class method not an object method
?var x:= o:MyProp        //x is now 23
?o:MyProp := 42           //now _prop is 42
?MyFunc()     //call a function with out any reference to a class
There are always many ways to do the things. The Question is what is the best to achieve your goals.

Stefan
User avatar
Fabrice
Posts: 405
Joined: Thu Oct 08, 2015 7:47 am
Location: France

Basic question about methods

Post by Fabrice »

Hi Jeff,
Function or Method is a matter of design :
Are you Data Centric or "Action" centric ?
In the first case, you concentrate on the Data, so you identify the Objects of the system, then design the Classes that will provide these objects.
As Objects have States (Properties/Fields), they have Actions related "only" to these (objects) : These are the Methods.

If you are Action-centric, you will search for the treatments, datas are then parameters and returns

Sometimes, in the Data-Centric model, you identify some treatments that are not-really related to objects (or you don't want to go that deeper in the design), so you use a "Static Method", or a Function in xBase terminology.

Now, Access and Assign are a kind-of Methods that allows Read/Write access to Fields, or that are used to create "virtual" fields :
Think about a Rectangle class, that has Width and Height Fields, but Area is a virtual field calculated each time, it's a good candidate for an Access.
More generally, in OOD, we talk about Getter and Setter, which you can find in xSharp Core, or Vulcan dialects, with Property Get and Property Set.

HTH,
Fab
XSharp Development Team
fabrice(at)xsharp.eu
Jeff

Basic question about methods

Post by Jeff »

Thanks for the guidance everyone, it's been extremely helpful!
Post Reply