CppCon talk about Lua

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

CppCon talk about Lua

Andreas Weis
Hi everyone,

I gave an introductory talk about Lua for C++ programmers at CppCon a
couple of weeks ago. CppCon is the biggest international conference for
C++ developers, so the idea behind the talk was to get experienced C++
programmers who don't really know Lua interested in the language and
show that it is easy to integrate with an existing C++ codebase.

You can find a video of the talk on YouTube:
https://www.youtube.com/watch?v=pfwHCiP1HFM

The reason why I'm posting this here is that I will give a longer
version of the same talk at another conference later this year
(code::dive in Poland) and I would be very grateful for any feedback
from the Lua community.
If you feel that I gravely misrepresented some aspect of the language or
you think that there is something that is clearly missing from the
presentation, I will gladly take your advice into consideration.

I already got feedback that people would like to see some examples of
using a library for the C++ bindings and I plan to add some Sol2 sample
code for that.
If you can think of anything else, please let me know!

Best regards,
Andreas

Reply | Threaded
Open this post in threaded view
|

Re: CppCon talk about Lua

Javier Guerra Giraldez
On 30 October 2017 at 21:05, Andreas Weis <[hidden email]> wrote:
> If you feel that I gravely misrepresented some aspect of the language or
> you think that there is something that is clearly missing from the
> presentation, I will gladly take your advice into consideration.

arghh.... so many semicolons...


a curious deviation: when i write javascript, i put a semicolon on
every line, but that's because JS actually ends every statement with a
semicolon.  of course you can omit them but then the "automatic
semicolon insertion" happens.... I prefer to put them myself, thankyou
very much.  (yes, i know that "modern JS developers" hate me for that)

but Lua doesn't work like that.  in fact, in most cases you don't need
_any_ separator between statements beyond some whitespace, "x=4 y=6"
is perfectly valid, and at no point the parser insert anything between
those two.  the semicolon is just a valid separator that helps to
disambiguate some dark corner cases.


apart from that, i liked the presentation very much.  especially about
how modern C++ can help with the always uncomfortable case of managing
dynamic types on a statically typed language.



--
Javier

Reply | Threaded
Open this post in threaded view
|

Re: CppCon talk about Lua

aryajur
Very nice talk. Thank you. About the debug question in the end it may be helpful to mention that Lua code running inside the C/C++ application can be debugged using an available debugger interface like zerobrane studio.

On Oct 30, 2017 3:03 PM, "Javier Guerra Giraldez" <[hidden email]> wrote:
On 30 October 2017 at 21:05, Andreas Weis <[hidden email]> wrote:
> If you feel that I gravely misrepresented some aspect of the language or
> you think that there is something that is clearly missing from the
> presentation, I will gladly take your advice into consideration.

arghh.... so many semicolons...


a curious deviation: when i write javascript, i put a semicolon on
every line, but that's because JS actually ends every statement with a
semicolon.  of course you can omit them but then the "automatic
semicolon insertion" happens.... I prefer to put them myself, thankyou
very much.  (yes, i know that "modern JS developers" hate me for that)

but Lua doesn't work like that.  in fact, in most cases you don't need
_any_ separator between statements beyond some whitespace, "x=4 y=6"
is perfectly valid, and at no point the parser insert anything between
those two.  the semicolon is just a valid separator that helps to
disambiguate some dark corner cases.


apart from that, i liked the presentation very much.  especially about
how modern C++ can help with the always uncomfortable case of managing
dynamic types on a statically typed language.



--
Javier

Reply | Threaded
Open this post in threaded view
|

Re: CppCon talk about Lua

Kevin Martin
In reply to this post by Andreas Weis
Hi,

I thought it was a really good presentation. I really like the call function as I can see that it would simplify a lot of our code.

> On 30 Oct 2017, at 21:05, Andreas Weis <[hidden email]> wrote:
>
> If you feel that I gravely misrepresented some aspect of the language or
> you think that there is something that is clearly missing from the
> presentation, I will gladly take your advice into consideration.

One thing that comes to mind is our biggest pain point, which is error handling. I have found very little guidance on mixing c++ exceptions and Lua.

We compile Lua as C (because we don’t really understand LUAI_THROW/LUAI_CATCH and how they mix with our own exceptions). Our understanding is that if we’re inside a C++ function, and a lua api function errors, our destructors won’t be called because it will longjmp to the nearest pcall. The most common example is calling lua_gettable on something with an __index metamethod - as there is no way to know prior to trying the gettable that the metamethod will fail.

At the moment we wrap all our code in try/catch blocks and translate any c++ exceptions to lua errors, we use lua_pcall instead of lua_call, and from the lua side, we try to ensure that nothing else can error while we’re inside a try block and there are objects that need destructors called.

Thanks,
Kev
Reply | Threaded
Open this post in threaded view
|

Re: CppCon talk about Lua

Andreas Weis
In reply to this post by aryajur
The semicolon question is a difficult one. While Lua programmers like to
omit them, many folks coming from C-like languages seem to want to write
them (at least during an initial transitioning period). I decided to
keep them on the slides here, but I do agree that it's not idiomatic Lua
style.

Thank you for the hint with the debugger interface, I will take a look
at it.

On 10/31/2017 3:18 AM, Milind Gupta wrote:

> Very nice talk. Thank you. About the debug question in the end it may be
> helpful to mention that Lua code running inside the C/C++ application
> can be debugged using an available debugger interface like zerobrane studio.
>
> On Oct 30, 2017 3:03 PM, "Javier Guerra Giraldez" <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     On 30 October 2017 at 21:05, Andreas Weis
>     <[hidden email] <mailto:[hidden email]>> wrote:
>      > If you feel that I gravely misrepresented some aspect of the
>     language or
>      > you think that there is something that is clearly missing from the
>      > presentation, I will gladly take your advice into consideration.
>
>     arghh.... so many semicolons...
>
>
>     a curious deviation: when i write javascript, i put a semicolon on
>     every line, but that's because JS actually ends every statement with a
>     semicolon.  of course you can omit them but then the "automatic
>     semicolon insertion" happens.... I prefer to put them myself, thankyou
>     very much.  (yes, i know that "modern JS developers" hate me for that)
>
>     but Lua doesn't work like that.  in fact, in most cases you don't need
>     _any_ separator between statements beyond some whitespace, "x=4 y=6"
>     is perfectly valid, and at no point the parser insert anything between
>     those two.  the semicolon is just a valid separator that helps to
>     disambiguate some dark corner cases.
>
>
>     apart from that, i liked the presentation very much.  especially about
>     how modern C++ can help with the always uncomfortable case of managing
>     dynamic types on a statically typed language.
>
>
>
>     --
>     Javier
>

Reply | Threaded
Open this post in threaded view
|

Re: CppCon talk about Lua

Andreas Weis
In reply to this post by Kevin Martin
Good point about the error handling.
I thought about this too, but unfortunately I found the topic to be too
complex to reasonably fit into the time available for such a talk.
In later Lua versions you can compile the source as C++ code and
configure it to use exceptions instead of longjmps, but I would already
consider that a fairly advanced topic.

Thank you all for the kind words and the very valuable suggestions!

Best regards,
Andreas

On 10/31/2017 10:07 AM, Kevin Martin wrote:

> Hi,
>
> I thought it was a really good presentation. I really like the call function as I can see that it would simplify a lot of our code.
>
>> On 30 Oct 2017, at 21:05, Andreas Weis <[hidden email]> wrote:
>>
>> If you feel that I gravely misrepresented some aspect of the language or
>> you think that there is something that is clearly missing from the
>> presentation, I will gladly take your advice into consideration.
>
> One thing that comes to mind is our biggest pain point, which is error handling. I have found very little guidance on mixing c++ exceptions and Lua.
>
> We compile Lua as C (because we don’t really understand LUAI_THROW/LUAI_CATCH and how they mix with our own exceptions). Our understanding is that if we’re inside a C++ function, and a lua api function errors, our destructors won’t be called because it will longjmp to the nearest pcall. The most common example is calling lua_gettable on something with an __index metamethod - as there is no way to know prior to trying the gettable that the metamethod will fail.
>
> At the moment we wrap all our code in try/catch blocks and translate any c++ exceptions to lua errors, we use lua_pcall instead of lua_call, and from the lua side, we try to ensure that nothing else can error while we’re inside a try block and there are objects that need destructors called.
>
> Thanks,
> Kev
>

Reply | Threaded
Open this post in threaded view
|

Re: CppCon talk about Lua

Vadi
I'd also recommend mentioning ZeroBrane as a GUI debugger.

On Fri, Nov 3, 2017 at 7:53 AM Andreas Weis <[hidden email]> wrote:
Good point about the error handling.
I thought about this too, but unfortunately I found the topic to be too
complex to reasonably fit into the time available for such a talk.
In later Lua versions you can compile the source as C++ code and
configure it to use exceptions instead of longjmps, but I would already
consider that a fairly advanced topic.

Thank you all for the kind words and the very valuable suggestions!

Best regards,
Andreas

On 10/31/2017 10:07 AM, Kevin Martin wrote:
> Hi,
>
> I thought it was a really good presentation. I really like the call function as I can see that it would simplify a lot of our code.
>
>> On 30 Oct 2017, at 21:05, Andreas Weis <[hidden email]> wrote:
>>
>> If you feel that I gravely misrepresented some aspect of the language or
>> you think that there is something that is clearly missing from the
>> presentation, I will gladly take your advice into consideration.
>
> One thing that comes to mind is our biggest pain point, which is error handling. I have found very little guidance on mixing c++ exceptions and Lua.
>
> We compile Lua as C (because we don’t really understand LUAI_THROW/LUAI_CATCH and how they mix with our own exceptions). Our understanding is that if we’re inside a C++ function, and a lua api function errors, our destructors won’t be called because it will longjmp to the nearest pcall. The most common example is calling lua_gettable on something with an __index metamethod - as there is no way to know prior to trying the gettable that the metamethod will fail.
>
> At the moment we wrap all our code in try/catch blocks and translate any c++ exceptions to lua errors, we use lua_pcall instead of lua_call, and from the lua side, we try to ensure that nothing else can error while we’re inside a try block and there are objects that need destructors called.
>
> Thanks,
> Kev
>

Reply | Threaded
Open this post in threaded view
|

Re: CppCon talk about Lua

Ulrich Schmidt
In reply to this post by Andreas Weis
Am 03.11.2017 um 07:45 schrieb Andreas Weis:
> The semicolon question is a difficult one. While Lua programmers like to
> omit them, many folks coming from C-like languages seem to want to write
> them (at least during an initial transitioning period). I decided to
> keep them on the slides here, but I do agree that it's not idiomatic Lua
> style.
>
<JUSTMYTWOCENTS>
(I did not start this discussion before,because i dont want to start a
not ending discussion:
I write semicolons EVERYWHERE i can, because it helps me reading and
understanding the code; If i try to use a free module from the web, I
crerate a test case and step throu the modules functions. I try to
understand what the module does. Within this proces i reformat the
indentation and the line breaks.

I like lua because it is like it is, BUT the one and only not well
defined feature of this language is the statement delimitter. From my
point of view omitting the semicolons is without any benefit except
honoring the lazyness of the programmer. A few days ago i came over
something like the following code lines:

   func(blabla)
   ;(higgs or superman)(donald)
   morefuncs()

looks strange to me. I reformatted the code sequence this way:

   func(blabla);
   (higgs or superman)(donald);
   morefuncs();

Many authors of free software spending a lot of effort into writing a
good documentation so others can easily understand there modules. And
then they make it harder to understand by different indentations styles
and omitting semicolons like in my example before.

</JUSTMYTWOCENTS>

Reply | Threaded
Open this post in threaded view
|

Semicolons in Lua (was CppCon talk about Lua)

Hugo Musso Gualandi
On Fri, 2017-11-03 at 08:36 +0100, Ulrich Schmidt wrote:

> A few days ago i came over something like the following code lines:
>
>    func(blabla)
>    ;(higgs or superman)(donald)
>    morefuncs()
>
> looks strange to me. I reformatted the code sequence this way:
>
>    func(blabla);
>    (higgs or superman)(donald);
>    morefuncs();

Instead of adding semicolons, I would prefer to split the function call
into two lines. This way the person reading the code doesn't need to
understand that one tricky corner case of the Lua syntax.

    local person = higgs or superman
    person(donald)

You can also use a do-end block if you want to limit the scope of the
new variable:

    do
        local person = higgs or superman
        person(donald)
    end

I don't like the putting a semicolon after *every* statement because it
I think it can teach the wrong thing about the Lua syntax. It gives the
impression that any statement could potentially be ambiguous without a
semicolon after it when in fact the only ambiguity in the language
involves the the rare `()()` function call statement. And in that one
ambiguity the actual solution is to put the semicolon *before* the
confusing statement, not after it.

One thing that I have seen happen in Javascript is people writing
things like the following, thinking it would avoid the problem

    func(blabla)
    (higgs or superman)(donald);
    morefuncs()

Often this happened when using a build system that concatenates
multiple Javascript files into a single file. In Javascript it was
common for modules to start with a `()()` function call statement
because of the Immediately Invoked Function Expression pattern.

Reply | Threaded
Open this post in threaded view
|

Re: Semicolons in Lua (was CppCon talk about Lua)

dyngeccetor8
In reply to this post by Ulrich Schmidt
On 11/03/2017 07:36 AM, Ulrich Schmidt wrote:
> I write semicolons EVERYWHERE i can, because it helps me reading and
> understanding the code; If i try to use a free module from the web, I
> crerate a test case and step throu the modules functions. I try to
> understand what the module does. Within this proces i reformat the
> indentation and the line breaks.

While experimenting with different formatting styles of Lua code I've
discovered that placing semicolon after every statement increases my
code perception.

The problem for me is that looks natural in reading is not natural
in writing. So I'm applying Occam's razor, not use semicolons,
distinct delimiters in tables, space paddings to make code look nice
in monospaced font and such things.

(Dreaming that maybe past another sixty years we'll develop programs to
represent code contents according our tastes.)

-- Martin

Reply | Threaded
Open this post in threaded view
|

Re: Semicolons in Lua (was CppCon talk about Lua)

Soni "They/Them" L.
In reply to this post by Hugo Musso Gualandi


On 2017-11-03 12:46 PM, Hugo Musso Gualandi wrote:

> On Fri, 2017-11-03 at 08:36 +0100, Ulrich Schmidt wrote:
>> A few days ago i came over something like the following code lines:
>>
>>     func(blabla)
>>     ;(higgs or superman)(donald)
>>     morefuncs()
>>
>> looks strange to me. I reformatted the code sequence this way:
>>
>>     func(blabla);
>>     (higgs or superman)(donald);
>>     morefuncs();
> Instead of adding semicolons, I would prefer to split the function call
> into two lines. This way the person reading the code doesn't need to
> understand that one tricky corner case of the Lua syntax.
>
>      local person = higgs or superman
>      person(donald)
>
> You can also use a do-end block if you want to limit the scope of the
> new variable:
>
>      do
>          local person = higgs or superman
>          person(donald)
>      end
>
> I don't like the putting a semicolon after *every* statement because it
> I think it can teach the wrong thing about the Lua syntax. It gives the
> impression that any statement could potentially be ambiguous without a
> semicolon after it when in fact the only ambiguity in the language
> involves the the rare `()()` function call statement. And in that one
> ambiguity the actual solution is to put the semicolon *before* the
> confusing statement, not after it.
>
> One thing that I have seen happen in Javascript is people writing
> things like the following, thinking it would avoid the problem
>
>      func(blabla)
>      (higgs or superman)(donald);
>      morefuncs()
>
> Often this happened when using a build system that concatenates
> multiple Javascript files into a single file. In Javascript it was
> common for modules to start with a `()()` function call statement
> because of the Immediately Invoked Function Expression pattern.
>

I understand what you're saying, but ; is the identity function.

;(x) is x.

--
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: Semicolons in Lua (was CppCon talk about Lua)

Martin
On 11/04/2017 01:23 PM, Soni L. wrote:
> I understand what you're saying, but ; is the identity function.
>
> ;(x) is x.
>

Really? f(;, {;=;})

Reply | Threaded
Open this post in threaded view
|

Re: Semicolons in Lua (was CppCon talk about Lua)

Coda Highland
On Sat, Nov 4, 2017 at 11:34 AM, Martin <[hidden email]> wrote:
> On 11/04/2017 01:23 PM, Soni L. wrote:
>> I understand what you're saying, but ; is the identity function.
>>
>> ;(x) is x.
>>
>
> Really? f(;, {;=;})
>

No, not REALLY. He was suggesting a way to think about it.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: Semicolons in Lua (was CppCon talk about Lua)

Soni "They/Them" L.


On 2017-11-04 11:50 AM, Coda Highland wrote:

> On Sat, Nov 4, 2017 at 11:34 AM, Martin <[hidden email]> wrote:
>> On 11/04/2017 01:23 PM, Soni L. wrote:
>>> I understand what you're saying, but ; is the identity function.
>>>
>>> ;(x) is x.
>>>
>> Really? f(;, {;=;})
>>
> No, not REALLY. He was suggesting a way to think about it.
>
> /s/ Adam
>

They* but yes it was just a silly joke.

--
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: Semicolons in Lua (was CppCon talk about Lua)

Ulrich Schmidt

Am 04.11.2017 um 21:18 schrieb Soni L.:
 >
 >
 > On 2017-11-04 11:50 AM, Coda Highland wrote:
 >> On Sat, Nov 4, 2017 at 11:34 AM, Martin
<[hidden email]> wrote:
 >>> On 11/04/2017 01:23 PM, Soni L. wrote:
 >>>> I understand what you're saying, but ; is the identity function.
 >>>>
 >>>> ;(x) is x.
 >>>>
 >>> Really? f(;, {;=;})
 >>>
 >> No, not REALLY. He was suggesting a way to think about it.
 >>
 >> /s/ Adam
 >>
 >
 > They* but yes it was just a silly joke.
 >

Back to topic: Andreas asked how to improve his talk. He try to get
people intrested in lua, people who are doing her dayly job in languages
like c, c++, java, ...

 From my point of view there are 2 different kinds of lua users.

1. The users who grow with the lua language since version 1.
If i remember correctly. there was 1 statement per line and the
statement delimitter was the line end. There is nothing bad about that.
(I dont insert semicolons, when i write simple scripts to be executed
like batch files or shell scripts.)
But as lua grows, at some point, it was nesseary to implement a optional
statement delimitter. And there is nothing wrong with keeping old habits
not writing any staement delimitter until nessesary.

2. Lua users, like me, who lerned programming in other languages and
with lots of presumptions and habits in her head.
If you come from pascal, c, c++, .. you are trained to place statement
delimitters and you SEARCH automatically for statement delimitter in
sources you read.
For me ommiting semicolons in lua looks like basic. (I am not talking
about tiny lua shell scrips here)

As a member of Anderss's Auditorium and with a background like described
in 2.) it would be easier to me to read his examples if they contain
semicolons.

I am already happy, if you dont shoot me, when i use semicolons ;-)

Ulrich.

Reply | Threaded
Open this post in threaded view
|

Re: Semicolons in Lua (was CppCon talk about Lua)

Andrew Starks-2

On Sun, Nov 5, 2017 at 03:26 Ulrich Schmidt <[hidden email]> wrote:

Am 04.11.2017 um 21:18 schrieb Soni L.:
 >
 >
 > On 2017-11-04 11:50 AM, Coda Highland wrote:
 >> On Sat, Nov 4, 2017 at 11:34 AM, Martin
<[hidden email]> wrote:
 >>> On 11/04/2017 01:23 PM, Soni L. wrote:
 >>>> I understand what you're saying, but ; is the identity function.
 >>>>
 >>>> ;(x) is x.
 >>>>
 >>> Really? f(;, {;=;})
 >>>
 >> No, not REALLY. He was suggesting a way to think about it.
 >>
 >> /s/ Adam
 >>
 >
 > They* but yes it was just a silly joke.
 >

Back to topic: Andreas asked how to improve his talk. He try to get
people intrested in lua, people who are doing her dayly job in languages
like c, c++, java, ...

 From my point of view there are 2 different kinds of lua users.

1. The users who grow with the lua language since version 1.
If i remember correctly. there was 1 statement per line and the
statement delimitter was the line end. There is nothing bad about that.
(I dont insert semicolons, when i write simple scripts to be executed
like batch files or shell scripts.)
But as lua grows, at some point, it was nesseary to implement a optional
statement delimitter. And there is nothing wrong with keeping old habits
not writing any staement delimitter until nessesary.

2. Lua users, like me, who lerned programming in other languages and
with lots of presumptions and habits in her head.
If you come from pascal, c, c++, .. you are trained to place statement
delimitters and you SEARCH automatically for statement delimitter in
sources you read.
For me ommiting semicolons in lua looks like basic. (I am not talking
about tiny lua shell scrips here)

As a member of Anderss's Auditorium and with a background like described
in 2.) it would be easier to me to read his examples if they contain
semicolons.

I am already happy, if you dont shoot me, when i use semicolons ;-)

Ulrich.

It may be good to mention the reason behind the design choice. I remember reading that semicolons are optional because it looks and works better in applications where Lua is used as a DSL or as a configuration language. 

It’s also true that most Lua code that I’ve read leaves it off and that there are no incompatiblities created when you mix the two styles in the same project. Therefore, while it is likely to offend your own preference, and unlike the situation created by mixing different object models, it matters not how which way you go.
Reply | Threaded
Open this post in threaded view
|

Re: Semicolons in Lua (was CppCon talk about Lua)

Luiz Henrique de Figueiredo
In reply to this post by Ulrich Schmidt
> 1. The users who grow with the lua language since version 1.
> If i remember correctly. there was 1 statement per line and the
> statement delimitter was the line end.

You misremember. The code below works in Lua 1.0 (I've just tested):
        a=2 print(a)