Filed under: Best Practices
Comments: 1
NexJ Scheme is NexJ’s scripting language, which is based on and extends Scheme R(6)RS.
The at sign (“@”) is a special macro in NexJ Scheme, and provides a special way of accessing a value through associations relative to this pointer and much more.
This table summarizes the most common use cases of the @ macro within NexJ Scheme, provided with examples and their explanations. Pay close attention to the “Note” column for macro usage recommendations and performance concerns.
Use | Example | Effect | Note |
Retrieve attribute value | (@ lastName)
|
(this'lastName)
|
Recommended especially for long association paths, as it simplifies the nested brackets.
Note that |
Set attribute value | (@ lastName : "New Name") |
(this'lastName "New Name") |
Avoid using this special syntax as it is difficult to read. |
Invoke class event | (@ createCopy : entity) |
(this'createCopy entity) |
Avoid using this special syntax as it is difficult to read. |
Get size of instance associated collection | In Act class:
|
Returns total count of all acts associated to all assign to entities on the current act. | These also work:
|
Use in collection-find-item | In Entity class:
|
Finds the first address for the entity in city Toronto. | The @ macro in (@ addrs) refers to the current entity instance.
The @ macro in |
Dynamic type checking | In Entity class:
In Address class:
|
Returns a collection of address first lines where the address’s city is “Toronto”.
When using |
Supports conjunctions. For example: (@ entity (and (instance? (@) Person) <another-condition1> ...) lastName)
Note that the filtering
|
Iterate over association paths with collections | In Entity class:
|
Iterates over all the cities in the addresses collection. | Can have arbitrary combinations of collections and instances on the association path, and supports filters. |
Optimize database filtering on collection associations | (Person'read |
Reads all Persons where the address city is Toronto.
SQL:
|
Recommended since the database will return only one person record per matching addresses as opposed to returning as many copies of a person record as there are matching addresses when using the equals operator (Person'read ... '(= (@ addrs city) "Toronto") ...)
SQL:
|
Interchange this with @ |
In Scheme Console:
|
Setting this allows @ to work accordingly. |
Useful when debugging in Scheme Console.
Also used in macros like |
Use in aggregate functions | (Person'read'(firstName lastName (: _countryCnt (count (@ addrs country))) '() '() '() '() '()) |
Reads Persons and counts the number of countries tied to addresses. | Attribute annotations must be aggregate expressions including: unique, count, sum, average, minimum, maximum.
Do not use on large collections, for system scalability reasons. |
Advanced: What is the “@@” Syntax?
The “@@” syntax is very different from “@” and can be useful in the following two scenarios:
- For downcasting attributes
- To indicate a reverse association with the WHERE clause. Be careful as this syntax has two completely unrelated meanings
Attribute downcast
Use | Example | Effect | Note |
(Entity'read '(lastName (@@ Person homePhone)) () () () () ()) |
Reads from Entity class and retrieves the homePhone attribute for the Person subclass of Entity only. Attribute homePhone is defined on Person class only. | Useful for performing polymorphic from a base class and loading subclass-specific attributes. | |
Where clause reverse association | (Address'read '() `(= (@@ User person addrs) ,(user)) '() '() '() '()) |
Reads Addresses from the person associated to the current user. Note that this is equivalent to (((user)’person)’addrs). SQL: select A.id, A.locking from NJAddress A inner join NJEntity B on A.entityId = B.id inner join NJUserPerson C on B.id = C.id where A.deletedFlag = 0 and A.viewPrincipalId in (…) and C.userId = ‘00000000000010008000BEEF0000000C’ |
Useful when querying from the end of an association back to the class you are reading, for example, querying on Address of User based on User instance, as opposed to forward association query on User based on User’s Address instance. NexJ UI clients may use this syntax. The syntax form is (@@ part3 part2 part1) where part1 (for example, addrs ) represents an attribute with a type of the class (for example, Address ) being read. |
Vaibhav khopade
May 20, 2016 at 8:44 am
Thank you so much for the wonderful article.
This is very helpful for better understanding of scheme scripting.