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

Justin Whear via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Nov 10 11:35:05 PST 2014


On Mon, 10 Nov 2014 19:07:38 +0000, 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);
>      }
> }

This has nothing to do with new--you're trying to use a virtual function 
scoped
to class userName with a string.  Rewrite it to a static module-scoped 
function:

class userName {
	string name;

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

void sayHello(string name)
{
	writeln(name);
}

void main()
{

	string name = "Suliman";
	userName username = new userName(name);

	userName.name.sayHello();
}


More information about the Digitalmars-d-learn mailing list