Home > varargs > Vala varargspassing property: value pairs

Vala varargspassing property: value pairs

January 28Hits:1
Advertisement

In the Project Vala Tutorial at Variable-Length Argument Lists, there is a calling technique shown:

A common pattern with varargs is to expect alternating string - value pairs as arguments, usually meaning gobject property - value. In this case you can write property: value instead, e.g.: actor.animate (AnimationMode.EASE_OUT_BOUNCE, 3000, x: 100.0, y: 200.0, rotation_angle_z: 500.0, opacity: 0);

is equivalent to:

actor.animate (AnimationMode.EASE_OUT_BOUNCE, 3000, "x", 100.0, "y", 200.0, "rotation-angle-z", 500.0, "opacity", 0);

How can I decode that? The code shown in the article is for the more normal all commas parameter passing. It doesn't properly decode the alternate style.

I tried the example code given:

void method_with_varargs(int fixed, ...) {     var l = va_list();     while (true) {         string? key = l.arg();         if (key == null) {             break;  // end of the list         }         double val = l.arg();         stdout.printf("%s: %g\n", key, val);     } } 

And it prints

x: 0  rotation_angle_z: 0 

If I take out the double val

void method_with_varargs(int fixed, ...) {     var l = va_list();     while (true) {         string? key = l.arg();         if (key == null) {             break;  // end of the list         }         stdout.printf("%s: \n", key);     } } 

it prints

x:0  y:0 rotation_angle_z: 0 opacity: 0 

My guess is that this needs to be a different type than string, but what?

string? key = l.arg();  

Answers

It's a bit hard to be sure without you showing the exact function call, but my guess is that you're mixing an int into the caller side. 0 is not the same as 0.0; the former is an int, the latter is a double. Typically, ints are 32 bits and doubles are 64 bits, so if you pass an int but try to read it as a double (or vice versa) things have a habit of getting out of sync and corrupting not just the current argument but future arguments as well.

Basically, either make sure all your value arguments are the same type, or assign the values of different keys to a variable of the right type (i.e., if (key == "opacity") { uint val = l.arg(); } else { double val = l.arg(); }).

With functions like Clutter.Actor.animate(), the type is determined by looking up the relevant GObject property. x, y, and rotation-angle-z are all floats, and opacity is an uint. Floats get promoted to double when passed to variadic functions (I can't actually remember if C specifies this or it's just all the x86 calling conventions) which is why 0.0 works as expected, but it would probably be more correct to use 0.0f for float arguments and 0.0 for doubles.

When using variadic arguments (in both C and Vala) you have to keep in mind that you lose type-safety. You have to be very careful about what types you're passing, because the compiler can't tell you when you mess up.

Tags:varargs, vala

Related Articles

  • Vala varargspassing property: value pairsJanuary 28

    In the Project Vala Tutorial at Variable-Length Argument Lists, there is a calling technique shown: A common pattern with varargs is to expect alternating string - value pairs as arguments, usually meaning gobject property - value. In this case you c

  • Writable Atomic property - cannot pair a synthesized getter with a user defined setterJanuary 10

    I have defined an atomic attributed property in my application but now find a new to override the setter so as to update some UI element whenever it is changed. However, I get the aforementioned warning. Is there a way to nullify this warning or if n

  • ECMA script - retrieving SPList property bagSeptember 18

    I have added a property bag to SPList. Since the SPList doesn't support property bag, I have added my custom property to SPList.RootFolder.Properties. I would like to retrieve this property using ECMA script. I couldn't find a getter method in ECMA s

  • Python property() implementation that caches getter while still allowing a setterMay 31

    For a session implementation I needed a property that caches its getter (since it involves a database lookup) but still allows modifications (e.g. assigning a new user and storing that user's id in the session). To make this as comfortable as possibl

  • Finding number pairs where the sum of their divisors are each otherFebruary 17

    Given a limit \$n\$, find all the pairs of values less than, or equal to n, which have the the property: pair(a,b) where a = sum of divisors of b and b = sum of divisors of a I need to speed up my double for-loop but I don't see how to do it in this

  • Clojure code for generating HAL JSON responsesJune 10

    I wanted to use hal in one of my personal Clojure projects, but unfortunately the only library I could find (halresource) was somewhat out of date when compared to the current specification. So, I decided to roll my own, which works fine for me perso

  • How to access a superclass method in octave?February 15

    I'm working with objects in octave and I would like to call the superclass set method in the subclass set. In the GNU octave documentation I haven't found how its works so I've tried to use the matlab documentation syntax but I get the next error: ''

  • HTML Utopia: Designing Without Tables Using CSS, Chapter 1: Getting The Lay Of The Land

    HTML Utopia: Designing Without Tables Using CSS, Chapter 1: Getting The Lay Of The LandMay 31

    CSS In Context Almost as soon as the Web became popularized by the emergence of the first graphical Web browser (the forerunner to Netscape Navigator), graphic designers became aware of a problem. The method by which the Web browser displayed informa

  • HTML Utopia: Designing Without Tables Using CSS, Chapter 5: Building the Skeleton

    HTML Utopia: Designing Without Tables Using CSS, Chapter 5: Building the SkeletonMay 31

    Building the Skeleton Most books on CSS begin by teaching you how to deal with the bits and pieces of a Web page: fonts, colors, lists, and the like. Then, they move on to explaining the broader issues associated with CSS positioning (CSS-P), which a

  • List properties using SharePoint.ClientApril 4

    How can I get a list of property/value pairs of a List? I have the following code: ClientContext context = new ClientContext("http://lalala.com"); Web web = context.Web; Guid g = new Guid("lalala"); List list = web.Lists.GetById(g); fo

  • CSS Architectures: Scalable and Modular Approaches

    CSS Architectures: Scalable and Modular ApproachesMay 1

    CSS Architectures CSS Architectures: Principles of Code Cleanup CSS Architectures: New Best Practices CSS Architectures: Scalable and Modular Approaches CSS Architectures: Refactor Your CSS CSS Architectures: Completing MetaCoax Refactoring As the on

  • User set .lnk association to Word 2007 Server 2008 R2 Terminal ServerJuly 11

    Nightmare. A user has managed to associate .lnk (shortcuts) with Word 2007. I cannot seem to find a way to remove this association on Server 2008 R2 (running as a TS with Citrix XA 6.5). I tried using a program called "unassoc" but this only see

  • Nullable T Implementation for VB6/VBAFebruary 14

    Because I was spoiled with C# and the .NET framework, whenever I have to work with VB6 I feel like something's missing in the language. A little while ago I implemented a List<T> for VB6 (here), and before that I implemented String.Format() and a nu

  • My Current CSS and Sass Styleguide

    My Current CSS and Sass StyleguideMarch 13

    It's never easy to find a way to write consistent, future-proof, and robust CSS. It's taken me months, if not years, to come up with a clean way to write mine. In fact, my process is still evolving, and I expect it to change even more. In the meantim

  • Dynamic Menu Builder for Bootstrap 3: Menu ManagerJune 20

    Dynamic Menu Builder for Bootstrap 3 Dynamic Menu Builder for Bootstrap 3: Menu Manager Dynamic Menu Builder for Bootstrap 3: Item and Link Creating menus and navigation bars has never been easier than with Twitter Bootstrap. We can easily create sty

  • Python Vector implementation that acts as a class and also a collection of staticmethodsJune 21

    Recently I've been wondering about ways of implementing objects in code that can be represented by other primitives. An example of this is a Vector, which can be represented by a Vector\$N\$D where \$N\$ is the dimension of vector, or it can be repre

  • What's the best design for a logical structure that contains different properties of various types for different entity categories? (MySQL or NoSQL)August 4

    I'm designing a MySQL database that needs to contain a list of products. Those products, based on their category, will need to have a list of various properties. Let's say all products in category "displays" will need to have a property "na

  • More optimized approach of Dijkstra's algorithm

    More optimized approach of Dijkstra's algorithmOctober 15

    I need a graph-search algorithm that is enough in our application of robot navigation and I chose Dijkstra's algorithm. We are given the gridmap which contains free, occupied and unknown cells where the robot is only permitted to pass through the fre

  • Scaling Values Across Breakpoints Using SassOctober 16

    Stop me if you've been here before: you're given a desktop comp for a site, then after you build it you get verbal feedback (or maybe another comp, if you're lucky) for a 'mobile view'. You then need to make the site responsive (including a couple of

  • Optimize CSS Rule DumperJanuary 28

    I have written the following Tampermonkey/Greasemonkey script. I am trying to obtain a list of all CSS rules for a page; across all stylesheets. As of now, I am just iterating over everything. Is there a faster way to obtain the rules? Also, I feel t

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.309 s.