Nim how to define constructors?
Asked Answered
M

1

10

Is there a way in Nim to define constructors for an object? For example I have

type Deck* = ref object
    cards* : array[52, Card]

can I create an empty constructor that creates automatically all the cards?

Mutualize answered 17/10, 2017 at 21:57 Comment(0)
J
10
type
  Card = int
  Deck* = ref object
    cards* : array[52, Card]

proc newDeck: Deck =
  new result
  for i, c in result.cards.mpairs:
    c = i
Jacklin answered 18/10, 2017 at 7:36 Comment(1)
The new operator should now be avoided.Mina

© 2022 - 2024 — McMap. All rights reserved.