Less doesn't have any true functions that can return values. Also a mixin can output content only within selector blocks (either a selector of its own or the parent selector). In your case there is only a string print but it is not assigned to any property (or) present within any selector block.
For your case, you can write a very simple mixin like below and use it:
.nth-child-mixin(@offset, @props){
&:nth-child(n + @{offset}){
@props();
}
}
td{
.nth-child-mixin(3, {
color: red;
});
}
td{
.nth-child-mixin(5, {
color: blue;
});
}
The above nth-child-mixin takes the offset and the properties that should be assigned as inputs. The properties is a detached ruleset which is then called from within the nth-child selector block. The compiled CSS output will look as follows:
td:nth-child(n + 3) {
color: red;
}
td:nth-child(n + 5) {
color: blue;
}
A more comprehensive mixin would be the below as you can pass it the multiplier for n also.
.nth-child-mixin(@multiplier, @offset, @props){
&:nth-child(@{multiplier}n + @{offset}){
@props();
}
}
td{
.nth-child-mixin(2, 5, {
color: blue;
});
}
The compiled CSS in this case would be the following:
td:nth-child(2n + 5) {
color: blue;
}
I feel using detached rulesets make it more clearer but if you want the mixin to just output a selector, you could make the mixin definition as follows. The only drawback is you may occasionally run into scoping issues when you need more than one such block within same nesting.
.nth-child-mixin(@multiplier, @offset){
@sel: ~":nth-child(@{multiplier}n + @{offset})";
}
td{
.nth-child-mixin(1, 3);
&@{sel}{
color: red;
}
}
td{
.nth-child-mixin(2, 5);
&@{sel}{
color: blue;
}
}
For example if you want to write two nth-child selectors like below within same td nesting then it wouldn't produce the expected output due to scope, lazy-loading etc.
td{
.nth-child-mixin(1, 3);
&@{sel}{
color: red;
}
.nth-child-mixin(3, 5);
&@{sel}{
color: red;
}
}
and that would push you into using anonymous selector boxes like in below sample to solve the scoping issues (these hacks are just terrible).
td{
& {
.nth-child-mixin(1, 3);
&@{sel}{
color: red;
}
}
&{
.nth-child-mixin(3, 5);
&@{sel}{
color: red;
}
}
}