E.g., the error message in the following example puzzles me. Here I try to define the ''*'' operator for replicating an array, i.e. [" 12 "] * 3 should give [" 12 "," 12 "," 12 "]
func `*`[ArrSize: static[Natural], T:typedesc, Cnt: static[Natural]](
elems: array[ArrSize, T], times: Cnt): array[Cnt * ArrSize, T] =
# ------------ Error: type expected ^^^
for idx in 0 .. elems:
for arrIdx, item in pairs(elems):
result[idx * arrIdx] = item
const test = [" 12 "] * 12
Many thanks for any pointers or hints, Helmut
the issue is that static[Natural] already is a value not the type of the value:
func `*`[ArrSize: static[Natural], T:typedesc](
elems: array[ArrSize, T], times: static[Cnt]): array[Cnt * ArrSize, T] =
Though there's another issue. Writing typedesc is not the same as just writing T. So finally it works this way:
func `*`[ArrSize: static[Natural], T](
elems: array[ArrSize, T], times: static[Natural]): array[times * ArrSize, T] =
discard
also please use synatx highlighting in code blocks by writing nim after the tripple accents.
func `*`[ArrSize: static[Natural], T](elems: array[ArrSize, T], times: static[Natural]): array[times *
ArrSize, T] =
for idx in 0 ..< times:
for arrIdx, item in pairs(elems):
result[idx * ArrSize + arrIdx] = item
const test = [" 12 "] * 12
echo test
- typedesc ist ein datentyp für typen als werte. Jeder Typ T hat einen Typdescriptor vom Typ typedesc[T].
- typedesc ohne parameter zu verwenden fügt implizit einen verborgenen generischen parameter hinzu
- static[T] ist ein Typ für werte die zur compilezeit bekannt sind.
- static[T] zu verwenden füght auch einen verborgenen generischen parameter hinzu, ein Typ, der genau diesen wert repräsentiert.
Persönlich halte ich diese verborgenen generischen parameter für sehr irreführend. Ich könnte mich aber nicht dazu durchsetzten diesen Teil der Sprache expliziter zu gestalten.