4 ms·
Seeing this: > function add( ... ): float {} I think a much better choice would have been: > float function add( ... ) {} Or just: > float add( ... ) {} S
by franciscop 12y ago
Seeing this:
> function add( ... ): float {}
I think a much better choice would have been:
> float function add( ... ) {}
Or just:
> float add( ... ) {}
Since that's how it's done in other languages and people would feel at home with it.
- Drakim 12y agoHaxe uses the first syntax, as does AS3. So it's not like "other languages" are universally opposed to this order.
- allan_s 12y agoRust also has this syntax if I remember correctly, for parsing reason
- tne 12y agoAFAICT both Haxe and AS3 consistently put types on the right, including argument declarations. Other languages consistently put it on the left. Most languages make a choice like that; PHP mixes things up a bit. But then, it wouldn't be PHP if it was too consistent ;).
- TazeTSchnitzel 12y agoTo be fair, it's not the same as an argument type. The type of `foo()` isn't int, it's function. You're not specifying the type of the thing itself, merely the type of what it returns. Though this seems to have been lost on the creators of C. ;)
- pilsetnieks 12y agoGo also has a similar syntax (except the colon)
- aikah 12y agosure but AS3, Go Haxe and others ARE consistant function foo(a:int,b:string):bool{} in PHP this will be function foo(int a,string b):bool{}
- notacoward 12y agoPutting the type first for parameters and last for return values seems like exactly the same kind of inconsistency that has already earned PHP plenty of criticism. Personally I kind of like the Pascal style, I use the C style all day every day, but mixed style seems like the worst option available.
- rokhayakebe 12y agoSwift?
- Spoom 12y agoI thought that was rather odd as well. Are there many languages using this form of return type specifier?
- dtech 12y agoIt's fairly common in languages that determine (return) types themselves, e.g. Haskell, Scala and F# all use this syntax. Most languages that use a C-style syntax put (return) types in front. It makes very little sense for PHP. A function definition now even has both styles: `function foo(argtype arg) : rettype` instead of `function rettype foo(argtype arg)` or `function foo(arg : argtype) : rettype`.
- TazeTSchnitzel 12y agoHack, ActionScript and TypeScript put the type after. Hack is most likely where this syntax came from.
- juriansluiman 12y agoThe idea is that the old php syntax "function foo ()" remains unchanged [1] so developers can keep searching for "function foo". Removing "function" as keyword is a different topic and is discussed in a separate php thread [2]. The thing you mention ("float function add(){}") keeps the option to search for "function add", but I am unsure why this is not taken into account in this proposal. Anyhow, I am pretty happy this finally made it to the php language. [1] https://wiki.php.net/rfc/return_types#position_of_type_declaration https://wiki.php.net/rfc/return_types#position_of_type_decla... [2] http://marc.info/?t=141235344900003&r=1&w=2 http://marc.info/?t=141235344900003&r=1&w=2
- steveklabnik 12y agoHaving 'function foo' be grep-able is _wonderful_. My last language, Ruby, isn't very grep-able, but my current one, Rust, uses 'fn foo', and being able to just use 'git grep' to find things is _wonderful_.
- greiskul 12y agoGo also does types different from C, and they have a nice page explaining the reasoning of their syntax here: http://blog.golang.org/gos-declaration-syntax http://blog.golang.org/gos-declaration-syntax
- amyjess 12y agoThis is actually how it's done in Pascal. Everything old is new again.
- Danack 12y ago> function add( ... ): float {} Which if you read it out, it sounds entirely natural "This is a function called foo, which takes some params, and returns a float". Most of the other variants don't read as naturally. > Since that's how it's done in other languages and people would feel at home with it. PHP doesn't need to copy the mistakes of other languages. It's got plenty of its own mistakes.