Skip to content

gotocase: a case for useful and not harmful goto #5950

@y0shir

Description

@y0shir

While it's obvious that goto is considered harmful, sometimes it is still a usefull tool.
Because zig dosen't support switch falltrough, i propose an alternative: gotocase.
Let's consider this esample from https://wiki.c2.com/?GoTo in C

Mask *pMask(NULL); switch (type) {
case POINT:
	Mask tmpMask;
	vector<Location> &v=getVectorOfPoints();
	for (iterator it=v.begin();it!=v.end();++it)
	tmpMask.set(*it);
	pMask = &tmpMask;
	goto drawMask;
case MASK:
	pMask=getBitMask();

drawMask:
	// here's the code to draw the mask overlay
	break;

}

This obviously saves time and looks more elegant than combination of if statements and functions.
Perhaps with gotocase it could look like this:

switch (type) {
 POINT =>
	//dopointstuff
	gotocase else;
MASK =>
	pMask=getBitMask();
        gotocase else;
else =>
	// here's the code to draw the mask overlay
}

To enforce good code practices one could restrict gotocase to apear only at the end of the case and only cases that are further down.

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions