New to Odin and playing with some code that I have questions about

Hello,

New to odin. Long time pre-standard C dev mostly in deep embedded.

I was interested in understanding the type signature in some example code and grabbed the whole repo to play with locally. It appears that a recent addition has broken the example, so I brute-forced it into working. I understand that this repo is pretty old and things may have changed, but it went from looking pretty slick - to looking pretty ugly.

Is there a better way to construct this example so that it tickles that elegant part of my brain again? or do I need to recalibrate my expectations?

repo: odin-color
original example: readme-image.odin

What I brute forced:

#+feature using-stmt
package main

import color ".."
import "core:fmt"
import "base:runtime"

main :: proc() {
    using color
    fg_colors := [?]proc(string, runtime.Allocator)->string {
        black, red, green, yellow, blue, magenta, cyan, white,
        bright_black, bright_red, bright_green, bright_yellow, bright_blue, bright_magenta, bright_cyan, bright_white,
    }
    bg_colors := [?]proc(string, runtime.Allocator)->string {
        on_black, on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white,
        on_bright_black, on_bright_red, on_bright_green, on_bright_yellow, on_bright_blue, on_bright_magenta, on_bright_cyan, on_bright_white,
    }
    for bg in bg_colors {
        for fg in fg_colors {
            fmt.printf("%s ", bg(fg("odin", context.allocator), context.allocator))
        }
        fmt.println()
    }
}

Also, is there a way to preview a post before creating?

The first thing is that the using statement (outside of struct embedding) needs the feature flag now, because it has the potential to cause confusion (with all the identifiers seeming coming from nowhere). As for how the original repo worked while leaving out a parameter type in the array declarations, I don’t know.

You could define e.g. Color_Proc :: proc(string, runtime.Allocator) -> string, and then fg_colors := [?]Color_Proc {...}, likewise for bg_colors. Maybe you could even make the allocator have a default argument like in the color package and avoid specifying the context allocator twice in the print.

When replying it does have a preview, no clue about posts though…

1 Like

Yes. When you draft a post/reply, there should be a “>>” button in the bottom right corner that will show/hide the preview window.

Having to explicitly pass in the context.allocator is what stuck out to me. I guess that’s just a byproduct of doing the array of function calls - the elegance of automatically ‘carrying in’ the default parameter is lost at that point.

I think the function signature you want is

black :: proc(s: string, allocator := context.allocator)
->string

// or 

black :: proc(s: string, allocator := context.temp_allocator)
->string

Then in your main code you can probably change the signature to

[?]proc(string) -> string

Or similar :slight_smile:

2 Likes

You’re right! That’s exactly the trick I was looking for.

Thanks for the help!

(also, no ‘>>’ anywhere)

1 Like

1 Like

It has now just shown up for the first time on this very message. :+1:
Maybe I’ve finally exceeded the new user threshold.