You could be doing more with static typing in Gleam

Поделиться
HTML-код
  • Опубликовано: 12 янв 2025

Комментарии • 35

  • @IsaacHarrisHolt
    @IsaacHarrisHolt  День назад

    The first 500 people to use my link skl.sh/isaacharrisholt01251 will get a 1 month free trial of Skillshare!

  • @taquanminhlong
    @taquanminhlong 19 часов назад +9

    Now I can finally understand the rust phantom type here 😂😂😂

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  18 часов назад

      Awesome! Glad this was useful

    • @khai96x
      @khai96x 6 часов назад

      Phantom types in Rust are necessary only because sometimes types need extra information to pass to trait impls. I'm pretty sure they weren't made with typestate/newtype pattern in mind.

  • @TheOlian04
    @TheOlian04 6 часов назад +1

    THIS is the kind of gleam videos we need! More of this please :)

  • @MrMysticphantom
    @MrMysticphantom 22 часа назад +3

    As someone who develops monitoring systems, CRMs, ERPs and ticketing systems this is hitting very very close to the pain i normally deal with. Many programmers sadly really underestimate the importance of business logic flaws, let alone making phantom types or similar mechanisms to restrict behavior.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  22 часа назад

      Yeah, it's definitely one of those "be the change you want to see" things. If you can start implementing them in the bits of the codebase you own, you may be able to start convincing people through the organisation to use them

  • @ivymuncher
    @ivymuncher 9 часов назад

    the camera fix is looking good!!

  • @Biriadan
    @Biriadan 13 часов назад

    You can get this concept to dynamically typed languages as well! Clojure supports metadata on elements, which can contain units of measure, provenance, etc. The book Software Design for Flexibility leverages metadata in scheme to implement an automatic type conversion system.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 часов назад +1

      Interesting! I don't know how many languages are likely to support it, but that's pretty cool

  • @BlueishSapphire7
    @BlueishSapphire7 18 часов назад

    Amazing video, I had never heard of phantom types until now. A small suggestion, it would be helpful if you zoomed in on the code a little more in the video because it was very hard to see on mobile

  • @khai96x
    @khai96x 6 часов назад

    What are the advantages of phantom type parameters over new type wrappers?

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  5 часов назад +1

      You can make generic functions with them across many different phantom types.
      For example, with the `Distance(a)` type, I could write:
      `fn add(dst1: Distance(a), dst2: Distance(a)) -> Distance(a)`
      Then I could add ANY two distances, as long as they have the same phantom type.
      If I was using new type wrappers, I'd have to have an add_miles, add_kilometres function, etc.
      Also, phantom types have no runtime overhead, as they're compile-time only.

  • @ballackuk13
    @ballackuk13 18 часов назад

    I'm sure most of the time we care about runtime bugs, thats why we do testing

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 часов назад

      Sure, but another tool in your belt is always helpful!

  • @AGeekTragedy
    @AGeekTragedy 8 часов назад

    Any video discussing strongly typed physical units/quantities is an automatic like from me. How easy is it in Gleam to have it work out the correct return type of (say) dividing a quantity in metres by a quantity in seconds?

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  7 часов назад

      Very easy! Phantom types are just generic type parameters at the end of the day.
      `fn calculate_speed(dst: Distance(a), time: Time(b)) -> Speed(a, b)`
      You'd have to write it as a function though, and have a `Speed(a)` type. You wouldn't be able to just use primitive operators (which makes sense as `Distance` and `Time` aren't primitives)

  • @applimu7992
    @applimu7992 23 часа назад +1

    Phantom types look similar to dependent types!

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  22 часа назад

      Phantom types are probably closer to branded types. Dependent types usually require the compiler to know something about the actual runtime value (e.g. my dependent type is only EVEN numbers) whereas phantom types are effectively user-assigned tags.

    • @reo101
      @reo101 19 часов назад

      @@IsaacHarrisHolt In the dependent-type world you could have Open and Closed be the constructors of a single enum-ish type, restricting the "type of the type" of File to be just one of those two (and not an unrestricted type). If I understand it correctly, Gleam has only 2 "levels" of "stuff" - values and types, with no way to differentiate different types (noone can stop me from working with a `File(File(Int))` or something. One might say that the "type of types" (the type of stuff you can put in `File(here)` is "Type" and not something more specific, i.e. the type of types is an "open type", in contrast with a closed one (like a hypothetical enum).

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  19 часов назад

      Interesting. Seems I need to do more research into type theory 😅

  • @boscodomingo
    @boscodomingo 21 час назад

    These are basically ValueObjects...

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  20 часов назад

      I'm not totally sure what they are! Are they a language-specific thing?

    • @Antash_
      @Antash_ 17 часов назад +1

      @@IsaacHarrisHolt ValueObject is a name often used in Domain Driven Design, and it describes a concept of creating classes/concepts/types (depending on the paradigm) that wrap primitive types, so instead of using a float, one would create a (for example) Measure class that would wrap a raw float. It's basically a design pattern to fight "primitive obsession", so the goal is the same, but it's in runtime, and allows to have for example methods that would allow adding Miles to Kilometers but with proper conversion ;)

    • @antoniong4380
      @antoniong4380 14 часов назад

      I might not understand what you mean. It doesn't feel like you could achieve Compile-time checks with this strategy

    • @Antash_
      @Antash_ 10 часов назад

      @@antoniong4380 You do. Instead of using some phantom types, just just use normal types. The disadvantage is that it can be a lot of boilerplate, as you now have a full type, so in places where you need a float (for example in some library you're using) you cannot directly use your own type, but must do a conversion, or break encapsulation, something like that.

    • @boscodomingo
      @boscodomingo 9 часов назад

      With ValueObjects you could achieve compile-time checks as well since you can make methods that only take said classes, thus forbidding certain unwanted behaviours.
      For example, I could make a value object for Email, and validate that the string provided is not empty, has a certain length and matches my email regex. With that, I know that anytime I'm using an Email elsewhere, I don't have to check for such things. Thus any code that relies on them can work based off those assumptions at runtime, and leverage their properties/methods at compile time

  • @josealonso7321
    @josealonso7321 16 часов назад

    I think Kotlin also has this construct, but it is not called I like that.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 часов назад +1

      Phantom typing is just the generic name for the concept. Languages may have their own names for sure

  • @blinklight
    @blinklight 22 часа назад

    Please complete 💯 subscribe 😢