Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot assign typeid literals to variables of type Maybe(typeid) #4926

Closed
tadeohepperle opened this issue Mar 11, 2025 · 4 comments
Closed

Comments

@tadeohepperle
Copy link
Contributor

Context

        Odin:    dev-2025-02:22ab8935c
        OS:      Manjaro Linux, Linux 6.1.126-1-MANJARO
        CPU:     12th Gen Intel(R) Core(TM) i7-1260P
        RAM:     31806 MiB
        Backend: LLVM 14.0.6

Current Behavior

This should work, but does not compile:

a: typeid
a = int // ok
b: Maybe(typeid)
b = int // compile error: `Cannot assign 'int', a type, to assignment`
b = Maybe(typeid)(int) // compile error: `Cannot assign 'int', a type, to assignment`

Steps to Reproduce

main :: proc() {
// for `string` type all is good
	{
		a: string
		a = "Hello" // ok
		b: Maybe(string)
		b = "Hello" // ok
		b = Maybe(string)("Hello") // ok
	}
// for `typeid` type it does not compile:
	{
		a: typeid
		a = int // ok
		b: Maybe(typeid)
		b = int // compile error: `Cannot assign 'int', a type, to assignment`
		b = Maybe(typeid)(int) // compile error: `Cannot assign 'int', a type, to assignment`
	}
}
@Kelimion
Copy link
Member

Looks like a type inference thing.
You can get it to work like this:

b: Maybe(typeid)
b = typeid_of(int)
fmt.println("b:", b)

@tadeohepperle
Copy link
Contributor Author

Great, thanks a lot for your response! :)

@Kelimion
Copy link
Member

With that said, you probably don't need Maybe(typeid).

a: typeid
fmt.println(a == nil) // true

If you don't assign anything to the typeid, it has the value nil, but nil itself isn't a type. Maybe is useful to determine whether something was assigned to it, but for a typeid nil already serves that same function.

@laytan
Copy link
Collaborator

laytan commented Mar 21, 2025

I believe this is correct, typeid_of is intended to be used to go from a type (not a value) to a typeid (a value).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants