question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to lookup for variable that doesn't exist?

See original GitHub issue

Hello! Is it possible to lookup for variables that doesn’t exist? I have a list of generated variables for every icon glyph. Some icons consist from two 2 glyphs (need for ::before + ::after for 2 colors icon). I want to pass base icon name to mixin and get correct output, but i get an error of looking @@icon2, and it doesn’t matter if i use when or if. Also tried to use mixin guards, but no changes. Some code:

// generated list of icons
@icon__glyph_about_20__1: '\E00C';
@icon__glyph_about_32__1: '\E00D';
@icon__glyph_about_32__2: '\E00E';

// common mixin for icons
.nbl-icon__use(@iconName) {
  @icon1: ~'icon__glyph_@{iconName}__1'; // always exists
  @icon2: ~'icon__glyph_@{iconName}__2'; // sometimes doesn't exist
  
  &::before {
    content: @@icon1;
  }
  
  & when not (@@icon2 = undefined) {
    &::after {
      content: @@icon2;
    }
  }
}

.test {
  .nbl-icon__use(about_20);
}

Example of error: Message: variable @icon__glyph_about_20__2 is undefined in file ... ezgif-3-2bd786268161

P.S. Unfortunately i can’t generate list of vars with null’s. P.S.S. sry for my eng, hope u understand me

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
matthew-deancommented, Oct 26, 2019

@theexplay The short answer is that no, there’s no current way to do this in exactly the way you mention. One workaround, though, would be to specify all the var names in a file loaded before the optional definitions, and set those values to something you would check later. In other words:

// default values
@icon__glyph_about_20__1: false;
@icon__glyph_about_20__2: false;
@icon__glyph_about_32__1: false;
@icon__glyph_about_32__2: false;

// generated values
@icon__glyph_about_20__1: '\E00C';
@icon__glyph_about_32__1: '\E00D';
@icon__glyph_about_32__2: '\E00E';

// common mixin for icons
.nbl-icon__use(@iconName) {
  @icon1: ~'icon__glyph_@{iconName}__1'; // always exists
  @icon2: ~'icon__glyph_@{iconName}__2'; // sometimes doesn't exist
  
  &::before {
    content: @@icon1;
  }
  
  & when not (@@icon2 = false) {
    &::after {
      content: @@icon2;
    }
  }
}

.test {
  .nbl-icon__use(about_20);
}
0reactions
matthew-deancommented, Oct 26, 2019

@seven-phases-max

Oh, shoot. That’s a good catch. This is one of those cases where doing a lot of Less refactoring has tricked my brain towards what Less will do vs. what Less currently does do.

It should be:

  & when not (@@icon2 = false) {
    &::after {
      content: @@icon2;
    }
  }

You’re right that Less currently expects something close to === equality, although not consistently.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript check if variable exists (is defined/initialized)
In JavaScript, a variable can be defined, but hold the value undefined , so the most common answer is not technically correct, and...
Read more >
3 Ways to Check if a Variable is Defined in JavaScript
How to check if a variable is defined in JavaScript using typeof operator, try/catch blocks, or window.hasOwnProperty().
Read more >
Update Lookup Field value with newly inserted object Id
variable does not exist - Update Lookup Field value with newly inserted object Id. Hi, I'm brand new to apex.
Read more >
Testing if a Variable Is Defined - Python Cookbook [Book]
It is considered unusual in Python not to know whether a variable has already been defined.
Read more >
Check if variable exists in workspace to plot ... - MathWorks
I am having some trouble with checking if a variable exists in the workspace. If the variable exists (which is a structure with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found