Skip to content

Function for String -> TagDeclarator #9467

@adamchalmers

Description

@adamchalmers

Currently tag declarator are always literals, e.g. $a or $myTag. This is a pretty artificial limitation, and in our discord, some users have asked us to lift it.

is it possible to define tag names dynamically or based on a stored string value? Just to illustrate what I mean using a basic example from the KCL tutorial docs, drawing a polygon dynamically using reduce but assigning a unique tag for each side so that the geometry can be referenced later.

I think a function which takes a string and outputs a tag declarator would be helpful. Especially paired with an int -> string function. This would let you dynamically build tags for each side of a polygon like so:

  polygonSketch = startSketchOn(plane)
    |> startProfile(at = [center[0] + radius, center[1]])

  // Use reduce to iteratively draw the sides of the polygon
  profile = reduce(
    [1_..sides],
    initial = polygonSketch,
    f = fn(@i, accum) {
      newTag = stringToTag("side" + numToString(i))
      return angledLine(accum, angle = interiorAngle * i, length = sideLength, tag = newTag)
    },
  )

  return profile
    |> close()```

Metadata

Metadata

Assignees

No one assigned

    Labels

    kclLanguage and compiler features

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions