odd typo, what's happening

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

odd typo, what's happening

Thijs Schreijer
I ran into the following;

local v = {type = "timestamp"}                          -- added to run it
if v.type == "number" or v .type == "timestamp" then    -- actual code
end



Notice above, the second `v.type` has a typo with an extra space; `v .type`. I'd expected an error, but it runs fine.

Why?

Thijs


Reply | Threaded
Open this post in threaded view
|

Re: odd typo, what's happening

Coda Highland
On Thu, Dec 17, 2015 at 3:25 PM, Thijs Schreijer
<[hidden email]> wrote:

> I ran into the following;
>
> local v = {type = "timestamp"}                          -- added to run it
> if v.type == "number" or v .type == "timestamp" then    -- actual code
> end
>
>
>
> Notice above, the second `v.type` has a typo with an extra space; `v .type`. I'd expected an error, but it runs fine.
>
> Why?
>
> Thijs


Why not? . is more or less a binary operator and follows the syntax
rules for one.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: odd typo, what's happening

Soni "They/Them" L.
In reply to this post by Thijs Schreijer


On 17/12/15 09:25 PM, Thijs Schreijer wrote:

> I ran into the following;
>
> local v = {type = "timestamp"}                          -- added to run it
> if v.type == "number" or v .type == "timestamp" then    -- actual code
> end
>
>
>
> Notice above, the second `v.type` has a typo with an extra space; `v .type`. I'd expected an error, but it runs fine.
>
> Why?
>
> Thijs
>
>
What if you replace .name with ["name"]? It becomes `v ["type"]`.

You can add spaces around . in many languages, including C, C++ and
Java. You can even add line breaks! E.g.

local dog = DogBuilder() -- Also works when using :
             . setName "Doggy"
             . setType "Poodle"
             . build()

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: odd typo, what's happening

Enrico Colombini
On 18-Dec-15 00:51, Soni L. wrote:
>
> You can add spaces around . in many languages, including C, C++ and
> Java. You can even add line breaks! E.g.
>
> local dog = DogBuilder() -- Also works when using :
>              . setName "Doggy"
>              . setType "Poodle"
>              . build()

Four decades of coding... and I missed this one :-)

--
   Enrico

Reply | Threaded
Open this post in threaded view
|

Re: odd typo, what's happening

Rena
In reply to this post by Thijs Schreijer

On Dec 17, 2015 6:26 PM, "Thijs Schreijer" <[hidden email]> wrote:
>
> I ran into the following;
>
> local v = {type = "timestamp"}                          -- added to run it
> if v.type == "number" or v .type == "timestamp" then    -- actual code
> end
>
>
>
> Notice above, the second `v.type` has a typo with an extra space; `v .type`. I'd expected an error, but it runs fine.
>
> Why?
>
> Thijs
>
>
Because whitespace between tokens is usually ignored in Lua and most other languages.

The exception is when it's ambiguous:
x = f
(a or b):c()
That expression could be interpreted as two statements or one (remove the line break and see). In that case Lua is kind enough to make an exception to the "ignore whitespace" rule and raise an error (which you can resolve by adding a semicolon or rearranging the statements to remove the ambiguity), instead of just strictly enforcing precedence rules and doing what you may not expect.

Fun fact: comments are treated like whitespace too:
x = --[[ my lucky number: ]] 3

Reply | Threaded
Open this post in threaded view
|

Re: odd typo, what's happening

David Favro
On 12/18/2015 07:19 AM, Rena wrote:
> The exception is when it's ambiguous:
> x = f
> (a or b):c()
> That expression could be interpreted as two statements or one (remove the
> line break and see). In that case Lua is kind enough to make an exception
> to the "ignore whitespace" rule and raise an error

That's not nice, that's nasty -- whitespace should be ignored except to
separate tokens.  The statement is equally "ambiguous" regardless of the
newline, and it isn't ambiguous at all so long as the semantics are
documented.  Let the programmer format his code as he likes.

And, thank goodness Lua does *NOT* raise that error anymore since 5.2
(which it raised on statements even simpler than the example you gave).  
You can resolve the ambiguity yourself by using semicolons and/or
parenthesis, but the former behavior forced disagreeable (to me)
formatting.  Furthermore, if you always terminate your statements with
semicolons, it doesn't appear ambiguous at all.

-- David


Reply | Threaded
Open this post in threaded view
|

Re: odd typo, what's happening

Egor Skriptunoff-2
In reply to this post by Rena
On Fri, Dec 18, 2015 at 3:19 PM, Rena <[hidden email]> wrote:

The exception is when it's ambiguous:
x = f
(a or b):c()
That expression could be interpreted as two statements or one (remove the line break and see). In that case Lua is kind enough to make an exception to the "ignore whitespace" rule and raise an error

Really?
I've always thought that Lua treats this chunk as absolutely correct way to write the following code:
x = (f(a or b)):c()

Reply | Threaded
Open this post in threaded view
|

Re: odd typo, what's happening

Dirk Laurie-2
2015-12-20 6:19 GMT+02:00 Egor Skriptunoff <[hidden email]>:

> On Fri, Dec 18, 2015 at 3:19 PM, Rena <[hidden email]> wrote:
>>
>> The exception is when it's ambiguous:
>> x = f
>> (a or b):c()
>> That expression could be interpreted as two statements or one (remove the
>> line break and see). In that case Lua is kind enough to make an exception to
>> the "ignore whitespace" rule and raise an error
>
> Really?
> I've always thought that Lua treats this chunk as absolutely correct way to
> write the following code:
> x = (f(a or b)):c()

It depends on the Lua version. It changed between Lua 5.1 and Lua 5.2,
A linebreak between a function and its arguments used to be illegal,
and an error message was issued. As from 5.2, no matter what whitespace
sits between the two parts (several blank lines if you like), x = (f(a
or b)):c()
is performed.

Reply | Threaded
Open this post in threaded view
|

Re: odd typo, what's happening

Coda Highland
On Sat, Dec 19, 2015 at 11:41 PM, Dirk Laurie <[hidden email]> wrote:

> 2015-12-20 6:19 GMT+02:00 Egor Skriptunoff <[hidden email]>:
>> On Fri, Dec 18, 2015 at 3:19 PM, Rena <[hidden email]> wrote:
>>>
>>> The exception is when it's ambiguous:
>>> x = f
>>> (a or b):c()
>>> That expression could be interpreted as two statements or one (remove the
>>> line break and see). In that case Lua is kind enough to make an exception to
>>> the "ignore whitespace" rule and raise an error
>>
>> Really?
>> I've always thought that Lua treats this chunk as absolutely correct way to
>> write the following code:
>> x = (f(a or b)):c()
>
> It depends on the Lua version. It changed between Lua 5.1 and Lua 5.2,
> A linebreak between a function and its arguments used to be illegal,
> and an error message was issued. As from 5.2, no matter what whitespace
> sits between the two parts (several blank lines if you like), x = (f(a
> or b)):c()
> is performed.
>

Let me just inject the words "Javascript semicolon insertion" into
this discussion and run away before people start having flashback
episodes. ;)

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: odd typo, what's happening

Soni "They/Them" L.
In reply to this post by Dirk Laurie-2


On 20/12/15 05:41 AM, Dirk Laurie wrote:

> 2015-12-20 6:19 GMT+02:00 Egor Skriptunoff <[hidden email]>:
>> On Fri, Dec 18, 2015 at 3:19 PM, Rena <[hidden email]> wrote:
>>> The exception is when it's ambiguous:
>>> x = f
>>> (a or b):c()
>>> That expression could be interpreted as two statements or one (remove the
>>> line break and see). In that case Lua is kind enough to make an exception to
>>> the "ignore whitespace" rule and raise an error
>> Really?
>> I've always thought that Lua treats this chunk as absolutely correct way to
>> write the following code:
>> x = (f(a or b)):c()
> It depends on the Lua version. It changed between Lua 5.1 and Lua 5.2,
> A linebreak between a function and its arguments used to be illegal,
> and an error message was issued. As from 5.2, no matter what whitespace
> sits between the two parts (several blank lines if you like), x = (f(a
> or b)):c()
> is performed.
>
It changed bewteen Lua 5.0 and Lua 5.2.

In Lua 5.0, a linebreak between a function and its arguments made 2
statements, not a function and its arguments. (so a newline worked like
a semicolon)
In Lua 5.1, it was changed to an error, as a sort of "transition period"
(see also: print[[a[[b]]--c]] in Lua 5.0 vs Lua 5.1 vs Lua 5.2).
In Lua 5.2, the error was removed.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: odd typo, what's happening

Dirk Laurie-2
2015-12-20 14:31 GMT+02:00 Soni L. <[hidden email]>:

>
>
> On 20/12/15 05:41 AM, Dirk Laurie wrote:
>>
>> 2015-12-20 6:19 GMT+02:00 Egor Skriptunoff <[hidden email]>:
>>>
>>> On Fri, Dec 18, 2015 at 3:19 PM, Rena <[hidden email]> wrote:
>>>>
>>>> The exception is when it's ambiguous:
>>>> x = f
>>>> (a or b):c()
>>>> That expression could be interpreted as two statements or one (remove
>>>> the
>>>> line break and see). In that case Lua is kind enough to make an
>>>> exception to
>>>> the "ignore whitespace" rule and raise an error
>>>
>>> Really?
>>> I've always thought that Lua treats this chunk as absolutely correct way
>>> to
>>> write the following code:
>>> x = (f(a or b)):c()
>>
>> It depends on the Lua version. It changed between Lua 5.1 and Lua 5.2,
>> A linebreak between a function and its arguments used to be illegal,
>> and an error message was issued. As from 5.2, no matter what whitespace
>> sits between the two parts (several blank lines if you like), x = (f(a
>> or b)):c()
>> is performed.
>>
> It changed bewteen Lua 5.0 and Lua 5.2.
>
> In Lua 5.0, a linebreak between a function and its arguments made 2
> statements, not a function and its arguments. (so a newline worked like a
> semicolon)

Did you try that out? On my system it does as I said.

Lua 5.0.3  Copyright (C) 1994-2006 Tecgraf, PUC-Rio
> a = {c=print}
> b = {c=print}
> a:c(); b:c()
table: 0x1003d40
table: 0x1004150
> f = function(t) if t==a then return b else return t end end
> f(a or b):c()
table: 0x1004150
> do
>> x = f
>> (a or b):c()
stdin:3: ambiguous syntax (function call x new statement) near `('

Reply | Threaded
Open this post in threaded view
|

Re: odd typo, what's happening

Soni "They/Them" L.


On 20/12/15 10:48 AM, Dirk Laurie wrote:

> 2015-12-20 14:31 GMT+02:00 Soni L. <[hidden email]>:
>>
>> On 20/12/15 05:41 AM, Dirk Laurie wrote:
>>> 2015-12-20 6:19 GMT+02:00 Egor Skriptunoff <[hidden email]>:
>>>> On Fri, Dec 18, 2015 at 3:19 PM, Rena <[hidden email]> wrote:
>>>>> The exception is when it's ambiguous:
>>>>> x = f
>>>>> (a or b):c()
>>>>> That expression could be interpreted as two statements or one (remove
>>>>> the
>>>>> line break and see). In that case Lua is kind enough to make an
>>>>> exception to
>>>>> the "ignore whitespace" rule and raise an error
>>>> Really?
>>>> I've always thought that Lua treats this chunk as absolutely correct way
>>>> to
>>>> write the following code:
>>>> x = (f(a or b)):c()
>>> It depends on the Lua version. It changed between Lua 5.1 and Lua 5.2,
>>> A linebreak between a function and its arguments used to be illegal,
>>> and an error message was issued. As from 5.2, no matter what whitespace
>>> sits between the two parts (several blank lines if you like), x = (f(a
>>> or b)):c()
>>> is performed.
>>>
>> It changed bewteen Lua 5.0 and Lua 5.2.
>>
>> In Lua 5.0, a linebreak between a function and its arguments made 2
>> statements, not a function and its arguments. (so a newline worked like a
>> semicolon)
> Did you try that out? On my system it does as I said.
>
> Lua 5.0.3  Copyright (C) 1994-2006 Tecgraf, PUC-Rio
>> a = {c=print}
>> b = {c=print}
>> a:c(); b:c()
> table: 0x1003d40
> table: 0x1004150
>> f = function(t) if t==a then return b else return t end end
>> f(a or b):c()
> table: 0x1004150
>> do
>>> x = f
>>> (a or b):c()
> stdin:3: ambiguous syntax (function call x new statement) near `('
>
Hmm... Older versions of Lua then? I looked at this a while ago but I
don't remember much of it anymore...

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: odd typo, what's happening

Dirk Laurie-2
2015-12-20 14:54 GMT+02:00 Soni L. <[hidden email]>:

>
>
> On 20/12/15 10:48 AM, Dirk Laurie wrote:
>>
>> 2015-12-20 14:31 GMT+02:00 Soni L. <[hidden email]>:
>>>
>>>
>>> On 20/12/15 05:41 AM, Dirk Laurie wrote:
>>>>
>>>> 2015-12-20 6:19 GMT+02:00 Egor Skriptunoff <[hidden email]>:
>>>>>
>>>>> On Fri, Dec 18, 2015 at 3:19 PM, Rena <[hidden email]> wrote:
>>>>>>
>>>>>> The exception is when it's ambiguous:
>>>>>> x = f
>>>>>> (a or b):c()
>>>>>> That expression could be interpreted as two statements or one (remove
>>>>>> the
>>>>>> line break and see). In that case Lua is kind enough to make an
>>>>>> exception to
>>>>>> the "ignore whitespace" rule and raise an error
>>>>>
>>>>> Really?
>>>>> I've always thought that Lua treats this chunk as absolutely correct
>>>>> way
>>>>> to
>>>>> write the following code:
>>>>> x = (f(a or b)):c()
>>>>
>>>> It depends on the Lua version. It changed between Lua 5.1 and Lua 5.2,
>>>> A linebreak between a function and its arguments used to be illegal,
>>>> and an error message was issued. As from 5.2, no matter what whitespace
>>>> sits between the two parts (several blank lines if you like), x = (f(a
>>>> or b)):c()
>>>> is performed.
>>>>
>>> It changed bewteen Lua 5.0 and Lua 5.2.
>>>
>>> In Lua 5.0, a linebreak between a function and its arguments made 2
>>> statements, not a function and its arguments. (so a newline worked like a
>>> semicolon)
>>
>> Did you try that out? On my system it does as I said.
>>
>> Lua 5.0.3  Copyright (C) 1994-2006 Tecgraf, PUC-Rio
>>>
>>> a = {c=print}
>>> b = {c=print}
>>> a:c(); b:c()
>>
>> table: 0x1003d40
>> table: 0x1004150
>>>
>>> f = function(t) if t==a then return b else return t end end
>>> f(a or b):c()
>>
>> table: 0x1004150
>>>
>>> do
>>>>
>>>> x = f
>>>> (a or b):c()
>>
>> stdin:3: ambiguous syntax (function call x new statement) near `('
>>
> Hmm... Older versions of Lua then? I looked at this a while ago but I don't
> remember much of it anymore...

In all versions of Lua, if you work interactively and the line loads
successfully, it will execute that and not wait for another line.
But not if you load a file.