www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Auto-Implemented properties

reply "michaelc37" <michaelc37 msn.com> writes:
i was trying to make a D template to mimic auto-implemented


I think i got it to work but when i tried to give the template a
more meaning full name like AutoImplementedProperty i get a
compile error "a.title is not an lvalue".
Is this a bug?
Is there a more suitable way of doing this?


class Bar
{
	public string Title { get; set; }
}

my attempt:
class Bar
{
	alias autoproperty!(string, "get", "set") title
}

template autoproperty(T, args...)
{
	import std.typetuple;
	 property
	{		
		private T _name;
		static if (args.length)
		{
			static if (staticIndexOf!("get", args) > -1)
			{
				public T autoproperty()
				{
					return _name;
				}			
			}
	
			static if (staticIndexOf!("set", args) > -1)
			{
				public void autoproperty(T value)
				{
					_name = value;
				}
			}
			
		}
	}
}

void main(string[] args)
{
	Bar a = new Bar();
	a.title = "asf";	
	writefln(a.title);

	return;
}
Jan 05 2013
next sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
Hi Michael,

your code works for me (DMD 2.061, Linux), with a semicolon after the alias
in class Bar.

Also, use writeln, not writefln, because writefln assumes the first
parameter is the formatting string.
Jan 05 2013
next sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Saturday, 5 January 2013 at 22:53:44 UTC, Philippe Sigaud 
wrote:
 Hi Michael,

 your code works for me (DMD 2.061, Linux), with a semicolon 
 after the alias
 in class Bar.

 Also, use writeln, not writefln, because writefln assumes the 
 first
 parameter is the formatting string.
Why would you want get/set though when D offers property functions? The argument of "if your public attribute becomes private, then code breaks" is invalid in D.
Jan 05 2013
parent "michaelc37" <michaelc37 msn.com> writes:
On Saturday, 5 January 2013 at 23:00:48 UTC, monarch_dodra wrote:
 On Saturday, 5 January 2013 at 22:53:44 UTC, Philippe Sigaud 
 wrote:
 Hi Michael,

 your code works for me (DMD 2.061, Linux), with a semicolon 
 after the alias
 in class Bar.

 Also, use writeln, not writefln, because writefln assumes the 
 first
 parameter is the formatting string.
Why would you want get/set though when D offers property functions? The argument of "if your public attribute becomes private, then code breaks" is invalid in D.
I'm not sure if i ever would use the template, but anyway I was having a cs vs d argument with a friend, and we ended up writing code samples for comparison. This is just one of the items that came up.
Jan 05 2013
prev sibling parent reply "michaelc37" <michaelc37 msn.com> writes:
On Saturday, 5 January 2013 at 22:53:44 UTC, Philippe Sigaud 
wrote:
 Hi Michael,

 your code works for me (DMD 2.061, Linux), with a semicolon 
 after the alias
 in class Bar.

 Also, use writeln, not writefln, because writefln assumes the 
 first
 parameter is the formatting string.
Thanks for the tip. Does it work for you if you rename the template to "AutoImplementedProperty"?
Jan 05 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/05/2013 03:25 PM, michaelc37 wrote:
 On Saturday, 5 January 2013 at 22:53:44 UTC, Philippe Sigaud wrote:
 Hi Michael,

 your code works for me (DMD 2.061, Linux), with a semicolon after the
 alias
 in class Bar.

 Also, use writeln, not writefln, because writefln assumes the first
 parameter is the formatting string.
Thanks for the tip. Does it work for you if you rename the template to "AutoImplementedProperty"?
I confirm this: Any name other than the seemingly magical autoproperty causes the following errors: Error: a.title is not an lvalue Error: expression has no value Ali
Jan 05 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/05/2013 04:32 PM, Ali Çehreli wrote:

 I confirm this: Any name other than the seemingly magical autoproperty
 causes the following errors:

 Error: a.title is not an lvalue
 Error: expression has no value
I retract that! :) That happens when I replace only the two "autoproperty" with "AutoImplementedProperty." There is no error when I replace all four! :) Ali
Jan 05 2013
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2013-01-05 23:40, michaelc37 wrote:
 i was trying to make a D template to mimic auto-implemented


 I think i got it to work but when i tried to give the template a
 more meaning full name like AutoImplementedProperty i get a
 compile error "a.title is not an lvalue".
 Is this a bug?
 Is there a more suitable way of doing this?


 class Bar
 {
      public string Title { get; set; }
 }

 my attempt:
 class Bar
 {
      alias autoproperty!(string, "get", "set") title
 }

 template autoproperty(T, args...)
 {
      import std.typetuple;
       property
      {
          private T _name;
          static if (args.length)
          {
              static if (staticIndexOf!("get", args) > -1)
              {
                  public T autoproperty()
                  {
                      return _name;
                  }
              }

              static if (staticIndexOf!("set", args) > -1)
              {
                  public void autoproperty(T value)
                  {
                      _name = value;
                  }
              }

          }
      }
 }

 void main(string[] args)
 {
      Bar a = new Bar();
      a.title = "asf";
      writefln(a.title);

      return;
 }
This won't work like you think it will. All instances of "Bar" will share the same "_name" variable. You need to use a mixin. This pass: void main () { Bar a = new Bar(); a.title = "asf"; Bar b = new Bar; assert(b.title == a.title); } -- /Jacob Carlborg
Jan 06 2013
parent "michaelc37" <michaelc37 msn.com> writes:
On Sunday, 6 January 2013 at 11:32:40 UTC, Jacob Carlborg wrote:
 On 2013-01-05 23:40, michaelc37 wrote:
 i was trying to make a D template to mimic auto-implemented


 I think i got it to work but when i tried to give the template 
 a
 more meaning full name like AutoImplementedProperty i get a
 compile error "a.title is not an lvalue".
 Is this a bug?
 Is there a more suitable way of doing this?


 class Bar
 {
     public string Title { get; set; }
 }

 my attempt:
 class Bar
 {
     alias autoproperty!(string, "get", "set") title
 }

 template autoproperty(T, args...)
 {
     import std.typetuple;
      property
     {
         private T _name;
         static if (args.length)
         {
             static if (staticIndexOf!("get", args) > -1)
             {
                 public T autoproperty()
                 {
                     return _name;
                 }
             }

             static if (staticIndexOf!("set", args) > -1)
             {
                 public void autoproperty(T value)
                 {
                     _name = value;
                 }
             }

         }
     }
 }

 void main(string[] args)
 {
     Bar a = new Bar();
     a.title = "asf";
     writefln(a.title);

     return;
 }
This won't work like you think it will. All instances of "Bar" will share the same "_name" variable. You need to use a mixin. This pass: void main () { Bar a = new Bar(); a.title = "asf"; Bar b = new Bar; assert(b.title == a.title); }
Yea i realized that later, but the only fix i could come up with required me passing the property name in the template parameters, so the declaration has changed a bit. second attempt; class Bar { public mixin autoproperty!(string, "title", "get", "set"); public mixin autoproperty!(string, "description", "get", "set"); } mixin template autoproperty(T, alias propertyName, args...) { import std.typetuple; mixin("private T _" ~ propertyName ~ ";"); property { static if (args.length) { static if (staticIndexOf!("get", args) > -1) { mixin(" T " ~ propertyName ~ "() { return _" ~ propertyName ~ "; }"); } static if (staticIndexOf!("set", args) > -1) { mixin(" void " ~ propertyName ~ "(T value) { _" ~ propertyName ~ " = value; }"); } } } }
Jan 06 2013