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

checker: check generic sumtype declaration (fix #18741) #18865

Merged
merged 2 commits into from Jul 17, 2023

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Jul 14, 2023

This PR check generic sumtype declaration (fix #18741).

  • Check generic sumtype declaration.
  • Add test.
type Fn[T] = fn (string) T

type Fnn = fn (string) string

type Parser = Fn | Fnn

fn main() {
	println('hello')
}

PS D:\Test\v\tt1> v run .
tt1.v:5:15: error: generic fntype `Fn` must specify generic type names, e.g. Fn[T]
    3 | type Fnn = fn (string) string
    4 | 
    5 | type Parser = Fn | Fnn
      |               ~~
    6 | 
    7 | fn main() {
type Fn[T] = fn (string) T

type Fnn = fn (string) string

type Parser[T] = Fn[T] | Fnn

fn f(x string) string { return '>> f $x' }

fn main() {
    _ := Parser(f)
}

PS D:\Test\v\tt1> v run .
tt1.v:10:10: error: generic sumtype `Parser` must specify type parameter, e.g. Parser[int]
    8 | 
    9 | fn main() {
   10 |     _ := Parser(f)
      |          ~~~~~~~~~
   11 | }

@spytheman
Copy link
Member

type Fn[T] = fn (string) T

type Fnn = fn (string) string

type Parser[T] = Fn[T] | Fnn

fn f(x string) string { return '>> f $x' }

fn main() {
    _ := Parser(f)
}

has a cgen error too.

_ := Parser[string](f) does compile however.

Imho the Parser(f) expression should either produce a checker error that the type can not be inferred, or it should infer it by the type of f, i.e. that it is string.

I am not sure what will be more maintainable, since in this case both Fn[T] and Fnn are compatible with the type signature for f . Perhaps it would be just easier/simpler to require the type name explicitly.

@spytheman spytheman merged commit 39cfaaf into vlang:master Jul 17, 2023
47 of 48 checks passed
@yuyi98 yuyi98 deleted the check_generic_sumtype_decl branch July 18, 2023 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sum type can't work with generic function type
2 participants