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

Can not convert array to string #766

Closed
aguspiza opened this issue Jun 28, 2019 · 5 comments
Closed

Can not convert array to string #766

aguspiza opened this issue Jun 28, 2019 · 5 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@aguspiza
Copy link
Contributor

aguspiza commented Jun 28, 2019

V version: 0.1.8
OS: ubunti 16.04

What did you do?

//os_test.v
const( ARRLEN = 16)

fn test_arrconst() {
    mut arr := [ byte(0) ; ARRLEN]
    str := tos(arr,ARRLEN)
    assert str.len == ARRLEN 
} 

What did you expect to see?
Compile and pass test

What did you see instead?
Compiler error:

pass=2 fn=`test_arrconst`
panic: os_test.v:5
Fn "tos" wrong arg #1. Expected "byte*" (s)  but got "[]byte"
@aguspiza aguspiza added the Bug This tag is applied to issues which reports bugs. label Jun 28, 2019
@aguspiza aguspiza changed the title Can convert array to string Can not convert array to string Jun 28, 2019
@athif23
Copy link

athif23 commented Jun 28, 2019

From what I know, it's clear that you need to pass a "byteptr" type to the first argument, but you pass an array of "byteptr" instead. So, I think it's not a bug. That's just how it is.

pub fn tos(s byteptr, len int) string {
	// This should never happen.
	if isnil(s) {
		panic('tos(): nil string')
	}
	return string {
		str: s
		len: len
	}
}

@aguspiza
Copy link
Contributor Author

aguspiza commented Jun 28, 2019

So:

arr := [ byte(1), 2, 3 ]         //type []byte acording to docs
arr2 := [3]byte                  //type byteptr ??
arr3 := [ byte(0) ; 3 ]          //type []byte

arr and arr3 can not be converted to string with tos() while arr2 can.

I was trying another notation to use a const for the length of the array because of #762

@medvednikov
Copy link
Member

medvednikov commented Jun 28, 2019

string() should work with all of these. Thanks, will fix.

@ntrel
Copy link
Contributor

ntrel commented Jul 10, 2019

Use arr.data. This code works:

    mut arr := [ byte(0) ; ARRLEN]
    str := tos(arr.data, ARRLEN)

string() should work with all of these

Closing this in favour of #934.

@medvednikov
Copy link
Member

Fixed:

str := string(bytes_array)

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.
Projects
None yet
Development

No branches or pull requests

4 participants