AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Returning null handle in native (https://forums.alliedmods.net/showthread.php?t=283246)

Tirant 05-28-2016 19:24

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
However, the documentation dictates that null is just another representation for INVALID_HANDLE, and replacing null with INVALID_HANDLE resolves the behavior. Is this a bug? I think being able to use null in this context is syntactic sugar. Is there some way to use null instead?

PHP Code:

public int Native_GetExtensions(Handle pluginnumParams) {
  
ValidateNativeParams(0numParams);
  if (
g_aExts == null) {
    return 
view_as<int>(null);
  }

  return 
view_as<int>(g_aExts.Clone());



Miu 05-28-2016 20:55

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:

view_as<int>(view_as<Handle>(null)) 


Neuro Toxin 05-28-2016 21:09

Re: Returning null handle in native
 
Normally I would return bool for success and have a byref param.

sdz 05-29-2016 15:21

Re: Returning null handle in native
 
PHP Code:

return _:INVALID_HANDLE


ddhoward 05-29-2016 20:55

Re: Returning null handle in native
 
Quote:

Originally Posted by EasSidezz (Post 2423061)
PHP Code:

return _:INVALID_HANDLE


That is old syntax.

sdz 05-30-2016 00:25

Re: Returning null handle in native
 
Quote:

Originally Posted by ddhoward (Post 2423105)
That is old syntax.

But it's still used.

ddhoward 05-30-2016 00:56

Re: Returning null handle in native
 
Quote:

Originally Posted by EasSidezz (Post 2423130)
But it's still used.

  • All of the previous code in this thread is new-syntax, so you can assume that we're dealing with new-syntax here.
  • If the code has #pragma newdecls required at the top then the plugin will not compile if old-syntax was used anywhere.
  • We should avoid using the old syntax whenever possible.

klippy 05-30-2016 06:21

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.

Tirant 05-30-2016 06:41

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

klippy 05-30-2016 07:36

Re: Returning null handle in native
 
Quote:

Originally Posted by Tirant (Post 2423192)
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.

That's not really true. You can look at it from the C# perspective: you can null reference types, but not value types. Here in the new SourcePawn API, you can look at integers, floats, characters and booleans (and arrays?) as value types, and handles as reference types. When you assign one handle to another, you are not copying the object but rather copying the reference, so both handles point to the same object.
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:

typeset NativeCall
{
    function 
int(Handle pluginint numParams),
    function 
Handle(Handle pluginint numParams)
}; 

So people can choose to either return a value of primitive type or of Handle type. Handles are just cells(ints) after all.


All times are GMT -4. The time now is 11:23.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.