Re: [Vala] Cannot have field and method with the same name



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?

public class Foo {
  public boolean stop = true;

  public boolean stop () {
    return true;
  }

  public Foo () {
    var X = this.stop;
  }
}

Actually, now that I think about it, I don't think Java does type
inferencing either.

In addition to type inferencing issues, this could cause problems with
generics, and would generally be a pain to read.

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


-Evan




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]