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

Null<T> #3

Open
Simn opened this issue Apr 20, 2023 · 5 comments
Open

Null<T> #3

Simn opened this issue Apr 20, 2023 · 5 comments
Assignees
Labels
TODO Big todo feature

Comments

@Simn
Copy link

Simn commented Apr 20, 2023

One of the biggest challenges when it comes to Haxe code generation is the handling of Null<T>. My experience is that it's good to get this out of the way early because otherwise it's going to come back with a vengeance.

The problem with C# Nullable is that it is constrained to struct. This means that it cannot be used with reference types, which in turn means that it cannot be "blindly" used in place of Haxe's Null<T>. Unfortunately, I don't remember what exactly the problem was with an approach that would emit Nullable only if the parameter is not a reference type.

The C# target has its own Null implementation that doesn't have the struct restriction. However, this has always caused some friction and doesn't seem ideal either.

I'm interested in knowing what the best approach is in the C# world!

@SomeRanDev
Copy link
Owner

SomeRanDev commented Apr 20, 2023

Ahh, true! I can see how this would become a headache 🤔

Unfortunately, I don't remember what exactly the problem was with an approach that would emit Nullable only if the parameter is not a reference type.

Reference types can already be assigned null in C# right? Perhaps the problem that was being solved is @:struct class types can be assigned null in Haxe(?), but not in C#. So instances are wrapped with Nullable?

But it would probably suck to just wrap all the custom value types in a reference type, removes the whole point of their optimization. Hmmmm... guess @:struct types can just throw an error if found being assigned a null during generation (without wrapping in Null<T>), but would be nice if null usage could be tracked better.

@Simn Sorry if this has been asked before, but I've been meaning to inquire: is having null-safety always "on" on the table for Haxe 5? Outside of the safety stuff, feels like it would help with optimizing generation for static targets for situations like this. But would probably break everything... so idkkk...

@SomeRanDev SomeRanDev added the TODO Big todo feature label Apr 20, 2023
@jeremyfa
Copy link
Collaborator

jeremyfa commented Apr 20, 2023

As far as I can tell, I would see the generated code like this:

  • translate to C# Nullable<T> value types (int, double, struct types...) when using Null<Int>, Null<Float>, ... on Haxe side
  • translate to original type without anything for reference types, as they are by default nullable anyway, regardless of them originating from Type or Null<Type> on Haxe side

But yes, that won't solve all cases, like if in the original Haxe code there is some generic class or function literally taking Null<T> as argument, which mean it could theoretically accept both value types and reference types as T, then, yes, in that case we might need wrap the value into some custom Nullable type...

If there is no going away from this, I guess we should try our best to use a custom Nullable type only in places where we can't do otherwise (like haxe generic Null<T>), and stick to C# Nullable value types or plain references in all other situations when possible. Can imagine that will require the exporter to be smart about that and can definitely see this as a challenge 😅. But not sure there is a better option...

@Simn
Copy link
Author

Simn commented Apr 20, 2023

Regarding default null-safety, I don't know yet. It might become a thing like DCE that is enabled by default for user-code. But at the end of the day, the problem here isn't going to go away.

@jeremyfa
Copy link
Collaborator

The C# target has its own Null implementation that doesn't have the struct restriction. However, this has always caused some friction and doesn't seem ideal either.

@Simn Do you have more details about the problems/friction you had when using that custom Null<T> implementation for C# target?

@Simn
Copy link
Author

Simn commented Apr 20, 2023

I never used it much, but from my understanding it makes native interop a mess. Maybe @kLabz has some insight, he's the expert on Haxe/C# annoyances.

@jeremyfa jeremyfa self-assigned this Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TODO Big todo feature
Projects
None yet
Development

No branches or pull requests

3 participants