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

  • GreatRam@lemmy.world
    link
    fedilink
    arrow-up
    29
    arrow-down
    3
    ·
    3 months ago

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

  • DreamButt@lemmy.world
    link
    fedilink
    English
    arrow-up
    12
    ·
    edit-2
    3 months 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

    • lobut@lemmy.ca
      link
      fedilink
      arrow-up
      2
      ·
      3 months ago

      I think you can do const thing = ... as constto lock down the mutation?

  • dan@upvote.au
    link
    fedilink
    arrow-up
    7
    ·
    3 months 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() {
    
      • dan@upvote.au
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        3 months 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.

        PHP’s object orientation is similar to languages like Java and C#, which is what I was comparing to.

        • sbv@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          1
          arrow-down
          1
          ·
          3 months ago

          Your example didn’t mention the use of the function keyword. Instead, it seemed to be questioning the placement of the return type - placing it after the argument list seems pretty common in newer languages.

    • HiddenLayer555@lemmy.mlOP
      link
      fedilink
      English
      arrow-up
      6
      ·
      3 months ago

      TIL PHP has statics.

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

  • JakenVeina@midwest.social
    link
    fedilink
    arrow-up
    6
    ·
    edit-2
    3 months 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.

  • barsoap@lemm.ee
    link
    fedilink
    arrow-up
    6
    arrow-down
    1
    ·
    edit-2
    3 months ago

    The actual reason why let … in syntax tends to not use C-style “type var” 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.

    • weird@sub.wetshaving.social
      link
      fedilink
      arrow-up
      1
      arrow-down
      1
      ·
      3 months ago

      There is also the thing where the compiler might mistake your c++ style variable declaration for a function, e.g.

      String myfunction():

      String myvariable();