On Fri, Dec 3, 2010 at 5:08 PM, Evan Nemerson <e...@coeus-group.com> wrote: > On Fri, 2010-12-03 at 15:38 -0800, Anatol Pomozov wrote: >> Hi, >> >> I have a class that contains a field and method with the same name, >> and Valac does not like it. What is the reason? Other languages >> (C#/Java) allow it. You have to use () for method so you know whether >> you access method or field. > > I'm not a C# or Java programmer, so feel free to correct me if I'm wrong > but... > > Java doesn't have delegates. In this example, what is X?
Ah, delegates, functions that act as variables. Now it a little bit clearer. > > public class Foo { > public boolean stop = true; > > public boolean stop () { > return true; > } > > public Foo () { > var X = this.stop; > } > } This example does not work. valac 0.11 complains with quite weird message a.vala:11.7-11.18: error: Assignment: Cannot convert from `Foo.stop' to `Foo.stop' var x = this.stop; We cannot use var - we need to declare Delegate type here. Something like MyDelegateType x = this.stop; But in this case it is clear that we want function. We cannot assign field/property to a delegate, isn't it? Sorry for stubbornness - I learn Vala and trying to illuminate all dark corners. I am looking for an example that clearly shows why we cannot have field/method with the same name. > Actually, now that I think about it, I don't think Java does type > inferencing either. It is planned for Java7. > > In addition to type inferencing issues, this could cause problems with > generics, and would generally be a pain to read. Not sure what do you mean here. > > C# doesn't allow this either. AFAIK, in C# you can overload methods so > that the most appropriate method is called, but you can't have a method > and a property with the same name. Example: > > class Foo { > bool stop () { > return true; > } > > // No problem > bool stop (bool a) { > return false; > } > > // Problem > bool stop = false; > > public static void Main () { > var foo = new Foo (); > } > } > > Overloading methods is a separate issue, which Aleksander Wabik already > explained. > >> anatol:vala $ valac a.vala >> a.vala:4.3-4.21: error: `Foo' already contains a definition for `stop' >> public boolean stop() { >> ^^^^^^^^^^^^^^^^^^^ >> a.vala:2.3-2.21: note: previous definition of `stop' was here >> public boolean stop = true; >> ^^^^^^^^^^^^^^^^^^^ >> Compilation failed: 1 error(s), 0 warning(s) > > Amusingly, this is basically the same error message as you get in C# (at > least from mcs): > > test.cs(12,8): error CS0102: The type `Foo' already contains a > definition for `stop' > test.cs(2,8): (Location of the symbol related to previous error) > Compilation failed: 1 error(s), 0 warnings Oops, my statement that it works in C# is wrong. I was looking at incorrect C# example. _______________________________________________ vala-list mailing list vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list