Raised This Month: $12 Target: $400
 3% 

Returning null handle in native


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Tirant
Veteran Member
Join Date: Jul 2008
Location: Los Angeles, California
Old 05-28-2016 , 19:24   Returning null handle in native
Reply With Quote #1

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());

Tirant is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 05-28-2016 , 20:55   Re: Returning null handle in native
Reply With Quote #2

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)) 
Miu is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 05-28-2016 , 21:09   Re: Returning null handle in native
Reply With Quote #3

Normally I would return bool for success and have a byref param.
__________________
Neuro Toxin is offline
sdz
Senior Member
Join Date: Feb 2012
Old 05-29-2016 , 15:21   Re: Returning null handle in native
Reply With Quote #4

PHP Code:
return _:INVALID_HANDLE
sdz is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 05-29-2016 , 20:55   Re: Returning null handle in native
Reply With Quote #5

Quote:
Originally Posted by EasSidezz View Post
PHP Code:
return _:INVALID_HANDLE
That is old syntax.
__________________
ddhoward is offline
sdz
Senior Member
Join Date: Feb 2012
Old 05-30-2016 , 00:25   Re: Returning null handle in native
Reply With Quote #6

Quote:
Originally Posted by ddhoward View Post
That is old syntax.
But it's still used.
sdz is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 05-30-2016 , 00:56   Re: Returning null handle in native
Reply With Quote #7

Quote:
Originally Posted by EasSidezz View Post
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.
__________________

Last edited by ddhoward; 05-30-2016 at 01:07.
ddhoward is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 05-30-2016 , 06:21   Re: Returning null handle in native
Reply With Quote #8

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.

Last edited by klippy; 05-30-2016 at 06:24.
klippy is offline
Tirant
Veteran Member
Join Date: Jul 2008
Location: Los Angeles, California
Old 05-30-2016 , 06:41   Re: Returning null handle in native
Reply With Quote #9

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

Last edited by Tirant; 05-30-2016 at 06:42.
Tirant is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 05-30-2016 , 07:36   Re: Returning null handle in native
Reply With Quote #10

Quote:
Originally Posted by Tirant View Post
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.

Last edited by klippy; 05-30-2016 at 08:18.
klippy is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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