Why is using a proc type to define an implementation not allowed?

My_Sign :: #type proc(some: int, complex: complex64, arguments: any)

my_simple_proc :: My_Sign {
	// this is not allowed
}

I imagine it’s disallowed for a reason. Does anyone know?

1 Like

Because that’s syntactically ambiguous to parse. From a syntax stance, how does it know if My_Sign { } is a struct literal or not? Odin is also a context-free grammar meaning that it cannot know anything like My_Sign is a procedure.


Now to answer why Odin doesn’t support such a construct (and I already know the precise syntax I would do to support this too). The macro trick of making the typedef and usage is nice in C to have different instantiations of the same type is useful but it’s just that: a trick.

Odin does have a different feature, unlike C, which allows to check if the type is correct, and it’ll be obvious once I show it.

my_simple_proc : My_Sign : proc(some: int, complex: complex64, arguments: any) {
    ....
}

Note what it is doing is that you are giving my_simple_proc an explicit type between the two colons, and then it is checking to see if the expression to the right of the colons is the coercible to the specified type. Normally people declare procedures with just :: so that the type is implicitly defined.

Having to write the signature manually again also makes it a lot easier to read from a local standpoint since you can directly see what the parameters are (names and types), which with the C macro trick, you have to go back to the macro to see what it actually is.

8 Likes

I actually prefer the explicit declaration for this. I only wanted to do the ‘trick’ because manually checking the types to match seemed like something I’d screw up but this solves that so I have no complaints. Thanks!

3 Likes