vickumar1981 / jupyter-dart-kernel

Dart Kernel for Jupyter Notebooks
MIT License
35 stars 4 forks source link

Define a class #5

Closed STRockefeller closed 1 year ago

STRockefeller commented 1 year ago

It seems that the contents in the code blocks of jupyter note will be insert to void main()

so the following code

class MyClass{
  MyClass();
}

will become

void main() {
class MyClass{
  MyClass();
}
}

and go wrong while executing.

Any suggestions?

vickumar1981 commented 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.

STRockefeller commented 1 year ago

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.

vickumar1981 commented 1 year ago

I think the issue might now be fixed on masterwith 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.

vickumar1981 commented 1 year ago

@STRockefeller if you can confirm this is fixed with the latest commits, I can close out the issue. Thanks.

STRockefeller commented 1 year ago

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();
}
vickumar1981 commented 1 year ago

@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.

STRockefeller commented 1 year ago

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.

vickumar1981 commented 1 year ago

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).

PhilipHayes commented 3 months ago

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',
  };
}
}
PhilipHayes commented 3 months ago

I added

or curr_line.startswith("sealed ")

and that fixed it.