Monday, 2 December 2013

RE: [dcphp-dev] Type safety in PHP

Thanks for the link, I haven't seen this presentation yet.
So now that HHVM's additional syntax is official I can outline a few more differences (I have mentioned them to one team
member at facebook during a short discussion but unfortunately they never came back to me, might be that they did not
like that I criticised a few points).

TSPHP has another syntax for return types than HHVM. TSPHP sticks to the C-Style syntax where HHVM goes for an left to
right approach (like Pascal, or GO). IMO this is somewhat inconsistent/not really intuitive for someone who has C, C++
etc. background but in the end it's a detail and I might consider to change the syntax in TSPHP in favour to have the
same syntax as HHVM (because in the end I believe the two projects could benefit from each other). But I have to say, I
don't like the inconsistency. Following the two syntax:
HHVM: function foo() : returnType {}
TSPHP: function returnType foo() {}

Another thing is the syntax for nullable.
HHVM: ?int
TSPHP: int?
I don't know what was behind the decision to go with ?int, maybe there is another language out there which has the same
syntax (that would be a good argument IMO, to lower the learning curve for users coming from that language) or maybe
they decided they should have something unique. I decided to go for the same syntax as C# because I do a lot of stuff
with C# as well and I don't want to rethink Nullable when I switch the language. However, I might consider changing the
syntax as well since it's just a minor difference.

Another difference will be closures. They are not yet supported in TSPHP but I have a different opinion on the syntax
than HHVM. And in fact, the team member at Facebook told me that due to the syntax for closures they decided to stick to
the Pascal like syntax for return types, because they found (the C-Style syntax)

function (function string (int $foo, float $bar)) foo(int $foo){
return makeCallback($foo);
}

more confusing than
function foo(int $foo) : (function(int $foo, float $baar) : string) {
return makeCallback($foo);
}

Fair enough, I am used to C syntax so I read the return type over very quickly, so it isn't that confusing for me but I
would agree in general that closures as return types can be confusing. Anyway, my suggestion was to reuse "callable"
instead of "function" (as in the type hints of PHP - seems more natural to me). This would make things less confusing
IMO and TSPHP will most likely stick to "callable" instead of "function". But since closures aren't yet supported in
TSPHP I will certainly reconsider it at the point where I will implement it. Maybe I will also go for another approach.

The syntax in TSPHP could look as follows
function callable string (int $foo, float $bar) foo(int $foo){}
or
function (callable string(int $foo, float $bar)) foo(int $foo){}
or even
function callable<string(int $foo, float $bar)> foo(int $foo){}

As you can see I am not sure yet :-)

It can still be confusing but less than "function function" in my opinion. Also HHVM's syntax would be less confusing
IMO:
function foo(int $foo) : callable(int $foo, float $bar) : string {}
or
function foo(int $foo) : (callable(int $foo, float $bar) : string) {}


I haven't seen the syntax for generics until now and TSPHP will most probably stick to the same syntax. Seems reasonable
to me, also the syntax for constraints. Yet, I am thinking about enabling structural typing for generics and thus things
might change.

Yet another difference between TSPHP and HHVM is how scalar fields (members/properties) are treated. TSPHP assigns a
default value to all scalar fields which do not have an explicit initial value. An example
class A{
private int $a; //will be 0
private int $b = 1; //will be 1
}

TSPHP might have another type hierarchy. TSPHP has the pseudo type object (could be similar to mixed in HHVM).

Last but not least, I have to say that TSPHP isn't yet at the same advanced stage as HHVM, there are still a lot of
design decisions to make (for instance how to tread HTML in TSPHP files) and implementations to write. There will most
probably be feature in TSPHP which aren't available in HHVM (as the cast modifier, an invention of mine
http://tsphp.tutteli.ch/wiki/display/TSPHP/cast+modifier) and vice versa.

Nevertheless, I hope that gives a better overview of the differences.


From: Sandy Smith [mailto:sandy@sfsmith.com]
Sent: Monday, December 02, 2013 10:01 PM
To: Robert Stoll
Cc: DC PHP Developers Group Washington
Subject: Re: [dcphp-dev] Type safety in PHP

The only thing so far is Keith Adams's presentation at StrangeLoop:

http://www.infoq.com/presentations/php-history

However, it does go over their approach and you can download the slides to see the examples of how they've implemented
their type system.


On Dec 2, 2013, at 4:56 AM, Robert Stoll <rstoll@tutteli.ch> wrote:


To be honest, not really, I know that the project exists and what the aims are, I read sometimes a few blog posts but
that's it.
I was in contact once with a team member at facebook and had an insight how they plan to extend the syntax of PHP to
allow further type hints (variable, return types etc.) but it was all unofficial at that stage and I couldn't really
find any language reference by now.
Do you have a link by chance to a language reference or a specification (the link on hhvm just leads to the wiki on
github which isn't a specification IMO)?
 
But I know a few contrasts between the projects. HHVM's big goal is performance and they have even introduced a new
runtime environment to achieve this goal where TSPHP's goal is introducing type safety without changing the runtime
environment.
That's the biggest contrast. If you want to run HHVM's PHP then you will need an own hosting server with the new runtime
environment set up whereas TSPHP runs on the common environment as it is today.
To conclude this point, I would say HHVM's clientele are (bigger) companies which usually have an own hosting server
where TSPHP's clientele are all PHP users but especially the man in the street which cannot afford an own hosting
server. This might well change over time if the HHVM should gain popularity and web hosting companies are picking up
HHVM runtime in their assortment.
 
Another difference is that one can write strong typed code with HHVM but it's optional (that might have changed by now,
link please :-) ) where TSPHP enforces strong typing (it is type-safe, more precisely type-safe during compile time,
during runtime it is still PHP and thus type-unsafe).
 
If you can provide me a link to a good documentation of HHVM then I am glad to point out more differences.
 
Cheers,
Robert
 
 





--
You received this message because you are subscribed to the Google
Group: "Washington, DC PHP Developers Group" - http://www.dcphp.net
To post, send email to washington-dcphp-group@googlegroups.com
To unsubscribe, send email to washington-dcphp-group+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/washington-dcphp-group?hl=en
---
You received this message because you are subscribed to the Google Groups "Washington, DC PHP Developers Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to washington-dcphp-group+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 comments:

Post a Comment