More about Unions in Vala

In a previous post I speculated about adding tagged unions to Vala. Let’s do that again.

In order to support the C usecase, Vala would need to include 2 types of tagged unions, a legacy one, and an idiomatic one. The legacy should look like the C version of a tagged union:

struct tagged {
    short tag;
    union variants {
        string text;
        int num;
    }
} tagged;

This is needed to bind the C libs out there. The type of the tag should be something that can be easily compared IMO (numeric datatypes, C enums, and string). The binding would then look like this:

[CCode (cname = "tagged", union_tag_field = "tag" union_tag_type = "short" union_field = "variants")]
union Tagged {
    [CCode (cname = "text", union_tag_id = "1")]
    TEXT(string),
    [CCode (cname = "num", union_tag_id = "2")]
    NUM(int)
}

The idiomatic ones, however, would actually look like a Rust struct, so if we declare:

public union OrderStatus {
	ACCEPTED,
	CANCELLED {string reason, string cancelled_by},
	REJECTED {string reason},
	COMPLETED {DateTime completed_at},
	ON_HOLD {string reason, Datetime until}
}

We should get:

enum OrderStatusTag {
	ACCEPTED,
	CANCELLED,
	REJECTED,
	COMPLETED,
	ON_HOLD
} OrderStatusTag

union order_status {
    struct accepted {OrderStatusTag tag};
    struct cancelled {OrderStatusTag tag; string reason, string cancelled_by};
    struct rejected {OrderStatusTag tag; string reason};
    struct completed {OrderStatusTag tag; GDateTime completed_at};
    struct on_hold {OrderStatusTag tag; string reason; GDatetime until};
} OrderStatus;

Fun things to support: GVariant variant types (unboxing, serialization, etc.), GValue, JSON representations.

Advertisements

Replay Edits

Being in the zone is amazing: your productivity skyrockets and the code just flows. Then somebody micromanages talks to you, the boss starts to scream discuss things on the phone, or it’s lunch time and this company won’t give back any minute you waste before starting to eat. Since this hapenned to me at least once, I’ve been looking for a simple way to get back my productivity when I resume my job. I’ve found that the best way for me is just doing insane amounts of Ctrl+Z and Ctrl+Shift+Z/Ctrl+Y to see what I did and where did I did it. But this is a tiresome activity, the kind that I expect to be automated. So why not add a simple option in your text editor, a nice Replay Edits that’ll just play in real time edits from last saved state to current state? I want this, and if you know something like this, PLEASE tell me, I’m all ears.