Returning null handle in native
I'm learning how to write natives, and I would like to have a native return a handle. My use-case is that I would either like to return a null ArrayList handle (0) in the case that the source handle is null, and the handle from a clone of the ArrayList in the case that it is not. In the code below, when trying to cast null to an int, I am getting
Code:
error 148: cannot assign null to a non-nullable type PHP Code:
|
Re: Returning null handle in native
pretty sure that's exactly what it was designed to do, fail to coerce to types where the whole null reference thing doesn't make any sense
you can do: PHP Code:
|
Re: Returning null handle in native
Normally I would return bool for success and have a byref param.
|
Re: Returning null handle in native
PHP Code:
|
Re: Returning null handle in native
Quote:
|
Re: Returning null handle in native
Quote:
|
Re: Returning null handle in native
Quote:
|
Re: Returning null handle in native
I guess you could just return 0, I believe null is a type-safe alias for 0.
I know it doesn't really say that you are returning a null handle to someone that is looking at the code. I guess you can add a comment on the same line saying "null" to document it better. |
Re: Returning null handle in native
It's certainly an interesting behavior, unique to the typelessness of SourcePawn and how the native API works. In other languages, null could not be used in this context (doesn't make sense to cast a null pointer to something else), but in this case where a native should be able to return null but can't because null isn't castable. Null is semantically equivalent to Handle:0 (INVALID_HANDLE), but null is not syntactically equivalent.
I think my best bet is the multi-cast approach mentrioned by @Miu, even if it's a bit ugly, but the cleanest would be modifying a byref param with return value (no casting null here), but that's different syntax which I don't like |
Re: Returning null handle in native
Quote:
In my opinion, it's understandable why null can't be cast to these primitive types. EDIT: @SM Devs, could we have something like the following? PHP Code:
|
All times are GMT -4. The time now is 11:23. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.