Skip to content

Conversation

@som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Nov 4, 2025

Fixes #24250

Make trivial end new useful by supplying the empty template in parser.

This makes

new C {
}

equivalent to

new C:
end new

@sjrd
Copy link
Member

sjrd commented Nov 4, 2025

Then

new C
end new

is just new C.

Why would we want that? It doesn't seem to follow the rule that end closes a block.

@som-snytt
Copy link
Contributor Author

som-snytt commented Nov 4, 2025

I thought the rule was that you can remove the marker without changing semantics, but that does not hold.

if b then e
end if

I'll try that out. Why doesn't github offer embedded REPL?

This obeys the Odersky blank line rule:

def f(b: Boolean) =
  if b then println("""hello,







    world""")
  end if
@main def test = f(true)

@som-snytt
Copy link
Contributor Author

som-snytt commented Nov 4, 2025

Why would we want that?

I remember the argument for

class C:
end C

was that I'm in the middle of editing, though I don't know if that's why it was finally allowed. But the same applies.

new C 
  // todo maybe override something later?
  // do we need an extra class? maybe we can avoid it.

  // override def toString = "Test C"
end new

The least github could do is test my late-night snippets in the browser.

@sjrd
Copy link
Member

sjrd commented Nov 4, 2025

The thing is that new C and new C {} mean completely different things. The latter, because of the block, creates an anonymous subclass of C. The former doesn't. You can't just "add overrides" to a new C. There's a first, very signifiant step where you have to introduce an anonymous class. That's when you add the : and possibly the end, not before.

@som-snytt
Copy link
Contributor Author

Yeah, maybe. I'm not wedded to any of this. Is colon enough of an indicator?

if I can write

  1
+ 2
end +

and I'm not sure they will yet, but my point is new gets equal protection. That is SIP 77. Or a similar argument for a named method, but I'm exaggerating for rhetorical effect.

@som-snytt
Copy link
Contributor Author

som-snytt commented Nov 4, 2025

it makes it hard to discern when a large indentation region ends

I'll update to read "or a very small region." Joke.

But this is clear:

If the statement defines an anonymous class, then s must be new.

end is intended only for when there was an anon class. I'll remove the even-more-trivial case of a named class instance.

That is at https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html#the-end-marker

@som-snytt som-snytt force-pushed the issue/24250-end-new branch from 43ea0cc to dc50485 Compare November 4, 2025 07:13
@som-snytt som-snytt marked this pull request as ready for review November 4, 2025 07:30
@Gedochao Gedochao requested a review from noti0na1 November 4, 2025 12:21
if MigrationVersion.Scala2to3.needsPatch then
patch(source, Span(in.offset), " ")

def possibleTemplateStart(isNew: Boolean = false): Unit =
Copy link
Contributor Author

Choose a reason for hiding this comment

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

isNew was never used


def possibleTemplateStart(isNew: Boolean = false): Unit =
def possibleTemplateStart(): Unit =
possiblyNewTemplateStart(): Unit
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ignores the boolean value

val body =
val bo = templateBodyOpt(emptyConstructor, parents, derived = Nil)
if !indented then reposition(bo) else bo
New(body)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

preserves existing behavior, but what is the span of the result?

else parent
case parents =>
// With brace syntax, the last token consumed by a parser is }, but with indent syntax,
// the last token consumed by a parser is OUTDENT, which causes mismatching spans, so don't reposition.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

comment inlined from the previous PR

@@ -0,0 +1,5 @@

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it possible to break the habit of inserting an initial blank line in test sources?

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.

end new for anonymous classes doesn't work under some conditions

3 participants