JustTesting,

This would be trivial in python with something like


<span style="color:#323232;">from typing import overload
</span><span style="color:#323232;">from enum import Enum
</span><span style="color:#323232;">
</span><span style="color:#323232;">@overload
</span><span style="color:#323232;">def hash(password: str, algorithm: BCryptAlgorithm, options: BCryptOptions):
</span><span style="color:#323232;">    ...
</span><span style="color:#323232;">
</span><span style="color:#323232;">@overload
</span><span style="color:#323232;">def hash(password: str, algorithm: Argon2Algorithm, options: Argon2Options):
</span><span style="color:#323232;">    ...
</span><span style="color:#323232;">
</span><span style="color:#323232;">def hash(password: str, algorithm, options):
</span><span style="color:#323232;">    [...implementation...]
</span>

Of course it’s python, so at runtime it wouldn’t matter, but the static type checker would complain if you called hash with BCryptAlgorithm and Argon2Options. You could also have it return different types based on the arguments and then in call sites it’d know which type will be returned based on the type of the arguments. And only the last function has am implementation, the @overload ones are just type signatures.

It’s documented here.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • programming_languages@programming.dev
  • ethstaker
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • Durango
  • InstantRegret
  • Youngstown
  • ngwrru68w68
  • slotface
  • rosin
  • cisconetworking
  • GTA5RPClips
  • kavyap
  • mdbf
  • megavids
  • tester
  • osvaldo12
  • tacticalgear
  • khanakhh
  • everett
  • modclub
  • anitta
  • Leos
  • cubers
  • normalnudes
  • provamag3
  • JUstTest
  • lostlight
  • All magazines