Skip to content

docs: add an example to -Yindent-colons section #10534

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

Merged
merged 1 commit into from
Dec 4, 2020
Merged

docs: add an example to -Yindent-colons section #10534

merged 1 commit into from
Dec 4, 2020

Conversation

SethTisue
Copy link
Member

@SethTisue SethTisue commented Nov 28, 2020

At least, I think this is the recommendation....... is it?

I admit this came up when I was intentionally expunging all braces from a repo of mine as an exercise, in an attempt to form an opinion on the future of -Yindent-colons

never using braces could be a pointless exercise, or it could be good pedagogically actually. it's simpler for learning if the answer to "do I ever actually need braces?" is "no"

in any case, I like knowing I have the option with locally

@soronpo
Copy link
Contributor

soronpo commented Nov 28, 2020

it's simpler for learning if the answer to "do I ever actually need braces?" is "no"

As discussed on gitter, for the record, here is a counter example.

  extension (src : => Unit) def ==> (dst : => Unit) : Unit = ()
  locally {
    println("hi")    
  } ==>
  locally {
    println("there")
  }

There is no option to do so with indentation only.

@LPTK
Copy link
Contributor

LPTK commented Nov 28, 2020

@soronpo you can do this:

locally:
  println("hi")    
.==> :
  println("there")

@LPTK
Copy link
Contributor

LPTK commented Nov 28, 2020

@SethTisue I don't think the example you gave is a good one, as you can just write:

val dinner =
  val s1 = "fish"
  val s2 = "chips"
  List(s1, s2)

instead of:

val dinner = locally:
  val s1 = "fish"
  val s2 = "chips"
  List(s1, s2)

@SethTisue SethTisue marked this pull request as draft November 28, 2020 15:02
@SethTisue
Copy link
Member Author

@LPTK well, dang. I thought I'd tried that? okay, I'll take a second look at the places in my codebase I thought I needed locally

@SethTisue SethTisue marked this pull request as ready for review November 28, 2020 17:03
@SethTisue
Copy link
Member Author

better example now

@LPTK
Copy link
Contributor

LPTK commented Nov 28, 2020

@SethTisue but you can just write this:

credentials ++ :
  val file = Path.userHome / ".credentials"
  if file.exists
  then Seq(Credentials(file))
  else Seq()

😄

@SethTisue SethTisue changed the title docs: reference locally in -Yindent-colons section docs: add an example to -Yindent-colons section Nov 28, 2020
@SethTisue
Copy link
Member Author

okay, so the remaining use case is this kind of thing:

locally:
  println("hi")    
.==> :
  println("there")

which seems too niche to even cover, so I've revised this not to mention or use locally at all

my real mission here was to get my questions about -Yindent-colons answered, so, mission accomplished :-)

@LPTK
Copy link
Contributor

LPTK commented Nov 28, 2020

Yes, I think the only use for locally is to open sub-scopes within the enclosing block. It's sometimes useful to avoid name clashes, and it works better than the old way with plain braces (which should not have anything right before them lest they're interpreted as block arguments).

@SethTisue
Copy link
Member Author

@LPTK I think the following wording in the doc:

colons at end of lines are accepted at all points where an opening brace enclosing a function argument is legal

misled me; I read “function argument” as referring specifically to lambdas & by-names.

I have amended this PR to omit the word “function” there

@LPTK
Copy link
Contributor

LPTK commented Nov 29, 2020

@SethTisue Ah yes, it's an English language ambiguity. I think the meaning originally intended by "enclosing a function argument" was "enclosing the argument to a function", not "enclosing a function passed as an argument". It could be misleading indeed!

@SethTisue
Copy link
Member Author

Discussion has settled down and this is ready for merge, I believe.

@smarter smarter merged commit dd462eb into scala:master Dec 4, 2020
@SethTisue SethTisue deleted the locally branch December 4, 2020 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants