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

Updated specification for Std.is / safe cast #7115

Closed
ncannasse opened this issue May 31, 2018 · 4 comments
Closed

Updated specification for Std.is / safe cast #7115

ncannasse opened this issue May 31, 2018 · 4 comments
Milestone

Comments

@ncannasse
Copy link
Member

ncannasse commented May 31, 2018

Given there was a disagreement over the specification of Std.is / safe cast, I'm using my dictatorship power to move forward for Haxe 4.0:

Std.is(null,T) should be false : null value has no runtime type information attached so while it's a valid value for a variable of type T it's not an object instance of T either, which is what Std.is is checking.

cast(null,T) should be null (not throw), because in that case we convert the value null to type T and it's valid value for this type. Also both (null : T) and (cast null : T) works. That's the way type casting works in C#, Java, and many other languages.

Requirements:

  • write corresponding unit tests
  • ensure platforms compliance
  • document platform changing behaviors for release notes

Additional checks:

  • null can only be caught by catch( e : Dynamic )
  • cast(null,Int|Float|Bool) : on static platforms, return the default value (same as unsafe cast). on dynamic platorms, returns null.
  • Std.is(null,Int|Float|Bool|Dynamic): should be false on all platforms as well
@Simn
Copy link
Member

Simn commented May 31, 2018

The canonical implementation of cast(v, T) is if (Std.is(v, T)) cast v else throw "Class cast error", so this makes no sense to me.

@ncannasse
Copy link
Member Author

The new one will be if (Std.is(v, T) || v == null ) cast v else throw "Class cast error"

@HaxeFoundation HaxeFoundation deleted a comment from Simn May 31, 2018
@ncannasse
Copy link
Member Author

I don't want another discussion topic, thanks

@Simn
Copy link
Member

Simn commented Oct 15, 2018

Closing in favor of #7532.

@Simn Simn closed this as completed Oct 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants