Skip to content

Commit c103795

Browse files
authored
work around scala bug 4762 (#205)
No longer duplicate the "id" field in generated NodeRef subclasses. Saves 8 bytes / node. Cf scala/bug#4762
1 parent c514e90 commit c103795

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

codegen/src/main/scala/overflowdb/codegen/CodeGen.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,14 +955,24 @@ class CodeGen(schema: Schema) {
955955

956956
val propertyDefaultValues = propertyDefaultValueImpl(s"$className.PropertyDefaults", properties)
957957

958-
s"""class $className(graph: Graph, id: Long) extends NodeRef[$classNameDb](graph, id)
958+
s"""class $className(graph_4762: Graph, id_4762: Long /*cf https://github.com/scala/bug/issues/4762 */) extends NodeRef[$classNameDb](graph_4762, id_4762)
959959
| with ${className}Base
960960
| with StoredNode
961961
| $mixinsForExtendedNodes {
962962
| $propertyDelegators
963963
| $propertyDefaultValues
964964
| $delegatingContainedNodeAccessors
965965
| $neighborAccessorDelegators
966+
| // In view of https://github.com/scala/bug/issues/4762 it is advisable to use different variable names in
967+
| // patterns like `class Base(x:Int)` and `class Derived(x:Int) extends Base(x)`.
968+
| // This must become `class Derived(x_4762:Int) extends Base(x_4762)`.
969+
| // Otherwise, it is very hard to figure out whether uses of the identifier `x` refer to the base class x
970+
| // or the derived class x.
971+
| // When using that pattern, the class parameter `x_47672` should only be used in the `extends Base(x_4762)`
972+
| // clause and nowhere else. Otherwise, the compiler may well decide that this is not just a constructor
973+
| // parameter but also a field of the class, and we end up with two `x` fields. At best, this wastes memory;
974+
| // at worst both fields go out-of-sync for hard-to-debug correctness bugs.
975+
|
966976
|
967977
| override def fromNewNode(newNode: NewNode, mapping: NewNode => StoredNode): Unit = get().fromNewNode(newNode, mapping)
968978
| override def canEqual(that: Any): Boolean = get.canEqual(that)

integration-tests/tests/src/test/scala/Schema01Test.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,8 @@ class Schema01Test extends AnyWordSpec with Matchers {
9797
innerNode.get shouldBe node3
9898
}
9999
}
100+
"work around scala bug 4762, ie generate no extraneous fields" in {
101+
Class.forName("testschema01.nodes.Node1").getDeclaredFields.length shouldBe 0
102+
}
100103

101104
}

0 commit comments

Comments
 (0)