PHP Gets Namespace Separators, With a Twist 523
jeevesbond writes "PHP is finally getting support for namespaces. However, after a couple hours of conversation, the developers picked '\' as the separator, instead of the more popular '::'. Fredrik Holmström points out some problems with this approach. The criteria for selection were ease of typing and parsing, how hard it was to make a typo, IDE compatibility, and the number of characters."
Going back to DOS style... (Score:5, Funny)
... and comming full circle.
Re: (Score:3, Funny)
Scroll your screen to see the animation:
\
-
/
|
Re:Going back to DOS style... (Score:4, Informative)
Uhm no. Because the DOS commands used '/' for indicating options as opposed to the '-' of the UNIX world.
Re: (Score:2, Informative)
which was based on CPM and VMS.
Re:Going back to DOS style... (Score:5, Funny)
Jeez, take a joke as it is, will you?
Re: (Score:3, Interesting)
'**' is easier to type than '^^'?
It is, once you consider some people have dead keys. Typing "^^" can become wildly different depending on what OS you're using, and result in weird behavior. On some systems/applications, the two carets are printed at once and you're back to normal editing. On others, the first one is written, but the other remains in dead-key mode. I've seen systems where this would just print a single caret (possibly coupled with a beep). Then the only reliable way on those keyboard layouts to type that symbol is to press
Re:It's all a joke (Score:5, Informative)
Java:
Attribute/Method access: foo.bar
Static method access: Foo.bar
Package access: foo.bar.baz
C#:
Attribute/Method access: foo.bar
Static method access: Foo.bar
Namespace access: foo.bar.baz
Python:
Attribute/Method access: foo.bar
Static method access: Foo.bar
Module access: foo.bar.baz
PHP:
Attribute/Method access: $foo->bar
Static method access: Foo::bar
Namespace access: foo\bar\baz
Re: (Score:3, Informative)
Yes but we are talking about a very, very slim minority here are we not? No offense intended but it seems like your whole point is hinging on the absolute minority of people out there. I can't think of any systems off hand that do this
According to Wikipedia, the circumflex accent is "used in written Croatian, Esperanto, French, Frisian, Norwegian, Romanian, Slovak, Vietnamese, Romanized Japanese, Romanized Persian, Welsh, Portuguese, Italian, Afrikaans, Turkish and other languages".
I don't know the keyboard layouts used by all of them, but I'd bet most (if not all) of them use a dead key for both the caret and the circumf
Re:It's all a joke (Score:5, Insightful)
"People with laptops" is a very, very tiny minority?
Re:It's all a joke (Score:4, Insightful)
:: is already used for static methods on classes... it would be harder to implement the differentiation of :: for namespaces and :: for static methods... especially if people started to use classes with the same name as a namespace (which is likely if all modules get their own namespace)
I actually think that '\' is appealing for what it will be used for. The one thing I first though of was SomeModule\new_object::test_function();
Wouldn't it try to evaluate the '\n' as a \n new line? I suppose it will be out of the double quite string scope so it could be alright... could get messy if eval()-ing code, though.
Re: (Score:2)
what exactly is Pikiwedia? just a static non-editable version of Wikipedia? it looks exactly like Wikipedia but with all collaborative editing features/links, Wikipedia branding, and basic navigation elements(i.e. search) removed.
the content is copied directly from Wikipedia, and though it doesn't seem completely up to date, it's only about a month outdated. therefore it's not completely static, so you wouldn't be able to cite Pikiwedia pages as a fixed source any more than you could Wikipedia.
at least Answ
Re: (Score:2)
Re: (Score:3, Insightful)
Re: (Score:3, Insightful)
So how do you like scamming your ads off of Wikipedia's content ?
Just when I thought the human race couldn't get any more pathetic, I get proven wrong.
Another fashionable addition for PHP: (Score:4, Insightful)
PHP 5.3 also adds support for local GOTOs. This langauge is so up with the times.
Re:Another fashionable addition for PHP: (Score:5, Insightful)
GOTO is what your CPU is actually doing 80% of the time.
You can pump up your ego by imagining that using a language without something explicitely called "GOTO" makes your code "up with the time". But what you actually do is nothing but GOTOs, just written in a different manner.
Ironically, the VM that PHP uses is completely GOTO-based (well, you can pick several methods at compile-time, but GOTO is what a lot of distributions chose because performance is often better than CALL and it's very stable nowadays).
Oh and even JAVA has GOTO and relies a lot on it. The compiler hides an explicit thing called "GOTO", but what you get after compilation is full of GOTO. And it's actually why apps can actually do something.
Laughing at "GOTO" is ignorance, or just blind trolling because you read somewhere that BASIC had a "GOTO" keyword. I guess in a few years your children will laugh at those horrible "$", "$this", "->", ":" and "\" symbols, that would remind them the old time of a language called PHP. Though you are proud of them now.
Using temporary variables like "$should_exit", dummy loops just to "break" at the right place, or named loops to work around "break" that would only exit the first loop is nothing but writing "GOTO" in an obfuscated and inefficient way. "GOTO" is not synonym for "spaghetti code" (the famous keyword always used by people blindly repeating that GOTO is bad).
Oh and grep for "goto" in your Linux kernel or in any BSD operating system. Wow, tons of them. Really. But I guess this is just because these source codes are shits written by people who can barely write GW-BASIC, and of course none of these operating systems actually work. Glad you are there to help. Teach them how to code, tell them that their code is so passé.
Or shut up.
Re:Another fashionable addition for PHP: (Score:5, Insightful)
GOTO is what your CPU is actually doing 80% of the time.
And your car's engine spends all of its time repeatedly causing small explosions with volatile petroleum.
The driver is generally recommended to let the engine do this and not try to intervene or do it themselves.
Wonderful analogy! (Score:4, Interesting)
Spot on. Dead on target and a car analogy. You rock.
--MarkusQ
Re:Another fashionable addition for PHP: (Score:5, Informative)
Goto's are useful. Labels are useful. Yes they can lead to problems, but so do things like pointers, dynamic typing, operator overloading, namespaces and automatic memory management. But they can also solve problems that are otherwise intractable, which is what the GP was trying to tell you. Dismissing them just because E. Djisktra said so is not really good enough.
If you want an argument from authority, or just a good read, here's Donald Knuth on Structured Programming with GOTO Statements [snu.ac.kr]. You need to read that paper before you can have a proper opinion on the GOTO statement. Otherwise you're just adhering to dogma.
Re:Another fashionable addition for PHP: (Score:5, Insightful)
Wow, can't type today. Let's try again:
GOTO remains the best way, in most programming languages, to exit multiple loops, branch to common clean-up code before leaving a function, etc.
Re:Another fashionable addition for PHP: (Score:4, Insightful)
Modern languages have "exit for" or "break" to bail out of a loop.
If you have a triple nested loop in the same function, you should refactor the code and move the inner loops into another function.
What do you mean by "Clean Up Code"? If you have so many branches in a single function, again, refactor the code and split them into multiple functions.
See also: Code Complete [cc2e.com]
Re:Another fashionable addition for PHP: (Score:5, Insightful)
Yes, for school problems, refactoring the code to make the inner loop a function is great. For real-world legacy crap code, it's often impractical. You might have 15 variables that would need to be passed into that inner loop, and you might have shop standards preventing you from passing some of those types into functions, etc, plus your boss (often rightly) object to you "refactoring" code that works today. "Refactoring" is a charmingly naive expectation for legacy crap code to begin with. And if you're not working with legacy crap code today, consider yourself lucky: you don't know how good you have it.
And by "clean-up code" I mean: you allocate resources at the top of a function, so they must be cleaned up at the bottom of the function (and no sneaky returning from the middle of the function). That code at the bottom of the function is "clean-up code". If you have 15 possible errors in your function, you either have some unreadable mess with 15 nested if statements that hide a perfectly straightforward logic flow, *or* you branch to the clean-up code in each of the 15 error cases, making the actual logic of the function obvious.
Of course, most coders are simply incompetant, don't even bother to check for errors, and certainly don't ensure that every resource allocated at the top is easily visually identifyable as being freed at the bottom, which is why there are so many job maintaining legacy crap code (and why Java exists in the first place).
And of course there are languages in which "goto" is pronounced "throw" and all the clean-up happens automatically, but mostly it's inventory and payroll databases that get coded in such languages: give me legacy crap code that does something *interesting* any day! I will forever cherish the one job I had in which C++ was used correctly (not the usual typing C into a cpp file) and goto was really unnecessary, but I don't expect lighting to strike twice in my career.
Blah, blah, Code Complete. Some of us were doing that stuff correctly long before Steve McConnell (and I'd hardly cite Microsoft as an example of a stable, secure, maintainable code base!).
Re: (Score:3, Insightful)
Although rare, there are times when GOTO is the cleanest way out.
I recall being stunned to run across one of them writing the code for my dissertation, and noting that I was going through a *lot* of code to avoid a single, simple GOTO. Yes, I could have avoided it, but under the circumstances, the GOTO was much cleaner.
Oddly, I don't remember the circumstances; just my sheer amazement that such circumstances actually existed.
hawk
Re: (Score:3, Informative)
In you example, F() took only two arguments, and the extra processing overhead of the function call was (presumably) not a concern. There are times when one or the other of those factors is practical. If the inner loop needs 15 variables (and is modifying some of them), the code can become far less readable than a simple goto, and noticably less performant. Of course, code performance is less of an issue every decade, but multiply nested loops are the one place where it still tends to matter.
Can\'t read summary (Score:5, Funny)
But in PHP for Windows (Score:5, Funny)
It'll be /, just to keep things interesting.
I have to say they are working really hard.... (Score:5, Insightful)
Re:I have to say they are working really hard.... (Score:5, Informative)
Re: (Score:2)
Hm... Thanks for the link. Enlightening reading.
Now I'm slowly getting why people even bother to use Python for Web. Pretty much natural choice for all who can't (or refuses) to grok Perl, yet want to have a real language at their disposal.
I used PHP around year 2000. It was simple like toy. But it seems that its developers didn't realize that the language did outgrow the sandbox they were living in. Also - the responsibility they carry before all the web developers who invested time into learning PH
Re: (Score:2)
For the record, I grok Perl just fine, but fell in love with Python because it felt like "Perl done right". I don't avoid Perl because I have to, but because I can. I just mentioned that because a lot of people really seem to believe that Python is for people who can't handle anything else.
Re: (Score:3, Interesting)
CGI days (only technical requirement is standard input and output) are long gone.
Well, not really. It still works. Your biggest complaint:
You gonna end up with huge performance problems unless you would use something more modern and advanced than trivial piping.
That is a vertical scaling concern. If vertical scaling is that important to you, consider using C, instead.
Not that it's unimportant. I'm just saying -- if all we had was CGI, there's no reason we couldn't still throw hardware at the problem.
No, I was talking about things like FastCGI, or even more relevantly, in-application webservers. It turns out, talking HTTP really isn't that hard. Ruby has, by my count, six separate in-application webservers su
Re: (Score:2)
Seriously...
That link compares PHP to Perl.
I spit on Perl and so does everybody else I know. Had it at least been Python, Java or something, but Perl?!
My biggest gripe with PHP now, is the lack of support for good IDE with -GOOD- debugging capabilities. God damn !"#%@-Zend...
Re: (Score:3, Insightful)
That link compares PHP to Perl.
And pretty soundly shows Perl to be as good as or better than PHP, in some fundamental ways.
I spit on Perl and so does everybody else I know.
You apparently don't know a lot of people who actually understand Perl.
The BASIC of the 21st century (Score:5, Insightful)
I once did a lot of work on PHP. Today, when people ask me for upgrades I just migrate it to Python.
This unfortunate choice of the escape character for namespace separator is stupid, but seems almost irrelevant to me. How many nails do they need in the PHP coffin to bury it?
Re: (Score:2)
and I'm migrating them over to Ruby on Rails or Merb. PHP is lame.
Re:The BASIC of the 21st century (Score:5, Funny)
Then I see people suggesting \ for a namespace separator, and I wonder what happened to all the people that put so much work into making PHP5 good, and why we can't get them back.
Re: (Score:3, Insightful)
The last one was seen downloading a Ruby On Rails development environment.
Re:The BASIC of the 21st century (Score:4, Insightful)
PHP ? Real OO? Thanks for the great joke.
How can I add methods to Number? Ehm you know, the class used for numbers... In order to write 3.times() for instance... ah, it doesn't exist? Ok, so how can add methods to strings? Impossible, strings aren't objects either?
Stop kidding.
Why do PHP programmers use classes for? Just to avoid collisions between two functions with the same name when files are included. Really... very few PHP code instanciates more than one object per class.
Introduction of namespaces might limit this.
But there's something else that PHP miss: a "static" keyword.
Guess how very large source code like OS kernels or demos have been built in C or assembler, without namespaces, without classes, without symbols like \, but without coders constantly fighting about names collisions?
The reason is file-local symbols, ie. the static keyword in C (and local symbols in assembler). Only export (ie. make non-static) what you need to use in other files. As a bonus, it helps the compiler in order to optimize the code.
Re: (Score:2)
I don't do minor upgrades, there are other people for that. When a major upgrade is needed, let's say from version "2.7" to "3.0" they call me.
Re: (Score:2, Funny)
I don't do minor upgrades, there are other people for that. When a major upgrade is needed, let's say from version "2.7" to "3.0" they call me.
Wow. So you're the guy that does those small changes huh? They usually call me for "3" to "4". I write it all in machine language for optimized speeds and job protection.
Re:The BASIC of the 21st century (Score:4, Funny)
in other words you're incapable of maintaining code and you rewrite the same thing for them in a different language...
Re: (Score:3, Interesting)
While at it... (Score:2)
While at it, they should have picked a page from the W3C and made namespaces full, compliant URIs.
That would have been epic! /sarcasm.
.NET / WPF is going this way (Score:3, Interesting)
At least in a sense. You can map a .NET namespace to an XML namespace. Say you have namespace that is:
Shados.Awesome.Controls
You can map that into:
http://www.shados.com/controls
In c# you'd plunk this into your AssemblyInfo.cs file:
[XmlnsDefinition("Shadows.Awesome.Controls","http://www.shados.com/controls")]
And thus add it to your XAML code:
<UserControl xmlns="http://www.microsoft.com/xml/something"
xmlns:shadow="http://www.shados.com/controls">
<shadow:AwesomeControl x:Name="myControl" param="aPar
yet another wtf (Score:3, Interesting)
The rfc [php.net] claims that typing "**" is easier than typing "%%" or "^^".
Re: (Score:2)
Re: (Score:2)
The rfc [php.net] claims that typing "**" is easier than typing "%%" or "^^".
But it is! ... if your right shift key is broken...
Re: (Score:2, Funny)
AltGr + Plus [the key right of number 0] on Estonian layout also. This is so discriminatory! :P
We should use a character present on most keyboard layouts. I propose the use of the Space-key for this purpose.
A long overdue addition (Score:2, Insightful)
Just my 2 cents.
Re:A long overdue addition (Score:5, Funny)
While you're livin' it up at your stately manor, I'm coding PHP out of my garage, you insensitive clod!
Re:A long overdue addition (Score:5, Insightful)
Thats all good. I personally feel its just easier to avoid PHP altogether and not have to adjust to all of the language's quirks for little to no benefits from other offerings. Simpler that way.
Backslash! (Score:4, Funny)
Well now we all know what trouble this is going .. (Score:5, Funny)
... to cause for windows servers...
imagine what directories will be deleted due to a typo!
Re: (Score:3, Insightful)
other issues (Score:5, Insightful)
Maybe they could starting fixing the noun-verb vs verb-noun problems instead.
Re: (Score:2)
Re: (Score:2)
or (needle,array) / (array,neddle) confusion
Even with years of coding, I can't remember exactly function names or attributes, which is blaming when PHP.net site is down.
I bet having better naming/call conventions will save them wagons of bandwith !
Today is a Wonderful Day (Score:5, Funny)
Thank you, PHP.
Re:Today is a Wonderful Day (Score:4, Interesting)
so what's the problem here? (Score:2)
Is the problem that they chose something 'different' than what everyone else uses, or is there a real technical reason behind the snarkiness? I think it's a good thing they even considered the issues involved, though I'd disagree with the 'ease of typing' issue - once your fingers leave the home row, you dramatically increase the chances of a typo. People who aren't touch typists have some pretty serious misunderstandings about typing ease. (Hello, Apple keyboard designers, I'm looking at you.)
Re:so what's the problem here? (Score:5, Informative)
The problem is not merely that it is different.
The problem is they chose the ESCAPE character as a namespace separator.
This is even worse than using $ as the namespace separator.
Because of the problems it causes syntax highlighters, the problem it causes to programmer sanity when storing identifier names in a string.
The problem it causes when searching through and sanitizing code.
For example, since \ has a special meaning in the context of a regular expression, searching and replacing using regular expressions just got very painful.
Copy and paste no longer works for searching and substituting.
Refactoring just became a major bitch.
What about Eval? (Score:4, Funny)
Now do you have to escape your namespaces before passing them through eval?
eval("$instance = new My\\Super\\Class(\"blah\"););
Since they now are using the escape character for namespaces, I wonder what kinds of security implications this might have? What happens when a PHP program for some reason evals() some user input that doesn't properly escape the namespaces?
Re: (Score:3, Insightful)
You'd have to do that anyway, because you used double quotes, "$instance" is going to get evaluated and your eval will fail anyway.
Re: (Score:3, Interesting)
1) "/" - used by almost all languages in regex expressions. I wouldn't want to pollute that namespace.
2) "::" - Awesome. Many other languages do this. I think this is already taken for PHP, but I dont know PHP well enough
3) "|" - Pipe has a very specific meaning on the command line and I'd hate to pollute that. Plus it looks ugly.
4) "~" - Any reasons this wouldn't work?
5) "." - Again, awesome. Many other languages use this.
6) "!" - Used
7) "&" - Ugly, used.
8) "," - Why not? Does any language use th
Phalanger (Score:2)
It's interesting to see what the Phalanger [php-compiler.net] guys are going to do now. They've already been using ":::" as a namespace separator for their .NET/CLR language extensions, as the next most logical choice. And now this.
On the whole, though, this is probably just as bad as using "." for string concatenation (it was sort of okay for Perl because back then it wasn't yet used ubiquitously as a member access operator, but for PHP, it was a dumb choice). Backslash has a near-universal meaning as an escape character
Re: (Score:3, Insightful)
Why, exactly? English is not the only language spoken in the world, you know, and programmers in many countries feel more at ease naming identifiers using their native language rather than trying to come up with an English translation with their limited knowledge of English. In fact, when people who don't know
Easier on which keyboard layout? (Score:5, Insightful)
How is it possible for even American developers to be this clueless. Which characters are convenient to type depends entirely on the keyboard layout that is used. Case in point, $ is insanely painful to type on Scandinavian layout.
If your choice of characters used in your programming language is affected by how easy/hard it is to parse the code, you probably shouldn't be involved in developing a programming language.
Re: (Score:3, Interesting)
I couldn't agree more. When I read this my only thought was "what the fuck?". On a German Mac keyboard, \ is Alt+Shift+7. I think on Linux and Windos it's something equally retarded.
Will the people who made PHP a good programming language please fork it and take it away from the clueless morons who have taken over?
Re:Easier on which keyboard layout? (Score:5, Insightful)
This line from the IRC discussion says it all:
[16:24:51] bjori: switch to US layout ;)
Classes, namespaces, and subnamespaces (Score:4, Informative)
Other suggestions (Score:3, Interesting)
For additionnal fun, read this: http://wiki.php.net/rfc/namespaceseparator [php.net]
Looks like they considered stuff like :> and :) as separators for namespace. Seriously.
Also, they don't give any malus for tybe-ability to \ while on most european keyboards it's a lot harder than any other suggested separator. Way to go !
\ vs / (Score:2)
Um, that should be "especially for DOS / Windows users". Unix was here first dumb asses
Furthermore, using the character, that just about every other programming language uses for the escape character, as an operator (or separator) is just plain stupid -- I'm also talking to you Microsoft, but that horse has definitely left the barn...
Easy to fix this (Score:5, Funny)
Since PHP is open source, someone will make a fork with a different separator and the dumber of the two choices will wither away.
Oblig. Bender (Score:3, Funny)
Yeah, screw those guys! I'll make my own PHP -- with blackjack! and hookers!
Forty years down the road (Score:3, Insightful)
with 4GB RAM machines with TB hard drives - and we're still worrying about "the number of characters".
Oh, please. Fucking nerds.
For the last forty years, we've been constrained by one pointless limitation after another, not to mention the complete inability of a PC to discern what is an identifier and what is command syntax if it has fucking SPACES in it.
Get your heads out of your asses.
And learn to type.
"Number of characters" - Jesus Baron von Christ!
And just to add to that (Score:3, Informative)
calling a computer programmer a "software engineer" is like calling a crack whore a "courtesan".
There's no "engineering" involved for ninety nine percent of them.
Re: (Score:3, Funny)
[sic]
Re:what wrong with (Score:5, Insightful)
PHP uses the . as the concatenation operator. PHP does not support operator overloading...
Re: (Score:2, Informative)
PHP uses the . as the concatenation operator. PHP does not support operator overloading...
You don't need operator overloading. To use the same symbol in different contexts.
You need a parser worth its salt.
a.one = $b.two . $a.one
Only has one use of the concatenation operator if 'a' and 'b' are namespaces.
A good convention is to simply require space around the concatenation operator. Or to require both operands meet the lexical conventions of a variable or string constant, for it to be interprete
Re:what wrong with (Score:5, Insightful)
I suspect they're doing the same thing with namespaces. The backslash isn't used for anything except escaping strings, and I doubt that's going to add any ambiguity at all. There are a lot of problems with PHP, and it's well worth your ridicule, but making sure that separate operations have separate operators isn't one of those problems.
Re:what wrong with (Score:5, Informative)
Re:Well, That Does It! (Score:4, Insightful)
It's not just developers, it's things like IDEs which highlight anything starting with '\' as an escape sequence.
Which - since nearly every other common programming or scripting language uses '\' to mark the beginning of an escape sequence - is something you might reasonably expect.
Come to think of it, so does PHP... ooops...
Re: (Score:2, Funny)
Re: (Score:2, Interesting)
This isn't the first (or last) time PHP developers have implemented a stupid workaround rather than fixing problems with the language/runtime/interpreter/parser/scanner.
Re:WTF? (Score:5, Interesting)
You should say "thanks" they haven't chosen something else. e.g Jam (build system; make analog) uses "!" as a "platform neutral" path separator. During evaluations for new build system I joked that I oppose jam since we do not need a "platform neutral" system - we need one for *nix and cygwin. To my surprise many supported me.
I think their decision to use '\' is very very dumb one.
I'm still huge fan of Objective-C in that aspect. Unlike C++, which tried to marry C and objects, ObjC took more pragmatic approach: C constructs remains C constructs and object oriented constructs got new distinctive syntax so that you can never mix up what code you are looking at.
In that aspect, I think PHP folks would regret their decision in future: '\' isn't distinctive enough and they would need to introduce more silly syntax hacks when extending language further.
Re:WTF? (Score:5, Insightful)
I think their decision to use '\' is very very dumb one.
You've summed up my opinion concisely. That is *truly* retarded to use the (almost?) universal escape character for another reason. Almost as retarded as Microsoft going with \ for a directory separator.
Re: (Score:2)
Having an AZERTY keyboard, living in France, I do remember the difficulty to have \ characters on certains small keyboard in MSDOS.
Two methods : AltGr + 8
or Alt + 9 + 2
And PHP group thinks it's easer to use ?
that is soooooooooooo simple to use it instead of "::"
Re: (Score:3, Funny)
Re: (Score:3, Interesting)
Not at all, there were some serious ambiguity issues that needed to be resolved.
Take the following code:
----------
# first file
namespace Foo;
function blarg()
{
echo "function";
}
# second file
class Foo {
public static function blarg()
{
echo "method";
}
}
# third file
Foo::blarg(); // what does this output?
-------
The problem here is that calling a static m
All languages (Score:2)
... are just another language. They all do the same stuff and 95% of what you want to do happens in the library anyway, so the language is just a framework for iterating your library calls or instantiating your objects if you prefer. They all have corners of stupidity you have to work around - some more than others, but what does it matter really?
Re:HOLY FUCKING SHIT!?!?! (Score:5, Informative)
Except you just made a typo. It is "\" instead.
Either way, most languages use either "." or "::" for namespaces
# perl looks like
use My::CPAN::Module qw();
my $instance = My::CPAN::Module->new("junk");
# c# looks like
using System.Windows.Controls;
System.Windows.Controls.ListBox box = new System.Windows.Controls.ListBox();
# c++ looks like (I think)
namespace Blah::Blah;
# php will now look like
$object_instance = new My\PEAR\Module("myvar");
I'll leave the "looks" of PHP's method to the reader.
Re: (Score:2, Funny)
Re:That's because.. (Score:5, Insightful)
So using + for string concat too would be basically impossible... When would you decide to concat the operands, and when would they be added? If you base it on the results of the string to number conversion, you get situations where the same line of code sometimes adds numbers and other times concatenates strings, or where it is impossible to concat two strings which contain only digits.
Re: (Score:2)
Re: (Score:2)
Personally, I get annoyed with languages like Javascript that use + for string concatenation. VB did it back in the day, and you would sometimes run into issues where you'd combine two variables and it would treat one as an integer or a decimal and do the wrong or unexpected thing. IIRC you could do this:
Dim myInt as Variant
Dim myStr as string
myInt = 1
myStr = "hi"
myStr = myStr + myInt
And you'd get something like "hi1.0"
It just doesn't feel right to do the same thing in modern languages. What should be th
Re: (Score:2)
Python: TypeError: cannot concatenate 'str' and 'float' objects
Ruby: TypeError: can't convert Float into String
Weak typing is the problem, not dynamic typing.
Re: (Score:3, Insightful)
Calling a variable "myInt" doesn't make it an integer -- it was a float from the second you added a dot.
First, duh. Second, are you sure it will return "hello1.0" and not "hello1.00" or "hello1"? I'm not, I could test to find out, but that makes my point. It adds ambeguity to the language and makes me think about something I really should be thinking about. To use your example, what does the following result in?
function blah(arg1) {
var blah = "10" + arg1 + 5 + "hi";
}
alert(blah("hi"));
alert(blah(1));
alert(blah("1"));
My example isn't as contrived as yours. What if blah() was in some other JS file. It could be easy to trip the function up based on what you pass in.
Using "." for string concatenation at least gives both you, the code reader, and the compiler a hint at what you mean. That way the compiler can barf if you do something silly like concatenate an integer with a string. Using "+" for both addition and concatenation just makes more work for both parties.
In any case, I don't find concatenation nearly as useful as interpolation, most of the time. In Ruby, at least, interpolation is known to execute faster than concatenation. But that doesn't help you if you're using JavaScript.
I agree with this statement. I much prefer how Ruby and Perl do it... just toss the variables into your string and it will interpolate them.
BTW, I'm not too familiar with Ruby. Does it pull the same trick Perl does and use a different operator for string comparison?
if($myStr eq "hi") { print "hi";}
Re:Gripe Moan Bitch and Holler! (Score:4, Insightful)
Scripting languages are for those of a weak mind and poor technical skills and the singular lack of the ability to plan a system out before you write one line of code.
Or for projects that need to be compiled at runtime. But, nice magnanimity.
Re: (Score:2)
Re: (Score:3, Informative)