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

untyped magic #33

Closed
mockey opened this issue Aug 15, 2016 · 17 comments
Closed

untyped magic #33

mockey opened this issue Aug 15, 2016 · 17 comments

Comments

@mockey
Copy link

mockey commented Aug 15, 2016

As I wrote before, I remember @Simn was not so happy with untyped __php__ and the like.
@Simn: Are there better ways to handle this, maybe already implemented in the newer generators (python, lua)?

@RealyUniqueName
Copy link
Owner

JS target uses the same approach: untyped __js__('alert(1)')

@Simn
Copy link

Simn commented Aug 15, 2016

untyped is bad for various reasons. It would be better to hide this as Lib.code('') or something to that extent.

@RealyUniqueName
Copy link
Owner

So should new generator support untyped or implement somethind like Lib.code() instead?

@Simn
Copy link

Simn commented Aug 15, 2016

I guess we should still support untyped __php__ but encourage the alternative.

@RealyUniqueName
Copy link
Owner

So what's the approved alternative? )

@Simn
Copy link

Simn commented Aug 15, 2016

I don't really have a strong opinion about that, any normal field call will do.

@mockey
Copy link
Author

mockey commented Aug 15, 2016

I think php.Lib.code is short and clear.
Can you use php as PHP namespace BTW?

@mockey
Copy link
Author

mockey commented Aug 15, 2016

Additionally php.Lib.call maybe var should be done differently, physeq is not needed I think. At least it wasn't in the old phpgen, don't know how you handle equality.

@nadako
Copy link

nadako commented Aug 15, 2016

In python we have python.Syntax and there's stuff for generating python expressions.

@nadako
Copy link

nadako commented Aug 15, 2016

I don't really see how it would make much difference compared to untyped though. It can be even worse, because untyped __php__ is an unbound var, while something like php.Syntax.pythonCode is a normal function call from the analyzer POV, and it's going to make assumptions that it's a normal call, not something "magic" that is handled in the generator.

@Simn
Copy link

Simn commented Aug 15, 2016

Yes but this allows us to introduce some @:magic metadata for the analyzer. In the long run I would like to get rid of unbound variables entirely.

@programmatix
Copy link

programmatix commented Sep 26, 2016

Have you guys looked at the solutions used by Typescript and scala.js for this general problem of calling into untyped external code? I know they're JS rather than PHP, but it's the same basic issue. They let you define a typed interface, like this scala.js for the Google maps JS lib:

trait MapOptions extends js.Object {
  var backgroundColor: String = js.native
  var center: LatLng = js.native
  var disableDefaultUI: Boolean = js.native
  var disableDoubleClickZoom: Boolean = js.native
...
}

I know it seems a bit of a pain, but they don't take long to knock up, and Typescript has a cool shared repo of these interfaces for all kinds of JS libraries.

You can then call methods and create objects from those JS libraries in a type-safe way, and the generated code is optimal too.

@programmatix
Copy link

programmatix commented Sep 26, 2016

untyped is my biggest pain point currently (I'm writing a Wordpress plugin in Haxe and need to call into Wordpress functions loads) and I'd love to see it get a lot slicker. To take another example from how scalajs handles things, if you don't setup an interface (as above), you can just do this:

var arg2 = 3;
js.Dynamic.global.some_external_func("arg1", arg2)

Admittedly there's no more type-safety than with untyped, and scala.js has no idea whether some_external_func exists and how many args it takes, I just think it's a bit easier to read and write that than:

untyped __php__('some_external_func("arg1", $arg2)');

I can more easily see the arguments, at least. Maybe this syntactic sugar is possible?

@benmerckx
Copy link

Haxe has the same concept called externs: https://haxe.org/manual/lf-externs.html

@programmatix
Copy link

Ah amazing, thank you. I'm very new to Haxe, these look great.

@RealyUniqueName
Copy link
Owner

@RealyUniqueName
Copy link
Owner

Implemented php7.Syntax which purpose is to replace all untyped code: https://github.com/RealyUniqueName/haxe/blob/php7/std/php7/Syntax.hx

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

6 participants