odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.17k stars 550 forks source link

`%w` in `fmt` is undocumented #3702

Closed Feoramund closed 1 month ago

Feoramund commented 1 month ago

I found this undocumented verb while delving through fmt earlier.

I used this program to get a sense for what it does, compared to %v:

package main

import "core:fmt"

main :: proc() {
    a: int
    b: matrix[2,2]f32
    c: quaternion256
    d: enum {A,B}
    e: struct { a: int }
    f: [3]int
    g: [3]string
    h:= type_info_of(typeid_of(int))
    i: bit_set[0..<32; u32]
    j: bit_field int {
        a: int | 4,
        b: int | 4,
    }
    k: map[string]u8
    k["hellope"] =5
    l: [dynamic]u8
    append(&l, 25)
    m:= #location()

    fmt.println("[ %w ]\n")

    fmt.printfln(
        "%w\n%w\n%w\n%w\n%w\n%w\n%w\n%w\n%w\n%w\n%w\n%w\n%w",
    a, b, c, d, e, f, g, h, i, j, k, l, m)

    fmt.println("\n[ %v ]\n")

    fmt.printfln(
        "%v\n%v\n%v\n%v\n%v\n%v\n%v\n%v\n%v\n%v\n%v\n%v\n%v",
    a, b, c, d, e, f, g, h, i, j, k, l, m)
[ %w ]

0
{0, 0; 0, 0}
0+0i+0j+0k
.A
{a = 0}
{0, 0, 0}
{"", "", ""}
int
bit_set[0..=31; u32]{}
{a = 0, b = 0}
{"hellope"=5}
{25}
Source_Code_Location{file_path = "/tmp/odin/w.odin", line = 23, column = 6, procedure = "main"}

[ %v ]

0
matrix[0, 0; 0, 0]
0+0i+0j+0k
A
{a = 0}
[0, 0, 0]
["", "", ""]
int
bit_set[0..=31; u32]{}
bit_field{a = 0, b = 0}
map[hellope=5]
[25]
/tmp/odin/w.odin(23:6)

My first assumption was that this was supposed to be a Python-like way of printing things, based on the usage of braces, but then I noticed it's printing arrays like {0, 0, 0}. Would anyone know what the intent behind the w verb is?

Kelimion commented 1 month ago

It's intended to be (but undocumented because unfinished) a way to print things in Odin-like source representation so that for simple enough things you could use this to generate a table in an .odin file and use as-is.

Kelimion commented 1 month ago

I found this undocumented verb while delving through fmt earlier.

This was amusing in a good way. Core contributors tend to have been here. Delving through core:fmt, as one does.