www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - what is the difference between static method of the class, module

reply "ref2401" <refactor24 gmail.com> writes:
I have a class, and I want to write factory methods which will 
encapsulate different ways of creating instances of this class.
I can do this in 3 ways:

// static method of the class
public class Item
{
	public static Item createItem_1() { ... }
}

// module function
public Item createItem_2() { ... }

// static function of the module
public static Item createItem_3() { ... }

Which way should I choose? What are their pros and cons?
Jun 20 2012
next sibling parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <alex lycus.org> writes:
On 20-06-2012 17:45, ref2401 wrote:
 I have a class, and I want to write factory methods which will
 encapsulate different ways of creating instances of this class.
 I can do this in 3 ways:

 // static method of the class
 public class Item
 {
 public static Item createItem_1() { ... }
 }

 // module function
 public Item createItem_2() { ... }

 // static function of the module
 public static Item createItem_3() { ... }

 Which way should I choose? What are their pros and cons?

The static keyword has no effect on module functions, so 2 and 3 are equivalent. There is no significant difference between static class methods and module functions. -- Alex Rønne Petersen alex lycus.org http://lycus.org
Jun 20 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 06/20/2012 09:52 PM, bearophile wrote:
 Alex Rønne Petersen:

 The static keyword has no effect on module functions, so 2 and 3 are
 equivalent.

This problem is caused by DMD sloppyness regarding its management of attributes. A serious language doesn't accept a keyword like "static" where it has no meaning.

What is a 'serious language'? interface I{ static int x=2; } // java
 It just causes confusion to newbies, and makes
 it harder to port D code across future D compilers...

 Bye,
 bearophile

To be fair, it does not have no meaning. It is simply redundant, because all module scope declarations are implicitly 'static'.
Jun 20 2012
prev sibling next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Wednesday, June 20, 2012 17:47:05 Alex Rønne Petersen wrote:
 On 20-06-2012 17:45, ref2401 wrote:
 I have a class, and I want to write factory methods which will
 encapsulate different ways of creating instances of this class.
 I can do this in 3 ways:
 
 // static method of the class
 public class Item
 {
 public static Item createItem_1() { ... }
 }
 
 // module function
 public Item createItem_2() { ... }
 
 // static function of the module
 public static Item createItem_3() { ... }
 
 Which way should I choose? What are their pros and cons?

The static keyword has no effect on module functions, so 2 and 3 are equivalent. There is no significant difference between static class methods and module functions.

The only difference is how they're called. createItem() vs Item.createItem(). - Jonathan M Davis
Jun 20 2012
prev sibling next sibling parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Wednesday, 20 June 2012 at 17:47:15 UTC, Jonathan M Davis 
wrote:
 On Wednesday, June 20, 2012 17:47:05 Alex Rønne Petersen wrote:
 The static keyword has no effect on module functions, so 2 and 
 3 are equivalent. There is no significant difference between 
 static class methods and module functions.

The only difference is how they're called. createItem() vs Item.createItem().

I'd say just a little more than how they are called, it would be a direct relationship of the function with the class. Plus due to the direct relationship you can shorthand some of it (as long as it still makes sense). Although technically static functions are regular functions. But it's kinda like the difference of having enums internal vs external of a struct/class: Sometimes it just makes more sense (or localizes information a little more strongly).
Jun 20 2012
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Alex Rønne Petersen:

 The static keyword has no effect on module functions, so 2 and 
 3 are equivalent.

This problem is caused by DMD sloppyness regarding its management of attributes. A serious language doesn't accept a keyword like "static" where it has no meaning. It just causes confusion to newbies, and makes it harder to port D code across future D compilers... Bye, bearophile
Jun 20 2012