Forums

def main is not shared

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

def main is not shared

Postby Charles » Fri May 15, 2009 3:59 am

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.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: def main is not shared

Postby arisawa » Thu Jul 30, 2009 11:15 am

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
arisawa
 
Posts: 51

Re: def main is not shared

Postby hopscc » Fri Jul 31, 2009 3:44 am

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
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: def main is not shared

Postby hopscc » Fri Jul 31, 2009 3:50 am

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,...) ??
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: def main is not shared

Postby Charles » Fri Jul 31, 2009 6:53 am

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".
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: def main is not shared

Postby arisawa » Sat Aug 01, 2009 10:46 am

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,
arisawa
 
Posts: 51

Re: def main is not shared

Postby Charles » Sat Aug 01, 2009 3:45 pm

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"
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: def main is not shared

Postby arisawa » Sun Aug 16, 2009 10:20 pm

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())
arisawa
 
Posts: 51

Re: def main is not shared

Postby Charles » Mon Aug 17, 2009 3:06 am

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?
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: def main is not shared

Postby arisawa » Mon Aug 17, 2009 3:37 am

I entrust you.
because knowledge for my ticket-system is shallow.
also, my English is strange.
arisawa
 
Posts: 51

Next

Return to Discussion

Who is online

Users browsing this forum: No registered users and 30 guests

cron