• 0 Posts
  • 29 Comments
Joined 1 year ago
cake
Cake day: August 2nd, 2023

help-circle


  • Symbols display with friendly string-y names in a number of languages. Clojure, for example, has a symbol type.

    And a number of languages display friendly strings for enumy things - Scala, Haskell, and Rust spring to mind.

    The problem with strings over enums with a nice debugging display is that the string type is too wide. Strings don’t tell you what values are valid, strings don’t catch typos at compile time, and they’re murder when refactoring.

    Clojure symbols are good at differentiation between symbolly things and strings, though they don’t catch typos.

    The other problem the article mentions is strings over a proper struct/adt/class hierarchy is that strings don’t really have any structure to them. Concatenating strings is brittle compared to building up an AST then rendering it at the end.

    Edit: autocorrect messed a few things up I didn’t catch.





  • American cheese is not a fancy cheese, yeah. It also isn’t great cold.

    It is a pretty decent cheese sauce, though. It doesn’t taste starchy, with a muted cheese flavor like mornay does. If you put extra sharp cheddar or smoked gouda in homemade mornay or homemade American, the homemade American will be noticeably cheesier. Having made homemade mac and cheese a number of ways, I strongly prefer homemade American to any of the other recipes I know.

    I only really eat store-bought American melted, such as on eggs in a breakfast sandwich, on a burger or grilled cheese.

    Also, there’s plenty of decent American cheeses, like Humboldt fog, Maytag blue, or rogue river blue. American cheese is called that, but it doesn’t define American cheese making.




  • Javascript is generally considered OOP, but classes weren’t widely available till 2017.

    Inheritance isn’t fundamental to OOP, and neither are interfaces. You can have a duck- typed OOP language without inheritance, although I don’t know of any off the top of my head.

    Honestly, the more fundamental thing about OOP is that it’s a programming style built around objects. Sometimes OO languages are class based, or duck typing based, etc. But you’ll always have your data carrying around it’s behavior at runtime.


  • keeping state (data) and behavior (functions) that operate on that state, together

    Importantly, that’s “together at runtime”, not in terms of code organization. One of the important things about an object is that it has dynamic dispatch. Your object is a pointer both to the data itself and to the implementation that works on that data.

    There’s a similar idea that’s a bit different that you see in Haskell, Scala, and Rust - what Haskell calls type classes. Rust gives it a veneer of OO syntax, but the semantics themselves are interestingly different.

    In particular, the key of type classes is keeping data and behavior separate. The language itself is responsible for automagically passing in the behavior.

    So in Scala, you could do something like

    def sum[A](values: List[A])(implicit numDict: Num[A]) = values.fold(numDict.+)(numDict.zero)
    

    Or

    def sum[A: Num](values: List[A]) = values.fold(_ + _)(zero)
    

    Given a Num typeclass that encapsulates numeric operations. There’s a few important differences:

    1. All of the items of that list have to be the same type of number - they’re all Ints or all Doubles or something

    2. It’s a list of primitive numbers and the implementation is kept separate - no need for boxing and unboxing.

    3. Even if that list is empty, you still have access to the implementation, so you can return a type-appropriate zero value

    4. Generic types can conditionally implement a typeclass. For example, you can make an Eq instance for List[A] if A has an Eq instance. So you can compare List[Int] for equality, but not List[Int => Int].






  • Pipoca@lemmy.worldtoProgramming@programming.devRust vs C
    link
    fedilink
    arrow-up
    12
    arrow-down
    1
    ·
    8 months ago

    C is many things, but elegant really isn’t one of them.

    C has always been part of the “worse is better”/New Jersey school of thinking. The ultimate goal is simplicity. Particularly simplicity of language implementation, even if that makes programs written in that language more complex or error prone. It’s historically been a very successful approach.

    Rust, on the other hand, is part of “The Right Thing”/MIT approach. Simplicity is good, but it’s more important to be correct and complete even if it complicates things a bit.

    I don’t really think of void* and ubiquitous nulls, for example, as the hallmark of elegance, but as pretty simple, kludgey solutions.

    Rust, on the other hand, brings a lot of really elegant solutions from ML- family languages to a systems language. So you get algebraic data types, pattern matching, non-nullable references by default, closures, typeclasses, expression-oriented syntax, etc.


  • Just like walking doesn’t really compete, like at all, with flying in an aircraft, Functional and Object Oriented Programming are at their best when you use whichever approach makes sense for a given situation and in any reasonably complex software that means your code should be full of both.

    I’m not really sure sure that’s true.

    In FP languages like Haskell, you get tools like algebraic data types, typeclasses, and pattern matching.

    FP is really opposed to imperative programming, while objects are opposed to algebraic data types.

    You can write OO code that’s 100% fully functional, and you can write code in Haskell or rust where you barely notice you never once used an object.