Page 1 of 1

Syntax for new constraint?

PostPosted: Thu Jul 22, 2010 9:17 pm
by nevdelap
Hi. Quick one. What's the syntax for declaring a new constraint?

Code: Select all
def method<of T>
    where T must be XXX
    t = T() # error: Cannot create an instance of the variable
            # type "T" because it doesn"t have the new() constraint
    ...

Re: Syntax for new constraint?

PostPosted: Fri Jul 23, 2010 5:06 am
by hopscc
rumour has it that you use callable

Code: Select all
class G1<of TArg>
        where TArg must be class, callable

        cue init
                base.init
                a = TArg()  # instantiate a generic parameter
                b = TArg()
                assert a is not b
                assert a.getHashCode
                assert b.getHashCode

see Tests/240-generics/300-declare-generic-classes/602-instantiate-generic-arg.cobra
and new constraint search

This stuff should be in the (NYI) wiki pages on generics