Assign null or reset variable


I’m interested in running something like the following:



if(!{ = nullptr;}

The issue I’m having is is not a shared_ptr of the type, but is being treated as a reference or something, so I cannot call .reset() on it even after registering the following:

a_script.add(chaiscript::fun([](std::shared_ptr<Creature> &a_self) {
}), "reset");

Is there a way in chaiscript to make is_var_null true again once you’ve assigned to the variable?


There is not, currently, any way to reset a variable. Although, I would generally expect the version you show to work.

There’s a couple of questions at play:

  1. Do you want to set a typed value to null?
  2. Or do you want to reset the ChaiScript variable all the way back to being undefined?


I want to assign null, but for my use case it would be functionally equivalent either way. Number 2 would allow you to essentially “change” the type of a variable which I don’t think is necessary and that’s why I say “assign null”.

I tried reference assigning nullptr (after binding it in c++ with add_global) but got a type mismatch exception.

So far I’m stumped.


Some small examples:


	a_script.add(chaiscript::fun([]() {
		return std::make_shared<int>(1);
	}), "GetSharedInt");

	a_script.add(chaiscript::fun([](std::shared_ptr<int> &a_self) {
	}), "reset");


auto testVariable;
	testVariable = GetSharedInt();


	print("This is expected");


Error: “Error with function dispatch for function ‘reset’” With parameters: (int).()


I just went on a run to think this over - I think the issue is with a non-const & to shared_ptr<> in my boxed_cast mechanism.

I’ll look into it as soon as I can. Possibly later today.



I can already confirm that that is in fact the issue.

Are you operating on the 5.8.1 release? Or off of develop? Or something else?


I’m on 5.8.1 downloaded the zip off the site.

I think getting .reset working is important as a general bug fix. But also think assignment to null would make a lot of sense as a general language feature. Since chaiscript has built in null checks via “is_var_null” I would also intuitively expect to be able to do:

MyVariable = null;


if(MyVariable == null)

Anyway, pretty impressed with this language so far, and thank you for your responsiveness!!


The problem I see right now is that I make assumptions that the pointed-to memory location never changes. And I cache the pointers and use that to optimize some aspects.

I’m not sure if I can change that assumption without having a bad impact on performance.

I’ll need to handle this in a way that doesn’t affect the normal case, but still allows you to reset a pointer, and possibly reset the entire object.


“There are only two hard things in Computer Science: cache invalidation and naming things.”

– Phil Karlton


Check out this commit:

Let me know if that version works for you. You should be able to download it from here:



Still having a problem with .reset()

I downloaded that and tried it out.

“Error with function dispatch for function ‘reset’” with parameters: (int).()

As an aside: I’d love to be able to use null assignment instead of .reset, but that’s more of a “nice to have” as this other method would be a viable work-around. The null assignment is more generic and more useful for types in the future (if I’m downloading a script I don’t want to have to have the foresight to expose a null assign to every single type of shared_ptr, and I’m not sure about bare pointers)


Hmm, that’s quite strange.

testVariable.reset(); // fails to dispatch
reset(testVariable); // works


OK, issue found and fixed. Give it another try with the latest from that branch.



I’ll give it a go later today or possibly tomorrow and report back!


Seems to work now!

Also I used to have some issues with min/max defines before which required me to change something in chaiscript (I just manually undef’d min and max and got rid of the pragma pop macro stuff) But I didn’t need to this time, I’m guessing you fixed that too (or my compiler is just behaving nicer now).