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

Error : cannot modify external-realm or non-realm object #1904

Closed
DIGIX666 opened this issue Apr 8, 2024 · 3 comments
Closed

Error : cannot modify external-realm or non-realm object #1904

DIGIX666 opened this issue Apr 8, 2024 · 3 comments

Comments

@DIGIX666
Copy link
Contributor

DIGIX666 commented Apr 8, 2024

I create a file test for my realm but I have this error test pkg: panic: cannot modifiy external-realm or non-realm object

SCR-20240408-qvih

In my code, I think I don't call other realm for modify.
Does anyone know what the problem is?

Link for my code

@deelawn
Copy link
Contributor

deelawn commented Apr 9, 2024

Hey @DIGIX666. This looks like it is expected behavior -- the test is trying to modify a package (non-realm) object. The short explanation is that modification of package (/p/) state is not allowed after package initialization. So when that realm test is run, it produces an error because it tries to add a new book to the package's state. The ideal approach for something like this is to store state in the realm by defining a type in the package that has methods to modify the state owned by the realm. Something like:

type Library struct {
    books []Book
}

func (l *Library) AddBook(id, title, author string) {
    l.books = append(l.books, Book{...})
}

Then in the realm:

var lib libPkg.Library

func AddBook(id, title, author string) string {
	lib.AddBook(id, title, author)
	return "Book added successfully"
}

So try something like that and let me know if that fixes it.

@DIGIX666
Copy link
Contributor Author

DIGIX666 commented Apr 9, 2024

SCR-20240409-sjmm-3 SCR-20240409-skrv

Thanks a lot for your explication ^^

I just changed

var lib libPkg.Library

to

var lib library.Library

@deelawn
Copy link
Contributor

deelawn commented Apr 9, 2024

No problem -- glad you got it figured out

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

No branches or pull requests

2 participants