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

Problem with structural typing #967

Closed
scabug opened this issue May 26, 2008 · 8 comments
Closed

Problem with structural typing #967

scabug opened this issue May 26, 2008 · 8 comments

Comments

@scabug
Copy link

scabug commented May 26, 2008

While trying to simulate SML:

scala> type Nat = { type T; val z: T; def s(arg: T): T }
defined type alias Nat

scala> val nat : Nat = new { type T = Int; val z = 0; def s(arg:Int) = arg + 1 }
nat: Nat = $$anon$$1@150ed68

scala> nat.z
res0: nat.T = 0

scala> nat.s(nat.z)
java.lang.NoSuchMethodException: $$anon$$1.s(java.lang.Object)
        at java.lang.Class.getMethod(Class.java:1581)
        at .reflMethod$$Method1(<console>:7)
        at .<init>(<console>:7)
        at .<clinit>(<console>)
        at RequestResult$$.<init>(<console>:3)
        at RequestResult$$.<clinit>(<console>)
        at RequestResult$$result(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflec...
@scabug
Copy link
Author

scabug commented May 26, 2008

Imported From: https://issues.scala-lang.org/browse/SI-967?orig=1
Reporter: Geoffrey Alan Washburn (washburn)

@scabug
Copy link
Author

scabug commented Jan 14, 2009

@odersky said:
Milestone next_bugfix deleted

@scabug
Copy link
Author

scabug commented Apr 17, 2009

@dubochet said:
I now understand why this is a problem. Unfortunately, without runtime types (or manifests) this bug cannot be fixed in another way than to have the compiler reject that code.

More precisely --- take a deep breath at that point ---, the compiler must reject method calls on structural types that contain methods in which a parameter is defined as a type member of the structural type.

I'll implement that test when I have time.

@scabug
Copy link
Author

scabug commented Nov 8, 2009

@dubochet said:
Fixed in r19442.

@scabug
Copy link
Author

scabug commented Dec 29, 2012

@milessabin said:
I'm going to raise this from the dead, because a very minor variation on Geoffrey's original example fails in exactly the same way. This isn't a regression in 2.10 ... I don't think the problem was ever properly fixed.

scala> import scala.language.reflectiveCalls
import scala.language.reflectiveCalls

scala> type Nat = { type T; val z: T; def s()(arg: T): T }
defined type alias Nat

scala> val nat : Nat = new { type T = Int; val z = 0; def s()(arg:Int) = arg + 1 }
nat: Nat = $anon$1@4a2ead0a

scala> nat.z
res0: nat.T = 0

scala> nat.s()(nat.z)
java.lang.NoSuchMethodException: $anon$1.s(java.lang.Object)
        at java.lang.Class.getMethod(Class.java:1622)
        at .reflMethod$Method1(<console>:11)
        at .<init>(<console>:11)
        at .<clinit>(<console>:11)
        at .<init>(<console>:7)
        at .<clinit>(<console>)
        at $print(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:731)
        at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:980)
        at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:570)
        at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:601)
        at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:565)
        at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:745)
        at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:790)
        at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:702)
        at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:566)
        at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:573)
        at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:576)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:867)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
        at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
        at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
        at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:83)
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

@scabug
Copy link
Author

scabug commented Dec 31, 2012

@paulp said:
See also #5378, which I never got back to somehow. There are probably a whole bunch of variations on this available given the ad hoc manner in which it is recognized.

@scabug
Copy link
Author

scabug commented May 20, 2013

@JamesIry said:
2.10.2 is about to be cut. Kicking down the road and un-assigning to foster work stealing.

@scabug
Copy link
Author

scabug commented Nov 21, 2016

Olivier Blanvillain (OlivierBlanvillain) said:
I think this can be closed, scalac now properly rejects the Nat type in Miles comments with an error: Parameter type in structural refinement may not refer to a type member of that refinement.

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

1 participant