Saturday, 15 February 2014

.net - Can type constraints be exclusive rather than inclusive? -



.net - Can type constraints be exclusive rather than inclusive? -

i working on unusual issue related vb.net , multiple generic interfaces. class have implements generic interface twice specifying 2 different generic type arguments. in effort find solution question (listed below), had thought couldn't find resources see if possible.

vb.net implement mutliple contravariant interface types

the msdn documentation here: http://msdn.microsoft.com/en-us/library/d5x73970.aspx explains type constraint usage, helpful not finish (or @ to the lowest degree language doesn't back upwards want).

normally constraint declared as:

public interface icopiesfrom(of tobject class) sub copyfrom(byval info tobject) end interface

but lets wanted exclude possible generic type argument instead of restrict subset.

public sub interface icopiesfrom(of tobject not specificbadtype)

can done?

it seem like: how phone call generic method type constraints when parameter doesn't have these constraints? duplicate, question bit different because i'd compile-time support.

edit:

here illustration utilize case (were possible):

'ideally, interface have definition public interface icopiesfrom(of tmodel not isecuretype) sub copyfrom(byval info tmodel) end interface 'target type exclude public interface isecuretype property accountvalue decimal property accountnumber string end interface public class accountmodel public overridable property accountcreated datetime public overridable property referredby guid end class public class detailedaccountmodel inherits accountmodel implements isecuretype public property accountnumber string implements isecuretype.accountnumber public property accountvalue decimal implements isecuretype.accountvalue end class public class profilemodel public property username string public property emailaddress string public property phonenumber string end class public class user 'composite representation view implements icopiesfrom(of profilemodel) implements icopiesfrom(of accountmodel) public property username string public property emailaddress string public property phonenumber string public property accountcreated datetime public property referredby guid public overridable overloads sub copyfrom(byval info profilemodel) implements icopiesfrom(of profilemodel).copyfrom if info isnot nil me.username = data.username me.emailaddress = data.emailaddress me.phonenumber = data.phonenumber end if end sub public overridable overloads sub copyfrom(byval info accountmodel) implements icopiesfrom(of accountmodel).copyfrom if info isnot nil me.accountcreated = data.accountcreated me.referredby = data.referredby end if end sub public function accountage() double homecoming (datetime.now - accountcreated).totaldays end function end class

in above scenario, never want able pass detailedaccountmodel user class never "accidentally" displayed , ideally caught @ compile time.

any of next acceptable answer:

a way accomplish exactly alternative approaches accomplish same outcome (or similar) a confirmation crazy , can't done (with sources of course).

no, there isn't way this. can ask considered language feature, think language creators sense encourage people write code violates open/closed principle.

for example, might know right there's specific class should not used generic type, what's stop else coming type don't know now. fact you're trying exclude particular type code smell indicates there's improve way approach problem you're trying solve. perhaps you're using inheritance should using composition. perhaps benefit segregating interfaces, or using improve separation of concerns. maybe not including feature language helping write more maintainable code.

.net vb.net generics type-constraints

No comments:

Post a Comment