Skip to content

Typesafety: assert function to fail at compile time #2747

Closed
@slorber

Description

@slorber

Hi,

What I'd like to do is something like:

.background-clip(@value) when (@value = border-box),
                              (@value = padding-box),
                              (@value = content-box),
                              (@value = inherit)
{
    -webkit-background-clip: @value;
       -moz-background-clip: @value;
            background-clip: @value;
}

When the background-clip is called with a wrong value, instead of ignoring that value, it could be nice to make the compilation fail.

Is it currently possible? or could it be in the future?

It would be really great to have a syntax to tell that the guard is a "compilation guard" and not simply "ignore-guard"

One simple solution i can think of is using a function that may fail. Something like:

.background-clip(@value) when (oneOf(@value,"border-box","padding-box","content-box","inherit")
{
            background-clip: @value;
}

The idea is that the function acts like an "assertion" that may make the compilation fail.

As far as I see there are already such functions that can make the compilation fail.

It would be nice to have more compilation safety with a oneOf function.

There are probably other useful options, like ensuring a list contains only know values.

For example when dealing with interaction modes, which can generally be mouse and/or touch. We have to handle special cases because the :hover is not possible on devices with only touch support.

.myMixin(@interactionMode) when (listOf(@interactionMode,"touch","mouse")
{

   div.someButton {
        & when (contains(@interactionMode,"touch")) {
            // ...
        }
        & when (contains(@interactionMode,"mouse"))  {
           &:hover {
                       // ...
           }
        }
        & when (contains(@interactionMode,"mouse","touch")) {
            // ...
        }
   }
}

The contains() function would be useful there :)

Maybe we could also pass as mixin arguments complex json objects and assert in functions that they have a certain "shape"

I don't know which functions would be useful exactly but I think it would be a nice progress for less to have more "typesafety".

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions