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 use a constant to set the size of a byte array #762

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

Can not use a constant to set the size of a byte array #762

aguspiza opened this issue Jun 28, 2019 · 4 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.6
OS: linux
**C compiler version: gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.11) **

What did you do?

const( Arrlen = 16)

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

What did you expect to see?
I expected to compile and pass the test

What did you see instead?
Compiler error:

test_arrconst
0
pass=2 fn=test_arrconst
panic: os_test.v:6
undefined: u8

@aguspiza aguspiza added the Bug This tag is applied to issues which reports bugs. label Jun 28, 2019
@ntrel
Copy link
Contributor

ntrel commented Jul 10, 2019

tos takes a byteptr, not a []byte:

pub fn tos(s byteptr, len int) string {

This code works:

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

//mut arr := [16]byte
//mut arr := [Arrlen]u8

See #1062 for fixed size arrays. Closing this in favour of that one as it already contains discussion.

@ntrel
Copy link
Contributor

ntrel commented Aug 1, 2019

Note: Go does support using a compile-time constant for fixed-size arrays:
https://play.golang.org/p/3t1y4TzFieN

@medvednikov
Copy link
Member

str := tos(arr.data, Arrlen)

This is a better way: str := string(arr)

tos will be made private soon, just like tos2 was.

@medvednikov
Copy link
Member

@aguspiza Fixed.

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

3 participants