Menu

What does '!' mean?

2002-08-28
2002-08-28
  • Nobody/Anonymous

    eg
    <Any T> String toString(!T)

     
    • Daniel Bonniot

      Daniel Bonniot - 2002-08-28

      '!' denotes a non-null type. It si the dual of '?', which denotes a option type.
      '!' is only required to specify that a type variable is ranging over non-null types. For type constants like String, File, it is the default. So String = !String.

      In this case, the declaration says that toString can be called on any value except null.

       
      • Nobody/Anonymous

        Ok, thanx.

        And how do I overload toString?
        [code]
        public String toString(CType t);

        toString(t@PolymorphicType) = "poly("+t.getBoundedVar()+", "+t.getBoundedType()+")";

        toString(t@CType) = "undefined";
        [/code]

        This gives a abiguity error because toString is defined in nice.lang also.

         
        • Daniel Bonniot

          Daniel Bonniot - 2002-08-28

          Just drop the first line. Since toString is already defined, you don't need to do it again.
          You need to *implement* it, which is what the last two lines do.

           
    • Nobody/Anonymous

      I have declared toString in another dir then the types. How will my function be found?

       
      • Daniel Bonniot

        Daniel Bonniot - 2002-08-28

        You don't need to declare toString, it is declared in nice.lang.

        If you mean "implement the method", then it makes sense to do it in the same package as the class definitions. Which means in the same dir, but it can be in a different file.

         
        • Nobody/Anonymous

          But how is the implementation found? And what happens if there are more implementations of toString (that are ambiguous)? Which one will be chosen?

           
          • Daniel Bonniot

            Daniel Bonniot - 2002-08-28

            An implementation is found in any program that imports the module containing that implementation.
            Concretely, the code for a method is created at then end of the compilation when all the packages are known. All the implementations are put together with the code that selects an implementation given the type of the arguments.
            It is always the most specific implementation that will be chosen.
            Missing implementations ambiguities are discovered at compile time.

             
            • Nobody/Anonymous

              But if I give a CType to eg a List en that list is going to be printed, will my toString be used, or the default implementation?

               
              • Daniel Bonniot

                Daniel Bonniot - 2002-08-28

                Well, if you ask if the runtime type is used: yes. So yes, that should be how it goes.
                Now the collection framework has not been worked on much yet, and toString is not implemented on collections.
                You could build your own Nil and Cons, and implement toString on it, and it would work.

                 

Log in to post a comment.

MongoDB Logo MongoDB