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

Enum functions are not documented by v doc #23264

Open
Le0Developer opened this issue Dec 25, 2024 · 2 comments
Open

Enum functions are not documented by v doc #23264

Le0Developer opened this issue Dec 25, 2024 · 2 comments
Labels
Unit: Documentation Bugs/feature requests, that are related to the documentations.

Comments

@Le0Developer
Copy link
Member

Le0Developer commented Dec 25, 2024

Describe the issue

I'd expect enum functions like Enum.from(), enum.has/enum.all/enum.any to be documented by vdoc similar to array/map functions using stubs.

Links

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.

Huly®: V_0.6-21698

@Le0Developer Le0Developer added the Unit: Documentation Bugs/feature requests, that are related to the documentations. label Dec 25, 2024
@jorgeluismireles
Copy link

Enum methods appart from are prefabricated by fn (mut p Parser) enum_decl() ast.EnumDecl parser only for enums with attribute flag

@[flag]
enum File { read write execute }
fn main() {
	$for f in File.methods {
		println('${f}')
	}
}

Code above prints the details of eight methods. Another way to document them is to create an interface, say ast.EnumFlag which already is implemented so users know what we have, and documenting that enums with attr flag implements the interface:

pub interface EnumFlag
  // explaining each of eight methods...
  is_empty(...) 
  has(...)
  all(...)
  set(...)
  set_all(...)
  clear(...)
  clear_all(...)
  toggle(...)
}

@jorgeluismireles
Copy link

Seems there is a confusion about "static" and "instance" enum build-in methods. For enums with attr @[flag] two static methods are built and work:

@[flag] enum File { read write execute }
fn main() {
	assert File.from(4)!.str() == 'File{.execute}'
	assert File.zero().str() == 'File{}'
}

But I can write my instance methods from and zero and will not be detected as duplicated:

@[flag] // this adds 8 methods
enum File { read write execute }
//fn (f File) set() { } // duplicated method correctly detected so this line is commented
fn (f File) zero() string { return 'my zero' } // method 9 not detected as duplicated
fn (f File) from() string { return 'my from' } // method 10 not detected as duplicated
fn main() {
	mut methods := []string{}
	$for m in File.methods {
		methods << m.name
	}
	methods.sort()
	assert methods == ['all', 'clear', 'clear_all', 'from', 'has', 'is_empty', 'set', 'set_all', 'toggle', 'zero']
	assert File.read.has(File.read | File.write) == true
	assert File.read.zero() == 'my zero'
	assert File.read.from() == 'my from'
}

https://play.vlang.io/p/5d41326dd4 V 0.4.9 7b9b3dd

Please notice I cannot duplicate method set since will be detected as duplicated, so far so good...

The confusion I am talking about comes from inspecting the Parser function fn (mut p Parser) fn_decl() ast.FnDecl { namely this section:

	if is_method {
			mut is_duplicate := type_sym.has_method(name)
			// make sure this is a normal method and not an interface method
			if type_sym.kind == .interface && is_duplicate {
				if mut type_sym.info is ast.Interface {
					// if the method is in info then its an interface method
					is_duplicate = !type_sym.info.has_method(name)
				}
			}
			if is_duplicate {
				if type_sym.kind == .enum
					&& name in ['is_empty', 'has', 'all', 'set', 'set_all', 'clear', 'clear_all', 'toggle', 'zero', 'from'] {

where a list of ten methods (including zero and from) are used to apparently detect a duplication. Since my code duplicated zero and from and were accepted seems the parser code above is not detecting a duplication but in the enum function I mentioned before or in another part. In fact we can have "duplicated" methods when one is static and the other don't.

Anyway this could be an issue to check but the important part is we need documentated the methods build-in for attribute @[flag] off and on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Unit: Documentation Bugs/feature requests, that are related to the documentations.
Projects
None yet
Development

No branches or pull requests

2 participants