Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the behavior of prefixed variables more logical #809

Open
kizu opened this issue Aug 24, 2012 · 5 comments
Open

Make the behavior of prefixed variables more logical #809

kizu opened this issue Aug 24, 2012 · 5 comments
Milestone

Comments

@kizu
Copy link
Member

kizu commented Aug 24, 2012

Right now there is two ways to write and use variables — without the $ prefix and with it.

I like the way it is now, but there can be a lot of ways where the undeclared variables without value have the value like symbols makes things hard and ugly to do.

Those are:

  1. Checking if the var exists: you must use is defined, instead of just the conversion to false (see Undefined variables return true in conditions #740).
  2. Using variables in operations — you can just add the variable to string and wait it to be empty if it don't exist.
  3. The above one makes it harder to write properties that should not be there if their content is empty, you need to check if the variable is defined etc, and that makes code ugly.

I propose this one thing: make the variables with $ prefix don't work like symbols. Yes, it would make the two syntaxes of variables not equal, but you could get a lot of nice things with the prefixed var if you'd want. And it is something that won't have any problems on change — adding better behavior would only remove all those $foobar went to CSS when things gone wrong. All the is defined checks and all those things would work as usual.

So, look at this example:

.test
  color: foo if foo is defined

  width: 10px + (bar is defined ? bar : 0)

  if foo is defined and bar is defined
    height: 10px

it looks awful!

But what if we'd add the desired behavior and with just adding prefix we'd get this nice code:

.test
  color: $foo

  width: 10px + $bar

  if $foo and $bar
    height: 10px

It's so much better!

The only thing we need to do is to convert the undeclared variables with $ prefix to false in boolean context, to empty string in string operations, to emptiness in cases like color: $foo, to zero in math operations etc.

@visionmedia what do you think? Is there any chance I could convince you to this? :)

@tj
Copy link
Contributor

tj commented Aug 24, 2012

yeah I'd like if they defaulted to null, I can think of that getting in the way of anything ATM. We'd like to eventually migrate away from prefix-less vars anyway, it's nice in examples but it gets annoying pretty quick

@kizu
Copy link
Member Author

kizu commented Aug 24, 2012

Yep, we tried to use variables perfix-less initially, but then stumbled over a lot of issues with them.

Anyway, it's a nice thing to migrate in the major version of Stylus — so if there'd be 1.0 version I'm all in for the prefix-only variables. Actually, there's a lot of things that can be done in Stylus and that are not back compatible: this, better syntax for interpolations with #{}, so we could use them everywhere, and I remember some other things in issues like that.

@rauchg
Copy link
Contributor

rauchg commented Aug 24, 2012

+1 for enforcing a $ prefix.

@scien
Copy link
Contributor

scien commented Feb 5, 2014

+1

any progress on this?

It's also weird that "an undefined identifier is still a truthy value". What was the thought behind that decision?

for an undefined variable $foo

// does not work as i would expect. $foo is not set
$foo = 'bar' unless $foo

// does not work as i would expect. $foo is not set
$foo = 'bar' if $foo is undefined

// works as i would expect it to. $foo is set to 'bar'
$foo = 'bar' unless $foo is defined

// would be nice
$foo ?= 'bar'

if variable equals an empty string

$foo = ''

// works as expected, foo is not updated
$foo = 'http://domain.com/default-image.jpg' unless $foo is defined

// works as expected, foo is updated
$foo = 'http://domain.com/default-image.jpg' unless $foo

@dmitrykiselyov
Copy link

$prefix-only 👍 codemirror stylus mode will be happier 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants