Re: [Vala] [SimpleType] public struct Integer : uint {} does not work



On 01/18/2012 11:27 PM, Jonathan Ryan wrote:
On Wed, Jan 18, 2012 at 5:20 PM, Jonathan Ryan
<jryan curious-computing com>wrote:

On Wed, Jan 18, 2012 at 11:56 AM, Henrik /KaarPoSoft<henrik kaarposoft dk
wrote:
Dear all,

I am trying to create a new type in vala,
like "typedef" in C.

http://live.gnome.org/Vala/**Tutorial<http://live.gnome.org/Vala/Tutorial>has this example:
/* defining an alias for a basic type (equivalent to typedef int Integer
in C)*/
[SimpleType]
public struct Integer : uint {}

However, it does not seem to work...

I have created the following file called i.vala:
<code>
[SimpleType]
public struct Integer : uint {
}
public static int main (string[] args) {return 0;}
</code>

Compiling with valac i.vala gives:
/home/henrik/v1/i.c: In function ‘integer_get_type’:
/home/henrik/v1/i.c:22:79: error: ‘integer_dup’ undeclared (first use in
this function)
/home/henrik/v1/i.c:22:79: note: each undeclared identifier is reported
only once for each function it appears in
/home/henrik/v1/i.c:22:109: error: ‘integer_free’ undeclared (first use
in this function)

This is running on Linux Mint 12 (like Ubuntu Oneiric)
Vala 0.14.0
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1

I tried also using latest version of vala from git:
Vala 0.15.0.72-516e7
with the same result.

valac --save-temps shows only this in i.c:
<snip>
GType integer_get_type (void) {
static volatile gsize integer_type_id__volatile = 0;
if (g_once_init_enter (&integer_type_id__volatile)) {
GType integer_type_id;
integer_type_id = g_boxed_type_register_static ("int32_t",
(GBoxedCopyFunc) integer_dup, (GBoxedFreeFunc) integer_free);
g_once_init_leave (&integer_type_id__volatile, integer_type_id);
}
return integer_type_id__volatile;
}
</snip>
So it is obvious that the C compiler barfs,
integer_dup and integer_free are never defined.


Any help on what I am doing wrong here would be most appreciated!

/Henrik
______________________________**_________________
vala-list mailing list
vala-list gnome org
http://mail.gnome.org/mailman/**listinfo/vala-list<http://mail.gnome.org/mailman/listinfo/vala-list>

Doesn't seem that you're doing anything wrong. Vala isn't generating the
normal dup and free functions for your simple type struct. I'm going to
check bugzilla if there's a bug about it. This should be removed from the
tutorial considering it doesn't work.

It looks like that deriving from any primitive type is broken. You might be
able to trick Vala into making it work by defining the functions, but I
wouldn't do that. My suggestion is to not do this.

Thank you very much for your comments.

If this is broken, I would suggest we try to fix it, instead of just removing it from the tutorial!

This allows for nice encapsulation such as this:
<code>
[SimpleType]
public struct PlayingCard {
public int rank() { return this % 13; }
public int suit() { return this / 13; }
public static PlayingCard from_rank_suit(int r, int s) { return s*13+r; } }
public PlayingCard get_card() { return PlayingCard.from_rank_suit(7, 2); }
public void show_suit(PlayingCard c) { stdout.printf("suit=%d\n", c.suit()); }
public static int main (string[] args) {
var c=get_card();
show_suit(c);
var clist = new Gee.ArrayList<PlayingCard?>();
clist.add(c);
show_suit(clist.get(0));
PlayingCard[] carr = { c, PlayingCard.from_rank_suit(9, 3) };
show_suit(carr[1]);
return 0;}
</code>

To me, this conveys the intentions behind the code much better than e.g. a global
int rank(int card) { return card % 13; }
would.

In valaccodestructmodule.vala we have
add_struct_dup_function (st);
add_struct_free_function (st);
guarded by
if (!st.is_boolean_type () && !st.is_integer_type () && !st.is_floating_type ()) {

As far as I can see, this means that a
struct <name>: <boolean/integer/floating_type>
can not be boxed (e.g. for use in a Gee collection).

I have tried to move the dup and free generation outside the guard:
<diff>
diff --git a/codegen/valaccodestructmodule.vala b/codegen/valaccodestructmodule.vala
index affb48e..3cae4c9 100644
--- a/codegen/valaccodestructmodule.vala
+++ b/codegen/valaccodestructmodule.vala
@@ -169,9 +169,9 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
add_struct_destroy_function (st);
}

- add_struct_dup_function (st);
- add_struct_free_function (st);
}
+ add_struct_dup_function (st);
+ add_struct_free_function (st);

instance_finalize_context = old_instance_finalize_context;

</diff>

Now, if I replace
[SimpleType]
with
[CCode (type_id = "G_TYPE_INT")]
[IntegerType (rank = 6)]
in the card example, it compiles and executes.




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