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

type alias for 2D array generates compile time error #20715

Closed
pd-giz-dave opened this issue Feb 2, 2024 · 4 comments · Fixed by #20898
Closed

type alias for 2D array generates compile time error #20715

pd-giz-dave opened this issue Feb 2, 2024 · 4 comments · Fixed by #20898
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@pd-giz-dave
Copy link

pd-giz-dave commented Feb 2, 2024

Describe the bug

This does not compile:

module main

pub type Labels = [][]int

pub fn new_labels(width int, height int) Labels {
	//mut labels := [][]int{len: height, init: []int{len: width}}  // compiles OK
	mut labels := Labels{len: height, init: []int{len: width}}  // does not compile
	return labels
}

fn main() {
	mut labels := new_labels(2,2)
}

Reproduction Steps

Try to compile the above code.

Expected Behavior

I would expect it to compile with no error.

Current Behavior

Get compiler error:

issues/complier_error.v:6:16: error: alias type name: [][]int is not struct type
    4 | 
    5 | pub fn new_labels(width int, height int) Labels {
    6 |     mut labels := Labels{len: height, init: []int{len: width}}
      |                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    7 |     return labels
    8 | }

Possible Solution

Either fix or update documentation to say its not allowed.

Additional Information/Context

No response

V version

V 0.4.4 ac2dcc2

Environment details (OS name and version, etc.)

EndeavourOS

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.

@pd-giz-dave pd-giz-dave added the Bug This tag is applied to issues which reports bugs. label Feb 2, 2024
@GGRei
Copy link
Contributor

GGRei commented Feb 5, 2024

@pd-giz-dave because you assign at the labels var in your function a struct that does not exist ( struct assignation declaration in your example ) and you trying to return it. Moreover, return type of your function is not a struct.

It's not a bug.

@pd-giz-dave
Copy link
Author

The type alias documentation is this:
"Type aliases
To define a new type NewType as an alias for ExistingType, do type NewType = ExistingType.
This is a special case of a sum type declaration."
So is "[][]int" not a type?
I'm a newbie to V so I niaively interpreted a type alias like a C #define, ie. anywhere where ExistingType is valid so would NewType be.
My understanding is clearly wrong, but a fuller explanation in the docs would help.

@JalonSolov
Copy link
Contributor

No, you are correct, you have just run into a bug.

@felipensp
Copy link
Member

If we change array to map on Labels, we'll get:

bug.v:7:16: error: direct map alias init is not possible, use Labels(map[int]string{}) instead

So, if we want to support array init via this struct way, probably we should support map too for consistency.

@felipensp felipensp added Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants