Use of define

0 votes
asked Feb 4, 2022 in Question / help by James Booth
I'm creating a library and currently using !define like below so that I can control the style and such apart from the layout. The docs state that !define is deprecated and not to use it. How would this be ported to function or procedure. I've tried a few ways and can't get it to work.

@startuml

!define Thing(label) entity label << (T, white) >>
!define SubThing(label, type) <i>label</i> : type

Thing("foo"){
    SubThing("bar", "string")
    SubThing("bar2", "string")
    SubThing("bar3", "object")
}

@enduml

1 Answer

0 votes
answered Feb 4, 2022 by The-Lu (89,080 points)
selected Feb 4, 2022 by plantuml
 
Best answer

Hello J.,

Here is a simple proposal:

@startuml

!procedure  Thing(label)
entity label << (T, white) >>
!endprocedure

!procedure SubThing(label, type)
<i>label</i> : type
!endprocedure

Thing("foo"){
    SubThing("bar", "string")
    SubThing("bar2", "string")
    SubThing("bar3", "object")
}

@enduml

See also doc. here:

Regards,
Th.

commented Feb 4, 2022 by JEBoothjr (120 points)

Perfect. Thanks. I noticed in the docs that sometimes variables are prepended with $ and sometimes not. They say that it should start with a $ but it seems more of a developer choice. I think when I was trying to make it work, I was using a mix of them. Does the prepended $ really matter? It seems more of a best practice and not required?

commented Feb 4, 2022 by plantuml (298,440 points)

Does the prepended $ really matter? It seems more of a best practice and not required?

It's more a best practice, and not required.

I'm sure it will bring some confusion but in your case you can also have:

@startuml

!function Thing($label)
!return "entity " + $label + " << (T, white) >>"
!endfunction

!function SubThing($label, $type)
!return "<i>" + $label + "</i> : " + $type
!endfunction

Thing("foo"){
    SubThing("bar", "string")
    SubThing("bar2", "string")
    SubThing("bar3", "object")
}

@enduml

The difference between function and procedure may be difficult to get :-)

commented Feb 4, 2022 by The-Lu (89,080 points)

Yes, it is a best practice...

To avoid... this kind of error:

@startuml
!procedure  Thing(label)
class "label of label is label" << (T, white) >>
!endprocedure

!procedure  Thing2($label)
class "label of label is $label" << (T, white) >>
!endprocedure

Thing("foo")
Thing2("foo")
@enduml

wink

Regards,
Th.

commented Feb 4, 2022 by The-Lu (89,080 points)

To continue PlantUML's answer...

In your case you can also have, function on one-line cheeky:

@startuml
!function Thing($label) !return "entity " + $label + " << (T, white) >>"

!function SubThing($label, $type) !return "<i>" + $label + "</i> : " + $type

Thing("foo"){
    SubThing("bar", "string")
    SubThing("bar2", "string")
    SubThing("bar3", "object")
}
@enduml

Enjoy,
Regards.

commented Feb 4, 2022 by JEBoothjr (120 points)

Yeah. Make sense. Having the $ within the library itself makes sense as a good practice. What I wasn't wanting is requiring it from the end user perspective, like below, where its prepended to the instantiation.

$Thing("foo"){
    $SubThing("bar", "string")
    $SubThing("bar2", "string")
    $SubThing("bar3", "object")
}
Thanks all for the help.
...