is there any reason UFCS can't be used with 'new'?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Nov 10 11:36:59 PST 2014


On 11/10/2014 11:07 AM, Suliman wrote:
> I can't understand how to use UFCS with instance of class:
>
> void main()
> {
>
> string name = "Suliman";
> userName username = new userName(name);
>
> /// How to use UFCS here?
> userName.name.sayHello();
> ///
> }
>
> class userName
> {
>      string name;
>
>      this(string name)
>      {
>          this.name = name;
>      }
>
>      void sayHello(string name)
>      {
>          writeln(name);
>      }
> }

UFCS is about calling a free-standing function like a member function. 
The following example makes more sense to me:

void main()
{
     string name = "Suliman";
     userName username = new userName(name);

     username.sayHello();    // UFCS
}

// Does not have a sayHello() member function
class userName
{
     string name;

     this(string name)
     {
         this.name = name;
     }
}

void sayHello(userName u)
{
     import std.stdio;
     writeln(u.name);
}

Ali



More information about the Digitalmars-d-learn mailing list