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

[0.4.0] Import as #775

Merged
merged 5 commits into from Dec 3, 2020
Merged

[0.4.0] Import as #775

merged 5 commits into from Dec 3, 2020

Conversation

ruby0x1
Copy link
Member

@ruby0x1 ruby0x1 commented Jul 11, 2020

I have still found the ability to rename an imported variable vital over time.
I plan to introduce this in 0.4.0 with the as keyword. This PR replaces #464

@ruby0x1 ruby0x1 added the 0.4.0 The 0.4.0 tag release label Jul 11, 2020
@ruby0x1 ruby0x1 changed the title Import as [0.4.0] Import as Jul 11, 2020
@ruby0x1 ruby0x1 added enhancement New feature or refinement to existing one language The Wren language syntax and semantics labels Jul 11, 2020
@mhermier
Copy link
Contributor

I have mixed fillings about that change. While I'm not particularly against "as" to be a new keyword, I don't find it particularly superior to:

import "..." for OtherName = Variable 

I also think there is a little ambiguity that is introduced by using as here. From what I remember variables are imported by copying and the as feels more like some kind of name alias/overload, inducing the variable is not copied, which lead to a little oddity in the language.

The as keyword can also be used to introduce variable names aliases at expression level, and I'm not sure it is really a desirable language feature. I can see a usage for refactoring, but I wonder about complexity in the compiler, the practicality...

@ruby0x1
Copy link
Member Author

ruby0x1 commented Jul 11, 2020

See the test cases, the values are copied because the variable is loaded onto the stack and then stored into a slot, all this does is make the slot name different if as is present. mechanically there is no change.

@mhermier
Copy link
Contributor

Ok so I'm not mistaken, the ambiguity is introduced by this change then. It is a minor issue thought, but I found it counter intuitive that this language choice can not convey the actual behavior. So the operator = usage here makes more sense to me as it enforce the idea of copying. But it may be only me.

Copy link
Contributor

@bjorn bjorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't find the copying (which is really just copying of the reference, most of the time) makes using as confusing, but then I'm already used to this keyword from QML's import syntax.

While this change is useful to avoid ambiguity to a certain degree, I really hope that once we'd have the as keyword we can eventually also make import "module" as M syntax work, making the module's variables available using M.Variable.

doc/site/modularity.markdown Outdated Show resolved Hide resolved
doc/site/modularity.markdown Outdated Show resolved Hide resolved
ruby0x1 and others added 2 commits July 11, 2020 13:20
Co-authored-by: Thorbjørn Lindeijer <bjorn@lindeijer.nl>
Co-authored-by: Thorbjørn Lindeijer <bjorn@lindeijer.nl>
@ruby0x1
Copy link
Member Author

ruby0x1 commented Jul 11, 2020

Note that as (and the wording) wasn't an arbitrary choice.
Dart, JS, Python and other languages use as OtherName because it's a simple english expression that is easy to understand.
Other languages use the same wording for casts, like rust and swift have let a = 0 as u8;.

@mhermier
Copy link
Contributor

It can be likewise decided to make import "module" also available as a regular expression that is returns the module variable (to allow assignment), and make for also a binary operator, that allow extracting variables from a map to local scope, so that it is compatible with module import. That way the as operator is also not required.

To add to the cons, and comparing to C++, the as operator also has the disadvantage to be a right definition like typedef, and readability seems to have improved since the adoption on using. As practice seems to had proven, we need the defined first for fast reading, not the possibly ugly/dirty definition.

@ruby0x1
Copy link
Member Author

ruby0x1 commented Jul 11, 2020

I agree import "something" Name OtherName would be confusing. This is not the case, though, and doesn't relate to typedef.

@mhermier
Copy link
Contributor

To clarify my words, I don't care about the choice of the as keyword, could be anything, and I know as is used in a lot of other language. I point that assignment syntax is also an interesting possibility with quite some pros and cons, that I prefer over introducing a new keyword.

@DethRaid
Copy link

import "..." for OtherName = Variable 

is less clear to me than

import "..." for Variable as OtherName

The first statement reads as "import this module so we can use OtherName to refer to Variable", while the second reads as "import this module to we can use Variable and call it OtherName". It's just more clear, and more conventional, and I don't see a real reason to use the assignment operator here

@mhermier
Copy link
Contributor

It is the same reason to why using is superior to typedef in C++.
When I read:

import "..." for OtherName = Variable

I read "Import this module, that give me this symbol followed by =" and I stop reading unless I really need to know what is the underlying type.
With the other syntax you have to read allllll the text before reaching the usable symbol name that doesn't has a fixed position.

If you import many symbols, you can do something like:

import "..." for
  OtherName = Variable,
  FooOtherName = FooVariable,
  ...
  BarOtherName = BarVariable

And the defined symbols is always visible at first sight, and does not require reading the whole expression, hacky text alignment or editor search.

@ruby0x1 ruby0x1 merged commit 3e0f71b into main Dec 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.4.0 The 0.4.0 tag release enhancement New feature or refinement to existing one language The Wren language syntax and semantics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants