scala-subscript / subscript

9 stars 2 forks source link

Annotations needed for if constructs. #35

Open AndreVanDelft opened 8 years ago

AndreVanDelft commented 8 years ago

The eyetest Login class contains:

selectFile = val   chooser = new FileChooser
             if    chooser.showSaveDialog(null)==FileChooser.Result.Approve
             then ^chooser.selectedFile

The call to showSaveDialog should be done in the gui thread, so an @gui: annotation would be needed:

selectFile = val      chooser = new FileChooser
             @gui: if chooser.showSaveDialog(null)==FileChooser.Result.Approve
             then    ^chooser.selectedFile

However, other than with while, it seems to be hard to allow for this in front of if:, since an annotation is a termRaw, and an if construct is an Expr7. Maybe it can be done.

For the time being the programmer could enclose the if construct in braces. Or maybe it would be convenient to write

if @gui:  chooser.showSaveDialog(null)==FileChooser.Result.Approve

That syntax could be allowed without ambiguity problems. But maybe it would not be a good idea.

BTW it would be good if the implicit there parameter for the call to gui would inform the caller that it has some executable code, i.e. a code fragment, or a while, if, annotation, script call (and maybe more).

def executeCode[N <: CallGraphNode, R](n: N): R = {

This definition should than also become more specific than just N <: CallGraphNode.

AndreVanDelft commented 8 years ago

while may be preceded by an annotation; if cannot have that without cluttering up the syntax. I think now we need an uniform approach for both.

if @gui:  chooser.showSaveDialog(null)==FileChooser.Result.Approve
then ... else ...

would work. Then also allow for

while @gui: (chooser.showSaveDialog(null)==FileChooser.Result.Approve)

The latter would have the same meaning as having the annotation in front of while.

anatoliykmetyuk commented 8 years ago

Neither will work due to the nature of the predicate. It is a piece of pure Scala code on the VM value, not a script, hence cannot be annotated. Possible solution is to use do-then-else instead; the predicate of do is a script:

do selectFile(chooser) then foo else bar

selectFile = @gui: {!chooser.showSaveDialog(null)==FileChooser.Result.Approve!} ~~(result: Boolean)~~> if result then [+] else [-]
AndreVanDelft commented 8 years ago

I disagree: syntactically it is possible, and semantically the annotation would as usual be executed when the if or while is activated, and it can do its work using the there pointer. Thus while @a: c would do exactly the same as @a: while c, and if @a: c then ... would be similar.

anatoliykmetyuk commented 8 years ago

Indeed, seems possible. On Feb 19, 2016 3:16 PM, "André van Delft" notifications@github.com wrote:

I disagree: syntactically it is possible, and semantically the annotation would as usual be executed when the if or while is activated, and it can do its work using the there pointer. Thus while @a: c would do exactly the same as @a https://github.com/a: while c, and if @a: c then ... would be similar.

— Reply to this email directly or view it on GitHub https://github.com/scala-subscript/subscript/issues/35#issuecomment-186209101 .