umka-lang icon indicating copy to clipboard operation
umka-lang copied to clipboard

`sizeof` doesn't accept types other than an unqualified type identifier

Open vtereshkov opened this issue 7 months ago • 7 comments

import "std.um"

fn main() {
    printf("%llv\n", sizeof(std::Err))  // Type cast or composite literal or enumeration constant expected
}

vtereshkov avatar Apr 26 '25 20:04 vtereshkov

Not only that:

sizeof([5]int)
sizeof([]int)
sizeof(map[str]int)
sizeof(^int) // Not necessary but for consistency
sizeof(struct {x, y, z: int})
sizeof(fn (a: int): int)
sizeof(enum {a; b; c})
sizeof(interface {})

ske2004 avatar Apr 27 '25 17:04 ske2004

@ske2004 Ah, you're right! It doesn't accept anything except an unqualified type name, because otherwise it would be hard to distinguish a type from an expression (an infinite lookup needed?)

The workaround is simple, though:

import "std.um"

fn main() {
    type E = std::Err
    printf("%llv\n", sizeof(E))
}

vtereshkov avatar Apr 27 '25 19:04 vtereshkov

@vtereshkov can types be expressions?

ske2004 avatar Apr 27 '25 20:04 ske2004

@ske2004 No, they cannot. Of what type could they be? Type type?

vtereshkov avatar Apr 27 '25 21:04 vtereshkov

@vtereshkov Unironically yes

ske2004 avatar Apr 27 '25 21:04 ske2004

@ske2004 We discussed this idea on Discord 1.5 years ago and were not very excited:

https://discord.com/channels/846663478873030666/846663478873030672/1140395255322136719

vtereshkov avatar Apr 27 '25 23:04 vtereshkov

@vtereshkov This would an implementation detail. Something like std::Err would be of type type, that can be passed to builtins. It's more so to unify the parsing of types and normal expressions.

Type inference and extraction is somewhat unrelated.

ske2004 avatar Apr 30 '25 23:04 ske2004