#`using` like in Odin or jai

1 messages · Page 1 of 1 (latest)

tough gale
#

odin and jai have a using keyword which helps a lot with nested structs and stuff like that. Zig has usingnamespace which is similar but I noticed that it's a lot less flexible. i was wondering if theres a suggestion/issue on adding a odin/jai styled using keyword or evolving usingnamespace into that and i was also just wondering what people think of this in general

echo quartz
#

usingnamespace used to have that ability but it was intentionally removed, that kind of global namespacing was seen as an anti-pattern

tough gale
#

im not talking about global namespacing specifically

#
Entity :: struct {
    using position: Vector3,
    orientation: quaternion128,
}
foo :: proc(entity: ^Entity) {
    fmt.println(entity.x, entity.y, entity.z)
}
#

more stuff like this

#

its not the same as usingnamespace because it actually includes the fields too

#

and just being able to nest structs that way in general

echo quartz
#

you can do (a limited version of that) with comptime, unless theres something special it also does other than copying the fields over

tough gale
#
Entity = struct {
  using position: Vector3
}

const ent = Entity{}
ent.position.x
ent.x
#

lets you do this pretty much

#

so its still like a field

#

but the fields of position are also available from entity

#

its not the best example but i hope it gets the point across

#

ive already had cases where this type of functionality would improve my code

#

so i was kinda bummed about it not being a thing

echo quartz
#

i dont think ive seen anyone propose that, i havent used either of those languages tho so i dont really know where id use that 😅 other than just making field access shorter

tough gale
#

thats basically what its for yeah

echo quartz
#

hmm afaik they arent accepting any large language proposals currently, but its a cool feature

tough gale
#

in odin you can also use the using keyword with function parameters to spill the fields into the function namespace too

#

so if you have struct methods you can do using on them be able to access self fields easily too

#

very nice feature

fickle notch
tough gale
#

do you reckon it makes sense to make a proposal like this?

#

im not sure about it because part of it does contain behavior that was previously removed

fickle notch
#

No

#

Because that

charred valve
high thunder
#

Explicit field aliasing is a bit different from the behaviour of using in Odin and Jai. By the way, using in Odin and Jai have different semantics too.
Odin's using is equivalent to doing #as using in Jai (if I understand it correctly since I have never used it). #as in Jai is equivalent to Odin's #subtype. And this is only in the context of struct fields too.

In Odin, using "merges" the field scopes of the two types, so everything within the scope of using field is available in the scope that using was declared. There is also the extra aspect of using is that it implies a subtype polymorphic relationship too. e.g.

Foo :: struct {using b: Bar}

f: Foo = ...
b: Bar = f // equivalent to `b: Bar = f.b`

Zig's philosophy and Odin's philosophy on things are quite different. I have said this before with Andrew and I usually phrase Zig as "maximizing explicitness" whilst Odin is "minimizing implicitness" [ish]. So for Zig, using on fields would be very odd and not really that great. The only benefit would be for interfacing with C code that has embedded record types, which is also a huge aspect of it in Odin too.

I do wonder how Zig should tackle the implicit embedded record types with @cImport et al, but I am not Andrew and I don't know what his vision is for the language.

#

P.S. using as a statement (rather than as a field modifier) is mostly a mistake and I wish I went with the original approach that Pascal did with with. There is a reason we banned using at the file scope in Odin because it's absolutely dreadful to read, maintain, and refactor code that used it. It's less of a problem with a procedure scope, but still absolutely dreadful and should only be used temporarily during a code refactor. In this case, using is close (not the same semantically) as usingnamespace in Zig. Zig has different functionality that does partially make usingnamespace necessary sometimes (even if I think it's probably not worth it whatsoever for different reasons I do not want to get into).

#

P.P.S. Odin's using also works on pointers too, meaning you can access a "live" pointer too. This can be very useful for vtables if you need to implement inheritance, especially COM APIs. The way that Zig does it is to wrap the methods with a Zig method instead. Since Odin does not have method, using + -> is how it achieves the same approach.

fickle notch
high thunder
fickle notch
#

oh anon iirc