Skip to content

Feature: Make "let" keyword for variable assignments in code blocks optional  #4245

Closed as not planned

Description

@markdoerr

Description

Dear Typst developers,
first of all I am very much impressed by what is already achieved in the Typst project - it is wonderful !

As a scientist and big admirer of a plain, memorable and simple syntax, I would suggest to make the
"let" keyword in code blocks optional for variable assignments.
Millions of lines of python code do to not need it, and mathematicians and physicists also do not write
their equations with an additional keyword to indicate a variable assignment:

x = 3

looks more familiar to scientist than

let x = 3

The python syntax parser manages to parse these statements well for decades and many young scientist
are familiar with python, so there is no reason to introduce a variable assignment keyword (or any other syntactic overhead) in a scripting environment.

Use Case

Use cases are endless: every superfluous word that does not need to be memorised and written saves time and makes the software more intuitive to be used.

With your experience in text parsing this should be a simple task, will not break the functionality and bring Typst even closer to your motto:
"We strive to make scientific writing simpler, more collaborative, and more fun."

Thanks :)

Activity

  1. added
    feature request
    on May 24, 2024
  2. jamesrswift commented on May 24, 2024

    @jamesrswift
    Sponsor

    I like this, but let also indicates a variable is being created within a scope as opposed to a variable being assigned to.

    It would work perfectly but would need to introduce some means to disambiguate when you intend to create a variable Vs reassignment of a variable

  3. markdoerr commented on May 24, 2024

    @markdoerr
    Author

    Thanks, @Jamesxx ,
    so 'let' is used similarly to the javascript 'let' and indicates block scope ?
    I could not find any hints in the documentation about variable scope (at least this should be made clearer then).

  4. markdoerr commented on May 24, 2024

    @markdoerr
    Author

    Maybe one way around this problem, @Jamesxx, would be to limit scope of variables to a code block by default, and if one wants to re-assign a (global) variable in a block - which is generally an error prone process and not recommended - one purposefully requires to add a "global" keyword ?

  5. bluebear94 commented on May 24, 2024

    @bluebear94
    Contributor

    I like having let for variable declarations, since it protects against typos.

  6. MDLC01 commented on May 24, 2024

    @MDLC01
    Collaborator

    Maybe one way around this problem, @Jamesxx, would be to limit scope of variables to a code block by default, and if one wants to re-assign a (global) variable in a block - which is generally an error prone process and not recommended - one purposefully requires to add a "global" keyword ?

    I believe is solution would make scripting in Typst less enjoyable. The way Typst works regarding variable declarations and assignment is very similar to most programming languages (with the notable exception of Python, which I would not say handles this specific matter well). I think having to learn a single keyword -- which happens to be the same as in many other languages -- is less of a problem for people who only know Python (or don't even know any programming language) than having a Python-like handling of variables is for people who want to use Typst as a scripting language.

  7. MDLC01 commented on May 24, 2024

    @MDLC01
    Collaborator

    so 'let' is used similarly to the javascript 'let' and indicates block scope ?
    I could not find any hints in the documentation about variable scope (at least this should be made clearer then).

    Regarding this specific issue, the documentation states that "Let bindings can be accessed for the rest of the containing block or document."

    More generally, this seems obvious to most Typst contributors because they happen to be used to programming languages working that way, but this comment illustrates that we must be careful not to think every Typst user has an experience with programming languages. We have to find the right balance between making the part of the language most people interact with require the least amount of pre-existing knowledge, and making the language generally work in a way that people who are used to programming expect a programming language to work.

  8. markdoerr commented on May 24, 2024

    @markdoerr
    Author

    @MDLC01 and @bluebear94,
    from a programmer's perspective you might be fully right, but Typst is mainly addressing scientist and scientist write usually mathematical equations without an extra keyword ;)
    Most scientist I know are only familiar with python and python is mainly taught nowadays at universities in natural science education, so I think, being a little bit more "Pythonic" would make many scientist more happy (the concept of a variable scope is more advanced to our science students - and less intuitive behavior should rather get a special keyword).

  9. laurmaedje commented on May 24, 2024

    @laurmaedje
    Member

    I like Python, but the need for nonlocal and global was always odd and this is what let prevents.

    Moreover bindings in code and markup are consistent currently and this would change that (since in markup we need the keyword to distinguish from just an interpolated variable).

  10. added
    syntax
    change request
    and removed
    feature request
    on May 24, 2024
  11. markdoerr commented on May 24, 2024

    @markdoerr
    Author

    so 'let' is used similarly to the javascript 'let' and indicates block scope ?
    I could not find any hints in the documentation about variable scope (at least this should be made clearer then).

    Regarding this specific issue, the documentation states that "Let bindings can be accessed for the rest of the containing block or document."

    More generally, this seems obvious to most Typst contributors because they happen to be used to programming languages working that way, but this comment illustrates that we must be careful not to think every Typst user has an experience with programming languages. We have to find the right balance between making the part of the language most people interact with require the least amount of pre-existing knowledge, and making the language generally work in a way that people who are used to programming expect a programming language to work.

    Thanks for the documentation reference, @MDLC01. The statement "Let bindings can be accessed for the rest of the containing block or document." is unfortunately ambiguous: It can mean "Let bindings can be accessed for the rest of the containing block or let bindings can be accessed for the rest of the document - even if it was defined in a block". Or it could mean: "Let bindings can be accessed for the rest of the containing block - if a variable is defined in the block - or the variable is global for the rest of the document, if it is not defined in a particular block". It should be made very clear in the documentation that let bindings are block scoped (as in most modern programming languages). => "Explicit is better than implicit." ;)

  12. markdoerr commented on May 24, 2024

    @markdoerr
    Author

    I like Python, but the need for nonlocal and global was always add and this is what let prevents.

    Moreover bindings in code and markup are consistent currently and this would change that (since in markup we need the keyword to distinguish from just an interpolated variable).

    After all discussions, I understand better the reasoning for the design decision, but it would be nice, to explain the variable scope more clearly in the Typst documentation as mentioned above. This makes the introduction of a "let" keyword more understandable (and maybe more memorisable for the non-programmer/non-computer science students/scientists).

    Alternatively one could consider the ":=" declaration syntax of go, which originated from mathematics and is closer to what common scientists know.

  13. Enivex commented on May 24, 2024

    @Enivex
    Collaborator

    Alternatively one could consider the ":=" declaration syntax of go, which originated from mathematics and is closer to what common scientists know.

    let is very easy to notice at a glance, especially with syntax highlighting. The tiny colon is not.

  14. MDLC01 commented on May 24, 2024

    @MDLC01
    Collaborator

    It should be made very clear in the documentation that let bindings are block scoped (as in most modern programming languages).

    Do you think the following would be explicit enough?

    Let bindings can be accessed for the rest of the containing block (or for the rest of the document, if there is no containing block).

    This is technically a bit wrong because a file does not necessarily correspond to a document (and global variables can be imported to other files), but I think this is acceptable given that this page is not made for package creators.

  15. markdoerr commented on May 24, 2024

    @markdoerr
    Author

    Yes, @MDLC01,
    I think, this formulation is more explicit and clear.
    What, if is additionally stated: "Variables, defined by "let" have global scope, if they are not defined in a block." ?
    (This would resolve your remark about including variables from other files.)

  16. MDLC01 commented on May 24, 2024

    @MDLC01
    Collaborator

    The problem is that they don't really have a global scope in that the following is invalid:

    #let b = a // Error: `a` is not yet defined.
    #let a = 1
  17. markdoerr commented on May 24, 2024

    @markdoerr
    Author

    The problem is that they don't really have a global scope in that the following is invalid:

    #let b = a // Error: `a` is not yet defined.
    #let a = 1

    I see, what you mean. Then what about this formulation:
    "Let bindings can be accessed for the rest of the containing block (or for the rest of the document, if they are not defined in a block, even if a Typst document consists of multiple files, chained by #import statements ). The "let" keyword indicates the position from which the (new) definition of a variable is valid".

  18. MDLC01 commented on May 24, 2024

    @MDLC01
    Collaborator

    even if a Typst document consists of multiple files, chained by #import statements

    I feel like this exposes beginners to too much complexity. I think it would be better to find a wording that is correct, but does not feel complex (meaning beginners would not necessarily have to understand all the implications of the specific wording).

  19. laurmaedje commented on May 24, 2024

    @laurmaedje
    Member

    Why not just replace "document" with "file"?

  20. jamesrswift commented on May 24, 2024

    @jamesrswift
    Sponsor

    I think the only issue (if there is one) is the assumed knowledge that a file is also it's own scope. For novice users, that's not going to make much material difference anyway.

  21. markdoerr commented on May 24, 2024

    @markdoerr
    Author

    OK, then we should have a final formulation:

    "(New) variables are introduced by the "let" keyword and can be accessed for the rest of the containing block (or for the rest of the file, if there is no containing block)"

  22. laurmaedje commented on Jun 3, 2024

    @laurmaedje
    Member

    Docs were rephrased in #4250, so I'll close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    change requestsyntax

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions