Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of custom values for [flag] enums #19774

Closed
2 tasks
ArtemkaKun opened this issue Nov 5, 2023 · 9 comments
Closed
2 tasks

Add support of custom values for [flag] enums #19774

ArtemkaKun opened this issue Nov 5, 2023 · 9 comments
Labels
Feature Request This issue is made to request a feature. Unit: Type System Bugs/feature requests, that are related to the V types system.

Comments

@ArtemkaKun
Copy link
Contributor

ArtemkaKun commented Nov 5, 2023

Describe the feature

Currently V will output an error if you try to mark enum, that has custom values defined for members, with [flag] attribute. V should add support for such a thing

Use Case

Usage of C libs that defines flags with custom values

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

V 0.4.2

Environment details (OS name and version, etc.)

Not related

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@ArtemkaKun ArtemkaKun added Feature Request This issue is made to request a feature. Unit: Type System Bugs/feature requests, that are related to the V types system. labels Nov 5, 2023
@JalonSolov
Copy link
Contributor

How would that work with something like

[flag]
enum Foo {
    my_enum = 237
}

?

V would have to exhaustively check every custom value to make certain they resolve to a single bit.

Not sure what it should do with enums with duplicate values... that could probably be allowed, though it could be confusing.

enum Foo {
    foo = 1
    bar = 1
    baz = 1
}

@ArtemkaKun
Copy link
Contributor Author

How would that work with something like

Idk, probably the same way it works in C

@JalonSolov
Copy link
Contributor

A flag in V is meant to be a single bit, so it doesn't match up with what C does.

@ArtemkaKun
Copy link
Contributor Author

So this issues is a proposition to change that or implement another way to match flags in C - otherwise this will be another problem with usage of C code/libs in V (at least, maybe there are other real world cases)

@JalonSolov
Copy link
Contributor

JalonSolov commented Nov 5, 2023

An enum that isn't a single bit isn't a flag, it's just an enum.

Do you have an example of C code that you're thinking about?

@ArtemkaKun
Copy link
Contributor Author

Here is an example (C code)

typedef enum TS_WindowFlags {
	TS_WindowFlagNone = 0,
	TS_WindowFlagTitle = 1,
	TS_WindowFlagClose = 2,
	TS_WindowFlagFrame = 4,
	TS_WindowFlagResize = 8,
	TS_WindowFlagMinimize = 16,
	TS_WindowFlagMaximize = 32,
	TS_WindowFlagTransient = 64,
	TS_WindowFlagFullscreen = 128,
	TS_WindowFlagTransparent = 256,
	TS_WindowFlagFileDropped = 512,
	TS_WindowFlagMultisample2 = 1024,
	TS_WindowFlagMultisample4 = 2048,
	TS_WindowFlagMultisample8 = 4096,
	TS_WindowFlagVerticalSync = 8192,
	TS_WindowFlagColorRGBAu8ns = 16384,
	TS_WindowFlagColorRGBu10Au2n = 32768,
	TS_WindowFlagColorRGBAf16 = 65536,
	TS_WindowFlagMultisample = 7168,
	TS_WindowDefaultFlags = 59,
	TS_WindowNumFlags = 17,
	TS_WindowFlags_Maxi32 = 0x7fffffff,
} TS_WindowFlags;

if(!tsWindow_create_cSWF(window, title, TS_WindowDefaultFlags | TS_WindowFlagVerticalSync) || !tsWindow_setHidden(window, 0)) return 1;

@JalonSolov
Copy link
Contributor

The last 4 could just be const values, and it would even make sense if they were const values built from the flag enums. Otherwise, how do you know which flags are in those? You have to manually convert the int's to binary, then compare which bits are set to the regular flags. I think this would be a much better way than using the example you've shown.

Except for the last one, of course... it's every bit except the sign bit.

@ArtemkaKun
Copy link
Contributor Author

But this is not my code, this is a code from C library that I must accept in my V program. I can't change it to make it compatible with current V things

@markcol
Copy link

markcol commented Apr 9, 2024

The code isn't as clear as it could be, but the values are ORed values of the flags. For example, the last few examples could be expressed more clearly as:

TS_WindowNumFlags = TS_WindowFlagTitle | TS_WindowFlagMinimize;  // 17
TS_WindowDefaultFlags =  TS_WindowFlagTitle | TS_WindowFlagClose | TS_WindowFlagResize | TS_WindowFlagMinimize | TS_WindowFlagMaximize;  // 59
TS_WindowFlagMultisample = TS_WindowFlagMultisample2 | TS_WindowFlagMultisample4 | TS_WindowFlagMultisample8; // 7168

The following code works fine (as of V 0.4.5 a1d4712):

module main

enum WindowFlags as u32 {
  @none = 0
  title = 1
  close = 2
  frame = 4
  resize = 8
  minimize = 16
  maximize = 32
  transient = 64
  fullscreen = 128
  transparent = 256
  filedropped = 512
  multisample2 = 1024
  multisample4 = 2048
  multisample8 = 4096
  verticalsync = 8192
  colorrgbau8ns = 16384
  colorrgbu10au2n = 32768
  colorrgbaf16 = 65536
  numflags = 17
  defaultflags = 59
  multisample = 7168
}

fn main() {
    println('numflags: ${int(WindowFlags.numflags)}')
    println('defaultflags: ${int(WindowFlags.defaultflags)}')
    println('multisample: ${int(WindowFlags.multisample)}')
}

Output:

$ v run main.v
numflags: 17
defaultflags: 59
multisample: 7168

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request This issue is made to request a feature. Unit: Type System Bugs/feature requests, that are related to the V types system.
Projects
None yet
Development

No branches or pull requests

3 participants