Re: [Vala] Name clashes in generated C code



On Tue, Dec 28, 2010 at 6:29 AM, Jan Hudec <bulb ucw cz> wrote:
On Mon, Dec 27, 2010 at 13:46:47 -0800, Anatol Pomozov wrote:
Hi,

I have a simple Vala program

public struct Struct { string name; }
public enum Type { STRUCT }

[...]
The problem here a name clash in generated Vala code - TYPE_STRUCT
both generated as type of Struct

#define TYPE_STRUCT (struct_get_type ())

and as a enum value

typedef enum  {
    TYPE_STRUCT
} Type;

what is the best way to avoid such clashes?

Well, it depends on whether you are writing a library or an application.

For library the API should be usable from C, so best way is just to avoid
calling the enum 'Type'. For an application the C names don't matter though,
so you can just tell vala to use different one in C.

Is it possible to change enum names to something else.

Try using the [CCode(cprefix=...)] annotation. You'll find examples in the
.vapi files. It's normally used to tell vala how externally provided things
are called, but it should work for renaming things generated by vala too.

Yeah, it is what i was looking for. Thanks for sharing this info.

It would be really great to have some information about attributes we
can use in vala code. There is almost nothing about it in the tutorial
http://live.gnome.org/Vala/Tutorial

I can change enum name from Type to something else e.g. RecordType, but
I would like to know if there is a better solution.

As I say, it depends on whether somebody will be using the API from C or not.
If they will, rename the type. If not, rename the members with cprefix.

I am sure using [CCode] is ok for libraries as well if cprefix value
does not change very often.

Another example of "C compatible names clashing" is method
overloading. I use it a lot in Java and miss it Vala. I would love
that vala compiler take care about it, something like:

If a developer compiles program without -H and -C then valac tries to
generate unique names for clashing names/overload methods &
constructors. It does not matter what names overloaded methods have
while it is consistent. If a user wants to have a library with C API
then valac fails with error saying "Names A.B_bar and A_B.bar are
clashing, please define external name for it using [CCode]". Does it
make sense?



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