Closed STRockefeller closed 1 year ago
@STRockefeller sorry for the delayed response.
https://github.com/vickumar1981/jupyter-dart-kernel/blob/master/dartkernel.py#L74
The parsing logic will probably have to be fixed up to pull out classes, after it parses for import statements, and then rearrange the output file to be:
[import statements]
[classes]
[main method]
also, i wonder if classes are sticky inside the notebook? If you declare a variable on cell 1, are you able to reference it in cell 2, or after cell 1 has executed inside the notebook?
thanks for bringing this up. will try to test over the weekend and push and update if possible.
Thanks for your reply.
also, i wonder if classes are sticky inside the notebook?
The following is the performance of Microsoft's .net interactive for your reference.
If I defined 3 cells in the note book.
1st cell
// old one
public class A{
public void Description()=>Console.WriteLine("old description");
}
2nd cell
// new one
public class A{
public void Description()=>Console.WriteLine("new description");
}
3rd cell
A a = new();
a.Description();
If the order of execution is 1st -> 2nd -> 3rd, you will get a result like this
new description
and 1st->3rd (or 2nd -> 1st -> 3rd) will get
old description
So I thought it would be a good idea to have classes stick in a notebook. It would be even better if they could be replaced by redeclaration.
I think the issue might now be fixed on master
with these 2 commits:
https://github.com/vickumar1981/jupyter-dart-kernel/commit/402da7a5c2e0fe702323003f22af8ce619a89ed3 https://github.com/vickumar1981/jupyter-dart-kernel/commit/e7403124454f35e492c52b6e6a0d7fb6c0969961
Testing it out, it looks like the cells might be "sticky" in jupyter by default. There's an execution order to the cells and I think the kernel.py might be handling it correctly.
Please let me know if this addresses the issue and I can close it out.
If the cells aren't "sticky", then I guess we can open a new ticket for that. Thanks @STRockefeller.
@STRockefeller if you can confirm this is fixed with the latest commits, I can close out the issue. Thanks.
Thank you for your response. I can now declare a class in the cell, but it seems that the parsed code in certain situations does not meet expectations. For example, in the following situation.
in ipynb
expected
class A {
A(){
print("A");
}
}
void main() {
var a = A();
}
actual
class A {
A(){
print("A");
}
void main() {
}
var a = A();
}
@STRockefeller good catch. I updated with another commit: https://github.com/vickumar1981/jupyter-dart-kernel/commit/02561d9081cefa898eba1a3af6fef41bc6e81f03
Should fix the issue with the example code above. Let me know if that works for you. Thanks again for reporting the issue and providing valuable feedback.
I just did a quick test, and I think this issue can now be closed. Thank you for providing this convenient kernel for everyone to use.
np @STRockefeller . Thanks for reporting the issue and providing sample code to help fix it.
Also, I have this kernel along w/ 15 other languages on my website if you ever want to test/check it out w/o having to install anything (log-in should provide a free 15 day trial, or 1 year if using a .edu address).
So this actually does not allow you to make sealed classes
sealed class Vehicle {}
class Car extends Vehicle {}
class Truck implements Vehicle {}
class Bicycle extends Vehicle {}
// Subclasses can be instantiated.
Vehicle myCar = Car();
String getVehicleSound(Vehicle vehicle) {
return switch (vehicle) {
Car() => 'vroom',
Truck() => 'VROOOOMM',
Bicycle() => 'ring ring',
};
}
Produces:
class Car extends Vehicle {}
class Truck implements Vehicle {}
class Bicycle extends Vehicle {}
void main() {
sealed class Vehicle {}
// Subclasses can be instantiated.
Vehicle myCar = Car();
String getVehicleSound(Vehicle vehicle) {
return switch (vehicle) {
Car() => 'vroom',
Truck() => 'VROOOOMM',
Bicycle() => 'ring ring',
};
}
}
I added
or curr_line.startswith("sealed ")
and that fixed it.
It seems that the contents in the code blocks of jupyter note will be insert to
void main()
so the following code
will become
and go wrong while executing.
Any suggestions?