Page 1 of 2

def main is not shared

PostPosted: Fri May 15, 2009 3:59 am
by Charles
That is to say, it does not have to be "shared" anymore. "Hello world" becomes:
class Hello
def main
print 'Hello, world.'
So `def main` can now be specified without `is shared` provided that the containing type has a parameterless initializer (so that it can be instantiated). And it can still be `shared` if that's required or desired.

Furthermore, `main` can no longer take arguments or return a value. Use `CobraCore.commandLineArgs` and `CobraCore.exit(code)` instead.

Finally, you can use the new -main: command line option to disambiguate when there is more than one `main` method. See cobra -h

This is in the repository. Any questions or problems, let me know.

Re: def main is not shared

PostPosted: Thu Jul 30, 2009 11:15 am
by arisawa
is this case where 'is shared' is required?
I got Cobra.Lang.AssertException.
class Program
var testObject as String

def main
testObject = "Hello"
print testObject

Re: def main is not shared

PostPosted: Fri Jul 31, 2009 3:44 am
by hopscc
No.
The exception here is caused by the attribute testObject not being initialized after the constructor completes.
You'll note the assert failure goes away if you comment the
Code: Select all
var testObject as String
line

Also I suspect that what you're doing here isnt what you think you're doing :
You're declaring an attribute ( var testObject) that is not initialized and never used then
using a local variable ( testObject) in the main method
perhaps what you were thinking was to declare and use an instance attribute
Code: Select all
class Program
    var testObject as String

    def main
        .testObject = "Hello"   # note the leading '.'
        print .testObject


This will still assert exception cos testObject isnt initialized in the ( implicit default noArg constructor)
so you need this
Code: Select all
class Program
    var testObject as String

    cue init
        base.init   # required now
        .testObject = ''initted'     # this makes the assert exception go away

    def main
        .testObject = "Hello"   # note the leading '.'
        print .testObject

Re: def main is not shared

PostPosted: Fri Jul 31, 2009 3:50 am
by hopscc
Chuck,
since this is now treating (def) main even more specially and applying some otherwise invisible wrapping around its handling shouldnt this
become
Code: Select all
cue main


to be logically consistent with the other special use/case/handling def methods ( init, compareTo, hashcode,...) ??

Re: def main is not shared

PostPosted: Fri Jul 31, 2009 6:53 am
by Charles
Another approach is just to give a basic value to the variable in the declaration:
class Program

var testObject = ''

def main
.testObject = 'Hello'
print .testObject

Another approach is to make the type nilable:
class Program

var testObject as String?

def main
.testObject = 'Hello'
print .testObject


I often prefer the first because non-nilable variables are unlikely to cause null-ref exceptions at run-time (unless of course you don't initialize their value during declaration or cue.init). Of course, it would be nice if Cobra issued at least a warning for obvious cases that are going to fail.

Regarding "cue", the thing I found strange about "main" is that it is not really an object-oriented cue like .init or (the upcoming) .hash. It is program-oriented and really has no special effect on the object. In the end, I chose "def".

Re: def main is not shared

PostPosted: Sat Aug 01, 2009 10:46 am
by arisawa
oops! I forgot period... :shock:
also, I learned necessity for initializing the attribute.

is 'is shared' hardly necessary anymore for main?

by the way,
This will still assert exception cos testObject isnt initialized in the ( implicit default noArg constructor)
so you need this

what "cos"?
"cos" is omitted phrase?

Thanks,

Re: def main is not shared

PostPosted: Sat Aug 01, 2009 3:45 pm
by Charles
Correct, the "is shared" is no longer necessary. When absent, the class will be instantiated and its .main method invoked.

"cos" is slang for "because"

Re: def main is not shared

PostPosted: Sun Aug 16, 2009 10:20 pm
by arisawa
I tried IE Browser test.
In the following cases, should it be main is shared?
If it is main is not-shared, [System.STAThreadAttribute] is needed in MainWrapper.cobra.cs
Code: Select all
use System.Windows.Forms

class MyForm inherits Form
   var _txb as System.Windows.Forms.TextBox
   var _btn as System.Windows.Forms.Button
   var _web as System.Windows.Forms.WebBrowser
   
   cue init
      base.init
      .text = 'IE8 test'
      _txb = TextBox(parent=this, text="http://")
      _btn = Button(parent = this, text="go to site")
      _web = WebBrowser(parent = this)
      listen _btn.click, ref .handleClick
      
   def handleClick(sender, args as EventArgs)
      try
         _web.navigate(Uri(_txb.text))
      catch
         _txb.text = 'ERROR'
      
class Program
   def main has STAThread
      Application.run(MyForm())

Re: def main is not shared

PostPosted: Mon Aug 17, 2009 3:06 am
by Charles
So the attributes are not being carried over. Yes, you'll have to add "is shared" for now.

Would you enter a ticket for this?

Re: def main is not shared

PostPosted: Mon Aug 17, 2009 3:37 am
by arisawa
I entrust you.
because knowledge for my ticket-system is shallow.
also, my English is strange.