Re: [Vala] Final / Sealed classes in Vala



2009/9/22 Jan Hudec <bulb ucw cz>:
On Tue, Sep 22, 2009 at 06:40:10 +0200, Marco Trevisan (Treviño) wrote:
Is it possible to define both in VAPIs and in a vala code a class which
can not be extended?

I mean something like "final" does for Java and "sealed" for C#...

I don't think so. Though some classes are not inheritable because they lack
proper construction functions. That's (unfortunately) the case for most
classes in the GLib namespace.

And I think vala should *NOT* get 'sealed'. It does not need it. In fact, C#
does not need it either -- they added it because Java had 'final', but in
Java it partly replaces missing non-virtual methods. In C# it's mostly
annoying.

I think they could be useful for grouping static methods in "virtual
classes" (I mean in a class which name has not a reference in a C
type/structure, but that has just been written for organizing similar
methods) without the risk of methods overriding or of extending a
"virtual" class that has been defined in a VAPI but that is not
available in the relative C code.

I think you are forgetting, that vala has non-member functions. Static
methods -- functions -- should be grouped in namespaces. There are many cases
of that in glib-2.0.vapi and gobject-2.0.vapi.

For example:

public final class Utility {
      public static void my_method();
}

That's only useful in Java which does not have non-member methods. And even
there the final keyword is useless -- it's static, so it cannot be virtual
and if you call it as Utility.my_method(), you always call that method and
nothing else. Only instance methods may be overriden.

In vala, you just use namespace as:

namespace Utility {
   public void my_method();
}

Note, that in C# you can now use a namespace now too, but in earlier versions
where non-member functions were not allowed there was special 'static class'
declaration for this purpose.

or in VAPI:

public abstract final MyOwnType {

Abstract final makes no sense, because 'abstract' means it must be inherited
and final means it must not.

In Java, it's an idiom for a static class. In C# it would be declared 'static
class' and in all languages that support non-member methods, including vala,
a namespace would be used instead.

And you forgot 'class' or 'struct'.

      public static void c_referenced_method1();
      public static void c_referenced_method2();
}

I hope you got the idea... :P

Yes. It is Java-specific idiom. In Vala, you just use namespace with
non-member functions.

Jan, I agree with you on almost everything you write, you do forget
one important use case for 'final' which (to some extent) applies to
Vala as well. Namely that of object serialization.

If I am writing/using a network protocol or other serialization that
has native object mapping (like fx. Java's RMI), then I will be quite
interested in assuring that I am serializing the right class, and not
some hacked up subclass which could cause in-compatibilities on the
wire. I've learned this the hard way in Java - and while it doesn't
apply directly to Vala, it might do so in the future in some wicked
DBus Object mapping.

-- 
Cheers,
Mikkel



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