diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 73b0ba69f183c4..56ba06216d8700 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -341,11 +341,11 @@ mut: b []Test2 } -fn (t Test2) str() string { +pub fn (t Test2) str() string { return '{$t.one $t.two}' } -fn (t Test) str() string { +pub fn (t Test) str() string { return '{$t.a $t.b}' } diff --git a/vlib/compiler/cflags.v b/vlib/compiler/cflags.v index cc0dae78c7a315..0df0cdcf178103 100644 --- a/vlib/compiler/cflags.v +++ b/vlib/compiler/cflags.v @@ -14,7 +14,7 @@ struct CFlag{ value string // eg. /path/to/include } -fn (c &CFlag) str() string { +pub fn (c &CFlag) str() string { return 'CFlag{ name: "$c.name" value: "$c.value" mod: "$c.mod" os: "$c.os" }' } diff --git a/vlib/compiler/compile_errors.v b/vlib/compiler/compile_errors.v index b095a87b2f57e4..a14ac775064d79 100644 --- a/vlib/compiler/compile_errors.v +++ b/vlib/compiler/compile_errors.v @@ -199,7 +199,7 @@ mut: last_nl_pos int } -fn (s ScannerPos) str() string { +pub fn (s ScannerPos) str() string { return 'ScannerPos{ ${s.pos:5d} , ${s.line_nr:5d} , ${s.last_nl_pos:5d} }' } diff --git a/vlib/compiler/comptime.v b/vlib/compiler/comptime.v index 69ef51d4770e9e..abcc0e0ddc378c 100644 --- a/vlib/compiler/comptime.v +++ b/vlib/compiler/comptime.v @@ -314,7 +314,7 @@ fn (p mut Parser) gen_array_str(typ Type) { p.error('cant print ${elm_type}[], unhandled print of ${elm_type}') } p.v.vgen_buf.writeln(' -fn (a $typ.name) str() string { +pub fn (a $typ.name) str() string { mut sb := strings.new_builder(a.len * 3) sb.write("[") for i, elm in a { @@ -342,7 +342,7 @@ fn (p mut Parser) gen_struct_str(typ Type) { }) mut sb := strings.new_builder(typ.fields.len * 20) - sb.writeln('fn (a $typ.name) str() string {\nreturn') + sb.writeln('pub fn (a $typ.name) str() string {\nreturn') sb.writeln("'{") for field in typ.fields { sb.writeln('\t$field.name: $' + 'a.${field.name}') @@ -366,7 +366,7 @@ fn (p mut Parser) gen_varg_str(typ Type) { p.gen_struct_str(elm_type2) } p.v.vgen_buf.writeln(' -fn (a $typ.name) str() string { +pub fn (a $typ.name) str() string { mut sb := strings.new_builder(a.len * 3) sb.write("[") for i, elm in a { diff --git a/vlib/compiler/fn.v b/vlib/compiler/fn.v index 6c645deb954e85..e09b5f7d837f12 100644 --- a/vlib/compiler/fn.v +++ b/vlib/compiler/fn.v @@ -60,7 +60,7 @@ const ( MainFn = Fn{ name: 'main' } ) -fn (a []TypeInst) str() string { +pub fn (a []TypeInst) str() string { mut r := []string for t in a { mut s := ' | ' @@ -254,7 +254,7 @@ fn (p mut Parser) fn_decl() { } // Don't allow modifying types from a different module if !p.first_pass() && !p.builtin_mod && t.mod != p.mod && - !p.is_vgen // allow .str() + !p.is_vgen // let vgen define methods like .str() on types defined in other modules { //println('T.mod=$T.mod') //println('p.mod=$p.mod') @@ -297,6 +297,10 @@ fn (p mut Parser) fn_decl() { if f.name == 'init' && !f.is_method && f.is_public && !p.is_vh { p.error('init function cannot be public') } + // .str() methods + if f.is_method && f.name == 'str' && !f.is_public { + p.error('.str() methods must be declared as public') + } // C function header def? (fn C.NSMakeRect(int,int,int,int)) is_c := f.name == 'C' && p.tok == .dot // Just fn signature? only builtin.v + default build mode @@ -726,7 +730,7 @@ fn (p mut Parser) fn_call(f mut Fn, method_ph int, receiver_var, receiver_type s if f.is_deprecated { p.warn('$f.name is deprecated') } - if !f.is_public && !f.is_c && !p.pref.is_test && !f.is_interface && f.mod != p.mod { + if !f.is_public && !f.is_c && !p.pref.is_test && !f.is_interface && f.mod != p.mod { if f.name == 'contains' { println('use `value in numbers` instead of `numbers.contains(value)`') } diff --git a/vlib/compiler/table.v b/vlib/compiler/table.v index 8552ec54939f90..3c0b8383795498 100644 --- a/vlib/compiler/table.v +++ b/vlib/compiler/table.v @@ -122,7 +122,7 @@ struct TypeNode { /* // For debugging types -fn (t Type) str() string { +pub fn (t Type) str() string { mut s := 'type "$t.name" {' if t.fields.len > 0 { // s += '\n $t.fields.len fields:\n' @@ -185,7 +185,7 @@ const ( ) // This is used for debugging only -fn (f Fn) str() string { +pub fn (f Fn) str() string { t := Table{} str_args := f.str_args(t) return '${f.name}($str_args) $f.typ' diff --git a/vlib/compiler/tests/fn_expecting_ref_but_returning_struct_test.v b/vlib/compiler/tests/fn_expecting_ref_but_returning_struct_test.v index 9e8d4727d37c2f..f9764586869435 100644 --- a/vlib/compiler/tests/fn_expecting_ref_but_returning_struct_test.v +++ b/vlib/compiler/tests/fn_expecting_ref_but_returning_struct_test.v @@ -1,6 +1,6 @@ struct Foo { } -fn (f Foo) str() string { return 'Foo{}' } +pub fn (f Foo) str() string { return 'Foo{}' } fn process_foo(foo &Foo) { println('>process_foo, called for ${foo} === ${*foo}') diff --git a/vlib/compiler/token.v b/vlib/compiler/token.v index ef2f15dc77a2c8..d8bfb4500eeb5f 100644 --- a/vlib/compiler/token.v +++ b/vlib/compiler/token.v @@ -257,7 +257,7 @@ fn is_key(key string) bool { return int(key_to_token(key)) > 0 } -fn (t TokenKind) str() string { +pub fn (t TokenKind) str() string { return TokenStr[int(t)] } @@ -290,7 +290,7 @@ fn (t []TokenKind) contains(val TokenKind) bool { return false } -fn (t Token) str() string { +pub fn (t Token) str() string { if t.tok == .number { return t.lit diff --git a/vlib/glm/glm.v b/vlib/glm/glm.v index 6bbd46d192386c..dfbe561c0e47e1 100644 --- a/vlib/glm/glm.v +++ b/vlib/glm/glm.v @@ -49,15 +49,15 @@ fn mat4(f &f32) Mat4 { return res } -fn (v Vec3) str() string { +pub fn (v Vec3) str() string { return 'Vec3{ $v.x, $v.y, $v.z }' } -fn (v Vec2) str() string { +pub fn (v Vec2) str() string { return 'Vec3{ $v.x, $v.y }' } -fn (m Mat4) str() string { +pub fn (m Mat4) str() string { mut s := '[ ' for i := 0; i < 4; i++ { if i != 0 {