less.js icon indicating copy to clipboard operation
less.js copied to clipboard

Mixin return values

Open matthew-dean opened this issue 14 years ago • 59 comments

Can I set a return value as a color? As in:

.myselector {
   border: 1px solid .custommixin(@colorValue);
}

Just wondering in response to another user's issue regarding color functions: https://github.com/cloudhead/less.js/pull/488

matthew-dean avatar Dec 28 '11 20:12 matthew-dean

Oh I see, no. But you can do..

.myselector () {
  @return: 2px;
}

.class {
  .myselector;
  border: @return solid red;
}

pretty ugly though.

cloudhead avatar Dec 28 '11 21:12 cloudhead

Ah, of course. It might be nice to have a returned value for custom color functions (and other custom value functions) when mixin guards are in place. @return could be a reserved variable for LESS. As you say, it's a bit ugly to just have a mixin do nothing but variable assignment when it's called.

Would be nicer to see the first example I posted supported at some point. The rationale being that you have spin, lighten, darken, etc functioning as built-in mixins, so the logic for return values is already there in LESS, it would just be opening up the door for custom mixins.

matthew-dean avatar Dec 28 '11 22:12 matthew-dean

Not I think that programming said functionality is easy mind you, just that it would empower people to extend the logic a bit.

matthew-dean avatar Dec 28 '11 22:12 matthew-dean

yes, true, but I also like how it currently is, where the 'functions' are all javascript..

cloudhead avatar Dec 29 '11 00:12 cloudhead

Couldn't variables accept arguments the same way mixins do? My usecase would be for instance:

@boxGlow(@spread: 5px, @color: #f00): inset 0 0 @spread @color;
.myselector {
   box-shadow: 1px 1px 1px #000, @boxGlow(2px, #00f);
}

It's also fairly analogous with JavaScript where functions live in regular variables.

jacobrask avatar Jan 30 '12 16:01 jacobrask

@cloudhead: If I spent some time implementing that feature (I haven't looked at the Less source yet, so I can't promise anything), would you be willing to add it if the code is good enough?

jacobrask avatar Jan 31 '12 09:01 jacobrask

Adding a return capability to mixins would preserve the mapping of mixins to JavaScript functions, while still facilitating @jacobrask and @matthewdl's use case. I've documented this a bit more on issue 637.

tylertate avatar Mar 21 '12 13:03 tylertate

duplicate of #538

lukeapage avatar Feb 25 '13 12:02 lukeapage

woops, this is #538

lukeapage avatar Feb 25 '13 12:02 lukeapage

This would be a very, very useful solution.

I'm trying to implement dynamically resizing images (like [UIImage resizable...] on iOS) and I would like to use a function which will return an URL string, not property/value pair.

Curently I'm using @cloudhead's solution.

akashivskyy avatar Mar 11 '13 18:03 akashivskyy

Just to tie this in a nice bow, this should be included when the options.json support is added. Issue: #850

matthew-dean avatar Dec 21 '13 04:12 matthew-dean

Mixins are mixins, we must not conflate their usage with functions. If having the ability to define custom functions in the style sheet is something we want though, that can be a separate discussion.

cloudhead avatar Dec 30 '13 13:12 cloudhead

Mixins are mixins, we must not conflate their usage with functions.

The problem is that there's no way to forbid using mixins as functions. It is possible to use one as the other and they (and me :) get to use that ability. So de facto it's only a matter of syntactic sugar and verbosity:

.sum(@a, @b) {
    @-: (@a + @b);
}

usage {
    width: @-;.sum(20px, 33px);
}

Using same "function" twice in the "same scope":

// short (since 1.6.2):
usage {
    & {width:  @-;.sum(55px, 77px)}
    & {height: @-;.sum(11px, 99px)}
}

// oldschool (since 1.4.x):
usage {
    .-() {width:  @-;.sum(20px, 33px)}
    .-() {height: @-;.sum(40px, 42px)}
    .-();
}

Is there a reason to invent some new "Less function declaration/definition" syntax (different from a mixin definition syntax) only to not allow to use width: .sum(20px, 33px); directly?

seven-phases-max avatar Dec 30 '13 13:12 seven-phases-max

What the what did you just do there. That's some next-level Less-hackery going on there, lol.

matthew-dean avatar Dec 31 '13 16:12 matthew-dean

@lukeapage I am sorry I can't understand the state of this issue. You've put a reference to this issue on several others but this is set as closed. Is there a plan to allow to use mixins as lambdas or are you suggesting us to take advantage of the global variable setting workaround? In other issue I've understand you were planning to remove that on some future.

axelcostaspena avatar Sep 24 '14 12:09 axelcostaspena

@seven-phases-max your approach is awesome but there is no way to pass to the sum mixin the result of calling sum two times, am I wrong? Talking on a somewhat more universal language, this: foo(bar(1, 2), bar(3, 4))

axelcostaspena avatar Sep 24 '14 12:09 axelcostaspena

@axelcostaspena Well, everything is possible using mixins, it's just a matter of verbosity (obviously 5 or 6 lines of code do not look like a practical replacement for a oneliner).

seven-phases-max avatar Sep 24 '14 16:09 seven-phases-max

less-plugin-functions

Wow. Nice! Really, the only thing I'd improve syntax-wise if this is moved to core, is turning the outer block into @functions and probably dropping the leading period on the mixin declarations for consistency with how these functions are used at call sites (namely; also without the leading period, as real Less functions).

E.g.

@functions {
    foo(@x) {
        return: @x * 2;
    }
}

This still leaves the issue of having functions/mixins assignable to variables and passable as parameters, so that they can be used as lambda delegates. But this right here is already a very nice way to segregate 'computation' mixins from 'render' mixins.

rjgotten avatar May 11 '15 13:05 rjgotten

Wow. Nice! Really, the only thing I'd improve syntax-wise if this is moved to core, is turning the outer block into @functions and probably dropping the leading period on the mixin declarations for consistency with how these functions are used at call sites

Yeah, we should respect this from @cloudhead :

Mixins are mixins, we must not conflate their usage with functions.

Defining a function as ".foo" by putting it in ".function" and then calling it with "foo" doesn't really make sense as a syntax.

@rjgotten The one suggestion I would make to your syntax is to not make functions plural, even if there is more than one function in the function block. Seems more Less/CSS consistent.

@function {
    foo(@x) {
        return: @x * 2;
    }
}

Also, as sugar:

@function foo(@x) {
     return: @x * 2;
}

matthew-dean avatar May 11 '15 16:05 matthew-dean