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

  • barsoap@lemm.ee
    link
    fedilink
    arrow-up
    26
    arrow-down
    1
    ·
    edit-2
    1 day 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.

    • HiddenLayer555@lemmy.mlOP
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      17 hours ago

      How do you do nested parameterized types without it becoming ambiguous though? That’s IMO the biggest advantage of the bracket syntax. For example: Map<Tuple<Int, Int, Int> Int>

      • barsoap@lemm.ee
        link
        fedilink
        arrow-up
        4
        ·
        17 hours ago

        Map (Int, Int) Int. Kind of a bad example because tuples have special-case infix syntax, the general case would be Map Int (Either Int Bool). Follows the same exact syntax as function application just that types (by enforced convention) start upper case. Modulo technical wibbles to ensure that type inference is possible you can consider type constructors to be functions from types to types.

        …function application syntax is a story in itself in Haskell because foo a b c gets desugared to (((foo a) b) c): There’s only one-argument functions. If you want to have more arguments, accept an argument and return a function that accepts yet another argument. Then hide all that under syntactic sugar so that it looks innocent. And, of course, optimise it away when compiling. Thus you can write stuff like map (+5) xs in Haskell while other languages need the equivalent of map (\x -> x + 5) xs (imagine the \ is a lambda symbol).

    • weird@sub.wetshaving.social
      link
      fedilink
      arrow-up
      8
      arrow-down
      1
      ·
      1 day 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();

    • ulterno@programming.dev
      link
      fedilink
      English
      arrow-up
      0
      ·
      22 hours ago

      Until now, I looked at let and thought, “maybe they just felt like doing it that way”.
      Makes a lot more sense now.