Forums

Properties with MetaInformation

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

Properties with MetaInformation

Postby terenced » Thu Feb 17, 2011 3:37 pm

I can't see to get MetaInformation to work with Auto properties.
In C#:
Code: Select all
[Serializable]
   public class Show
   {
      [XmlElement("showid")]
      public int ShowId
      {
         get;
         set;
      }
   
      [XmlElement("name")]
      public string Name {
         get;
         set;
      }
      
      public string DisplayName
      {
         get
         {
            return string.Format ("({0}) {1}", ShowId, Name);
         }
      }
   }

In Cobra:
class Show has Serializable
pro showId from var as int has XmlElement('showid')
pro name from var as String has XmlElement("name")
pro displayName as String
get
return '[.showId] - [.name]'

How should I get the MetaInfo?
terenced
 
Posts: 15
Location: Montreal, Quebec, Canada

Re: Properties with MetaInformation

Postby terenced » Thu Feb 17, 2011 7:46 pm

Woo who, I figured it out!
You have to but the has statement on a separate line
class Show has Serializable

pro showid from var = 0
has XmlElement('showid')
pro name from var = ''
has XmlElement("name")

pro displayName as String
get
return '[.showid] - [.name]'


Also, you have to make sure your properties have default values.
If you try
class Show has Serializable

pro showid from var as int
has XmlElement('showid')
pro name from var as String
has XmlElement("name")

pro displayName as String
get
return '[.showid] - [.name]'

you get errors:
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Cobra.Lang.AssertException:
sourceSite = /Users/terry/Development/Projects/bc.cobra:15 in Show.cue.init for object Show
info = nil
this = Show
_name is not nil = false
_name = nil

terenced
 
Posts: 15
Location: Montreal, Quebec, Canada

Re: Properties with MetaInformation

Postby Charles » Thu Feb 17, 2011 8:20 pm

There might be patch for this on one of the tickets--I'll have to check. Thanks for bringing this up.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Properties with MetaInformation

Postby hopscc » Fri Feb 18, 2011 4:54 am

I think you will find that this is not a bug and the assert failure has nothing to do with MetaInformation.

You've probably not noticed (and I dont think its clearly described in doc anywhere obvious) but its enforced (asserted) that
a class instances fields (unless declared as being nillable) are not allowed to be nil at the end of the initializer.
(so that once inited the instance is in a known valid state)

This is what youre seeing.

The auto generating property code is generating backing variables for the properties - theres no explicit constructor/initializer being
given so a noArg noop constructor is generated( by the compiler) at the end of which the String backing variable (_name for property name) is nil
triggering the assert failure.

I.e you can see the same effect with this code that has nothing to do with metainfo
Code: Select all
#cobra
class Show

    pro showid from var as int # =0

    pro name from var as String # = ''
       
    pro displayName as String
        get
            return '[.showid] - [.name]'
         
   def main is shared
      s = Show()


This goes away if the properties are explicitly initialized since that puts the backing variables in a non nil state by the end of the Initializer.

So its doing what its supposed to - telling you that your class code initialization is incorrect :)


Secondly

Please if you post code describing something failing make sure its complete - That way its a quick cut and paste and compile to see what the problem is - if its easy to do that in one step its more likely that it will be done rapidly and the results examined and something intelligible can be said/provided about the problem.

None of the cobra code (fragments) you give below compiles (much less runs) because its
a) missing a main method
b) missing a reference to the XmlAttribute
(and possibly for ease of reproduction c) missing the compiler cmdline invocation used)

my first few stabs at correcting this actually worked fine since the code I made up wasnt instantiating Show ( I use an explicitly shared main)
so thats 4 extra compile/run cycles plus some doc lookup and associated fiddling about to perhaps poorly reproduce what you already knew/had but didnt describe.

Fortunately (for your feedback) its a slow night...
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Properties with MetaInformation

Postby terenced » Fri Feb 18, 2011 6:20 am

The auto generating property code is generating backing variables for the properties - theres no explicit constructor/initializer being
given so a noArg noop constructor is generated( by the compiler) at the end of which the String backing variable (_name for property name) is nil
triggering the assert failure.


In C#, primitive type, such as string and int, have default value. So for example,
Code: Select all
using System;

namespace TVRage
{
    public class Show
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var s = new Show();
            Console.WriteLine("Id = {0}", s.Id);
            Console.WriteLine("Name = {0}", s.Name);
        }
    }
}

Id = 0 and Name = ""
I assumed that is would be the same in Cobra ;)
I guess it has something to do the differences between value and reference types. Does Cobra treat everything as a reference type?

As for code example completeness, duly noted :D
terenced
 
Posts: 15
Location: Montreal, Quebec, Canada

Re: Properties with MetaInformation

Postby hopscc » Sat Feb 19, 2011 4:27 am

If you look at the source code cobra generates ( -kif) theres explicit code setting the initial value of the reference value _name to null.

Looks like I'm running against .Net/csc 3.5 and if you elide that explicit null setting in the C# code generated above and compile it
I get the same error implying the C# compiler also (implicitly) sets the same value to null.
( Which was also my understanding of what it did for uninitialised reference Type variables)

Would the the C# code you gave be specific to .Net4/csc4 (???).

If I run it as is I get C# compile errors
Code: Select all
567 xx:...hops/src/cobra/Tst> cobc0 cprop.cs
c:\home\hops\src\cobra\Tst\cprop.cs(7): error: "TVRage.Show.Id.get" must declare a body because it is not marked abstract or extern (C#)
c:\home\hops\src\cobra\Tst\cprop.cs(7): error: "TVRage.Show.Id.set" must declare a body because it is not marked abstract or extern (C#)
c:\home\hops\src\cobra\Tst\cprop.cs(8): error: "TVRage.Show.Name.get" must declare a body because it is not marked abstract or extern (C#)
c:\home\hops\src\cobra\Tst\cprop.cs(8): error: "TVRage.Show.Name.set" must declare a body because it is not marked abstract or extern (C#)
Compilation failed - 4 errors, 0 warnings
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Properties with MetaInformation

Postby terenced » Sat Feb 19, 2011 4:58 am

I ran the C# code .net 4.0 and mono 2.8/2.10, and it worked fine. I have been using C# since 2.0, and primitive types have always behaved that way.
I also run the kif code (minus the cobra lang stuff), and it behaved the same way as my original C# code.
I think it has to do with CobraLangInternal.CobraCore._willCheckNil && value==null) throw new System.ArgumentNullException("value"), but I could be wrong.
Code: Select all
using System;
//using CobraLangInternal = Cobra.Lang;

public class MainClass : System.Object
{

   int _ih_invariantGuard;

   public static void Main() {


         Show s = new Show();
         Console.WriteLine ("Id = {0}", s.Showid);
         Console.WriteLine ("Name = {0}", s.Name);
   }

   public   MainClass() : base() {

   }

} // class MainClass


public class Show : System.Object
{
   int _ih_invariantGuard;
   protected System.Int32 _showid;

   public virtual  System.Int32 Showid {

      get{

         return _showid;
      }

      set{

         _showid=value;

      }

   }

   protected System.String _name = null;

   //[CobraLangInternal.NotNull]
   //[System.Xml.Serialization.XmlElementAttribute("name")]

   public virtual  System.String Name {

      get{

         return _name;
      }

      set{

         //if (CobraLangInternal.CobraCore._willCheckNil && value==null) throw new System.ArgumentNullException("value");

         _name=value;
      }

   }

   //[CobraLangInternal.NotNull]
//

   public   Show() : base() {

      //if (Cobra.Lang.CobraCore._willCheckNil) {
      ////   if (CobraLangInternal.CobraCore._willCheckAssert && !((_name)!=null)) throw new CobraLangInternal.AssertException(new CobraLangInternal.SourceSite("/Users/terry/Development/Projects/bc.cobra", 15, "Show", "cue.init", this), new object[] { 0, "_name is not nil", ((_name)!=null), "_name", _name}, this, null);
      //}
   }
   
} // class Show
terenced
 
Posts: 15
Location: Montreal, Quebec, Canada

Re: Properties with MetaInformation

Postby Charles » Sat Feb 19, 2011 3:10 pm

When this C# code:
Code: Select all
Console.WriteLine("Name = {0}", s.Name);

is given an "s.Name" that is null, you just get a blank on the screen, which is not the same as an empty string. It's the way that "null" is treated by "WriteLine". Try:
Code: Select all
Console.WriteLine("Name = {0}, {1}", s.Name, s.Name == null);

And you should see "True".

All reference types in Cobra and C# default to null. I haven't heard that anything has changed in .NET 4.0, but if I missed something, let me know.

You can get rid of the problem by initializing to an empty string or changing the type to "String?"--a nullable string. I prefer the empty string unless I have an explicit purpose for the null case.

Regarding my earlier comment about a patch to fix this problem, I didn't explain clearly. I was only referring to the syntax of having the "has" on the same line.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 71 guests