Made with KolourPaint and screenshots from Kate (with the GitHub theme).

  • @barsoap@lemm.ee
    link
    fedilink
    72 hours ago

    The actual reason why let … in syntax tends to not use C-style “var type” like syntax is because it’s derived from the syntax type theory uses, and type theorists know about parameterised types. Generics, in C++ parlance, excuse my Haskell:

    let foo :: Map Int String = mempty

    We have an empty map, and it maps integers to Strings. We call it foo. Compare:

    Map Int String foo = mempty

    If nothing else, that’s just awkward to read and while it may be grammatically unambiguous (a token is a name if it sits directly in front of =) parser error messages are going to suck. Map<Int,String> is also awkward but alas that’s what we’re stuck with in Rust because they reasoned that it would be cruel to put folks coming from C++ on angle bracket withdrawal. Also Rust has ML ancestry don’t get me started on their type syntax.

    • @pivot_root@lemmy.world
      link
      fedilink
      10
      edit-2
      7 hours ago

      Rust is verbose, but C++ might still take the cake with its standard library templates. Especially when using fully-qualified type names…

      auto a = ::std::make_shared<::std::basic_string<char, ::std::char_traits<char>, MyAllocator<char>>>();

      A reference-counted shared pointer to a string of unspecified character encoding and using a non-default memory allocator.

        • @pivot_root@lemmy.world
          link
          fedilink
          2
          edit-2
          5 hours ago

          std::string doesn’t have a template type for the allocator. You are stuck using the verbose basic_string type if you need a special allocator.

          But, of course, nobody sane would write that by hand every time. They would use a typedef, like how std::string is just a typedef for std::basic_string<char, std::char_traits<char>, std::allocator<char>>. Regardless, the C++ standard library is insanely verbose when you start dropping down into template types and using features at an intermediate level. SFINAE in older versions of C++ was mindfuck on the best of days, for example.

          Don’t get me wrong, though. I’m not saying Rust is much better. Its saving grace is its type inference in let expressions. Without it, chaining functional operations on iterators would be an unfathomable hellscape of Collect<Skip<Map<vec::Iter<Item = &'a str>>>>

          • @Ajen@sh.itjust.works
            link
            fedilink
            45 hours ago

            Yeah, I missed the custom allocator at first. I thought I deleted my comment fast enough, but I guess you were faster. :)

  • @marcos@lemmy.world
    link
    fedilink
    4912 hours ago

    Good, now invent a keyword for variables you don’t want to declare the type. And now that you have a mix of keywords and identifiers on the same place, you can never update your language again.

    Also, make the function declarations not use a keyword too, so you get the full C-style madness of code that changes meaning depending on what libraries you import.

    • @ZILtoid1991@lemmy.world
      link
      fedilink
      22 hours ago

      Good, now invent a keyword for variables you don’t want to declare the type.

      auto. Also in D, you only need const if you don’t want to specify a type for a constant, the compiler automatically inferres it to you.

      Function declarations can be easily decyphered from context, no problem.

    • @stingpie@lemmy.world
      link
      fedilink
      1210 hours ago

      I don’t understand how not using a keyword to define a function causes the meaning to change depending on imports. I’ve never run into an issue like that before. Can you give an example?

      • @marcos@lemmy.world
        link
        fedilink
        49 hours ago

        Some declarations terminate on the name, other declarations go one requiring more tokens. In C, the only thing that differentiates them is the type.

        Parenthesis in particular are completely ambiguous. But asterisks and square brackets also create problems.

          • @sushibowl@feddit.nl
            link
            fedilink
            252 minutes ago

            The basic problem is that identifiers can be either types or variables, and without a keyword letting you know what kind of statement you’re dealing with, there’s no way of knowing without a complete identifier table. For example, what does this mean:

            foo * bar;
            

            If foo is a type, that is a pointer declaration. But if it’s a variable, that’s a multiplication expression. Here’s another simple one:

            foo(bar);
            

            Depending on how foo is defined, that could be a function call or a declaration of a variable bar of type foo, with some meaningless parentheses thrown in.

            When you mix things together it gets even more crazy. Check this example from this article:

            foo(*bar)();
            
            

            Is bar a pointer to a function returning foo, or is foo a function that takes a bar and returns a function pointer?

            let and fn keywords solve a lot of these ambiguity problems because they let the parser know what kind of statement it’s looking at, so it can know whether identifiers in certain positions refer to types or variables. That makes parsing easier to write and helps give nicer error messages.

        • @sus@programming.dev
          link
          fedilink
          13
          edit-2
          10 hours ago

          So I think it’s still probably unclear to people why “mix of keywords and identifiers” is bad: it means any new keyword could break backwards compatibility because someone could have already named a type the same thing as that new keyword.

          This syntax puts type identifiers in the very prominent position of “generic fresh statement after semicolon or newline”

          …though I’ve spent like 10 minutes thinking about this and now it’s again not making sense to me. Isn’t the very common plain “already_existing_variable = 5” also causing the same problem? We’d have to go back to cobol style “SET foo = 5” for everything to actually make it not an issue

          • @AnotherPenguin@programming.dev
            link
            fedilink
            English
            37 hours ago

            At least in C#, you can define variables with keyword names like this:

            var @struct = “abc”

            I think in Kotlin you can do the same, and even include spaces with backticks like val abstract class = “abc”

            I’m not sure if other languages allow that, regardless it should be rarely used.

            • @pivot_root@lemmy.world
              link
              fedilink
              36 hours ago

              Swift also uses backticks and Rust has a dumb one in the form of r#thekeyword. Still much better than introducing a async as a new keyword in a minor version of a language and breaking a bunch of libraries.

          • @piccolo@sh.itjust.works
            link
            fedilink
            310 hours ago

            Ah I was misunderstanding the problem. And learned something new about C#, seems in order to avoid breaking existing code they introduce “contextual keywords” var being added later, it is a contextual. You can create a class ‘var’ and the compiler will prefer it.

    • Voytrekk
      link
      fedilink
      English
      7
      edit-2
      11 hours ago

      C++ has auto, which determines the type automatically.

  • GreatRam
    link
    fedilink
    4413 hours ago

    You’re encoding more information in the typescript one. You’re saying it’s a string that will get updated.

    • @Hotzilla@sopuli.xyz
      link
      fedilink
      27 hours ago

      C# has const string a = “Hello, World”;

      var in js is legacy, and default should be let, but changing that would break everything

    • @masterspace@lemmy.ca
      link
      fedilink
      English
      2813 hours ago

      Yeah, it’s explicitly distinct from const a: String which says it won’t change, and var a: String, which means this is legacy code that needs fixing.

      • Psaldorn
        link
        fedilink
        813 hours ago

        If there’s only two options you only need one keyword

        • @Hotzilla@sopuli.xyz
          link
          fedilink
          3
          edit-2
          7 hours ago

          True, but var and let are not same in js, so there is three.

          if(true) {

          var a = "dumdum"

          }

          console.log(a)

          Is valid and functioning javascript. With let it is not.

    • @Scoopta@programming.dev
      link
      fedilink
      1713 hours ago

      You aren’t though. In most languages that use the latter declaration you would prefix the declaration with final or const or the like to specify it won’t be updated.

      • @calcopiritus@lemmy.world
        link
        fedilink
        1311 hours ago

        It’s also valid rust syntax.

        But if it were rust, this meme would not make sense, since you would just type let a and type inference would do its thing. Which is much more ergonomic.

        • Victor
          link
          fedilink
          130 minutes ago

          Type inference is a pretty big thing in TypeScript as well though. In fact it’s probably the biggest thing about it, IMO.

        • Victor
          link
          fedilink
          129 minutes ago

          I would because I know TypeScript and I don’t know Rust.

  • DreamButt
    link
    fedilink
    English
    19
    edit-2
    11 hours ago

    Because sometimes that let can be replaced by other things like const. Which can be managed statically by the machine and not by my (imperfect) ability to know if it’s mutated or not

      • @ZILtoid1991@lemmy.world
        link
        fedilink
        22 hours ago

        So is let in some languages. In Rust, you have to constantly opt out from immutability with let mut, which makes writing more procedural code feel like you’re fighting with the compiler, and otherwise I don’t really see the rationale behind full functional coding. I only had a bug caused only once by unwanted mutation, the hardest part fixing it was to learn the proper use of my debugger tool.

    • @Scoopta@programming.dev
      link
      fedilink
      111 hours ago

      Ok but, in the second example you typically just put final or const in front of the type to denote immutability. I still don’t see the advantage to the first declaration.

      • DreamButt
        link
        fedilink
        English
        211 hours ago

        oh for sure, but I think that’s the rarer case for language implementions. Having a consistent structure with alternative keywords in static positions is just easier to develop an AST for. Personally my favorite language doesn’t even allow for const values (except by convention) so it’s really just a matter of preference

        • @Scoopta@programming.dev
          link
          fedilink
          111 hours ago

          Is it rarer? I think a lot of modern languages go for the first option but pretty much all C style languages use the latter. It’s probably a wash for which is more popular I’d think.

          • DreamButt
            link
            fedilink
            English
            111 hours ago

            I’m talking about quantity not the popularity of a given language. There are certainly a number of popular languages that follow that convention

  • @JakenVeina@midwest.social
    link
    fedilink
    9
    edit-2
    11 hours ago

    Not to short-circuit the joke, but in this case, it’s because the valid JavaScript version is…

    let a
    

    …and one of TypeScript’s main design goals is to be a superset of JavaScript, that only adds syntax, and doesn’t re-write it.

    Beyond that, it’s probably a case of some new language just using what the designer is familiar with.

  • tisktisk
    link
    fedilink
    English
    1313 hours ago

    I’ve always wondered where all this ‘let’ business started

    • @HiddenLayer555@lemmy.mlOP
      link
      fedilink
      English
      26
      edit-2
      13 hours ago

      It’s commonly used in math to declare variables so I assume programming languages borrowed it from there.

      • @chaos@beehaw.org
        link
        fedilink
        77 hours ago

        More specifically, they’re borrowing the more mathematical meaning of variables, where if you say x equals 5, you can’t later say x is 6, and where a statement like “x = x + 1” is nonsense. Using “let” means you’re setting the value once and that’s what it’s going to remain as long as it exists, while “var” variables can be changed later. Functional languages, which are usually made by very math-y people, will often protest the way programmers use operators by saying that = is strictly for equality and variable assignment is := instead of == and = in most C-style languages.

      • @bandwidthcrisis@lemmy.world
        link
        fedilink
        613 hours ago

        BASIC uses (used?) it to declare variables. (I don’t know if earlier languages did.)

        Not that that’s a reason for other languages to copy it.

          • @dan@upvote.au
            link
            fedilink
            9
            edit-2
            12 hours ago

            Older variants used DIM for arrays and LET for other variables. DIM was originally called that because it was setting the dimensions of the array.

            In modern BASIC variants, DIM has become a backronym: “declare in memory”.

            • tisktisk
              link
              fedilink
              English
              412 hours ago

              TIL Backronyms and cuil BASIC technicalities Much obliged all

            • @marcos@lemmy.world
              link
              fedilink
              212 hours ago

              Even older variants required both a let to declare the variable and a dim to set its size.

              I remember a REDIM command, but I really can’t remember what basic it’s from.

              • @dan@upvote.au
                link
                fedilink
                211 hours ago

                The first programming language I used was Visual Basic (both VBA in Excel, and VB3 then VB6). I think it used redim to resize arrays.

      • tisktisk
        link
        fedilink
        English
        512 hours ago

        I doubted you until I got about halfway through this whole page. I concede tho–you are most correct lol Still a decent read and for that I thank you

  • @dan@upvote.au
    link
    fedilink
    712 hours ago

    Can we talk about PHP functions with typehints too?

    public static function foo(): string {
    

    Practically every other language with similar syntax does this instead:

    public static string foo() {
    
    • @HiddenLayer555@lemmy.mlOP
      link
      fedilink
      English
      612 hours ago

      TIL PHP has statics.

      Also, does PHP actually enforce the type declarations? I’d assume it would but knowing PHP…

    • @calcopiritus@lemmy.world
      link
      fedilink
      211 hours ago

      JavaScript (Typescript for the type part) and python, the most popular scripting languages, use the same order as PHP.

      It’s usually compiled languages that do the other one.

      • @dan@upvote.au
        link
        fedilink
        210 hours ago

        TypeScript doesn’t need the “function” keyword for a method in an object or on a class though.

        const foo = {
          bar(): string {
           ... 
          } 
        }
        

        which I assume is doable because the syntax is unambiguous.

        In PHP’s case, the method syntax should also be unambiguous.

  • @Oisteink@feddit.nl
    link
    fedilink
    212 hours ago

    First time i used let it was to inline variable declaration with assignment . Can’t remember the language.