6 ms·
1 & 2 == "12" - Now THAT I call unexpected behavior. :-/ Interesting language though.
by dlsym 13y ago
1 & 2 == "12" - Now THAT I call unexpected behavior. :-/
Interesting language though.
- pogden 13y agoIn what way is that unexpected, given then & is string concatenation?
- arcatek 13y agoWell, because '&' is probably not a very good choice for a concatenation operator since it has a very different meaning in most of languages. Most developers would overlook such an error. Of course you can say that since it is defined as such, it's not a issue, but then we could say that any Javascript "Wat" behavior is also defined in the spec.
- protomyth 13y agoI would say that & is a fine choice (much better than +) for a concatenation operator. It allows numbers to be built like strings in an interesting way and keeps the meaning of the & consistent. It really is past times that every language needs to conform to the choices made by C.
- 30thElement 13y ago+ has problems as string concatenation, but that doesn't make & a good choice. We still use a lot of C-ism's because people are used to them, and any deviations from C-like behavior, which at this point could be called "common behavior" just increases the learning curve and the cost of a context switch. Personally, I like Perl's period(.) for string concatenation, but I think it can too easily get lost in the line, especially for more complex stuff. $foobar = "foo" . $bar->{'bar'}
- protomyth 13y agoPeriod has a general meaning of ending a sentence. At this point in our history, people willing to clear the deck of C-ism should be encouraged. I think the learning curve is worse when folks imitate C for no reason other than computer tradition as opposed to building something that feels tied together. We ditched a lot of older habits with C, we should continue the path.
- gkop 13y ago& is not only a bitwise operator, but also a set arithmetic operator in many languages (Ruby and Python, for examples).
- onli 13y ago& is normally (= in C) a bitwise operator, not a string concatenation. Especially looking strange for me when it makes a string out of two numbers.
- cameronkknight 13y agoI never claimed for GorillaScript to be a C-like language, because at its core, stripped of syntax, JavaScript is not a C-like language. It more resembles LISP with its macros and closures with nice syntax built around those concepts. I tried to make all the operators match their closest arithmetic, mathematical equivalents. Thus using `^` for exponent and `+` for addition. Since JavaScript does not support bitwise operators cleanly, needing to cast to int32 and generally being disued, I felt they could be better aliased with `bit`, freeing up those operators. Since string concatenation needed some operator as a building block for string interpolation, the freed-up `&` was a ripe candidate for it. That all said, string interpolation is the typical use case for creating strings, so feel free to just use that if possible. Anything more than that and you might need a templating engine.
- onli 13y agoI realize you probably also wanted to answer OP, but let me just state that there was no real judgement intended. I am not a C-programmer, it was meant as an explanation. > Thus using `^` for exponent and `+` for addition That is good in my eyes.
- X4 13y agoWhat do you think about not using any operator (but space+"") for concatenation and using a syntax like this: var 1 = 5+5 // I would expect: Error 0-9 cannot be used as a variable var foo = "Hello " "Bob!" "\nAre you there?" var bar = "Hello " 1 console.info(1) // throw error and refer to var 1 console.info(foo) "Hello Bob! Are you there?" console.info(bar) "Hello 1"
- cameronkknight 13y agoJuxtaposition as an operator is already used by function invocation, e.g. f "Hello" If f were a string, how would I know I wanted to concat it instead of calling?
- rm445 13y agoI have always liked ~ for string concatenation. Looks like a little string tying the strings together, and hardly likely to be confused with the bitwise not operator.
- rattray 13y agoWhere is a ~ used for string concatenation?
- theOnliest 13y agoPerl 6 does...I can't recall anywhere else, though.
- alecrn 13y agoD uses it too. I think they use it for array concatenation as well.
- cameronkknight 13y agoI thought about it, but for the most part I used `~` as a way of referring to something in an unstrict way, e.g. `~+` doesn't typecheck, it just adds and auto-coerces both operands to a number. I didn't want to have `~` mean string concat because it overloads the semantic meaning of what `~` would represent. I actually stole `&` from VB, as well as `\` as floor division.
- cameronkknight 13y agoIn production code, using & for string concat is pretty atypical, since you can use string interpolation: "$(alpha)$(bravo)"