Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concepts already implemented in Nim at time of publishing #17

Closed
ZoomRmc opened this issue Jun 6, 2023 · 1 comment
Closed

Concepts already implemented in Nim at time of publishing #17

ZoomRmc opened this issue Jun 6, 2023 · 1 comment

Comments

@ZoomRmc
Copy link

ZoomRmc commented Jun 6, 2023

The only existing language today which can accomplish something like this is C++, with its `requires requires` clause:

Not sure when exactly, but they had been implemented for a few years by 2022 when the article was published.
The current state: https://nim-lang.org/docs/manual_experimental.html#concepts
The redesign (testing implementation exists): nim-lang/RFCs#168

type
  Strong = concept t
    t.strength is int

  Damaging = concept a, b
    calcDamage(a, b) is int

func calcDamage(attacker, target: Strong): int =
  attacker.strength - target.strength

func battle(attacker, defender: Damaging): int =
  calcDamage(attacker, defender)

type Ship = object
  strength: int

let rocinante = Ship(strength: 10)
let  bessie = Ship(strength: 9000)

echo bessie.battle(rocinante)
@Verdagon
Copy link
Owner

Verdagon commented Jun 9, 2023

Added a mention for Nim's concepts, thanks for the heads up!

@Verdagon Verdagon closed this as completed Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants