as-keyword

Is there any keyword in Java which is similar to the 'AS' keyword of C# [duplicate]

倖福魔咒の 提交于 2019-12-30 03:48:27
问题 This question already has answers here : How to emulate C# as-operator in Java (6 answers) Closed 5 years ago . As we know C# provides an AS keyword which automatically performs a check whether the Object is of a type and if it is, it then casts it to the needed type else gives a null. public class User { } .... Object obj = someObj; User user = obj As User; ... Here in the above example, An Object obj can be of type User or some other type. The user will either get an object of type User or

Why is 'is' implemented as 'as'?

我怕爱的太早我们不能终老 提交于 2019-12-20 11:07:11
问题 Given that this is a very natural use case (if you don't know what as actually does), if (x is Bar) { Bar y = x as Bar; something(); } is effectively equivalent (that is, the compiler-generated CIL from the above code will be equivalent) to: Bar y = x as Bar; if (y != null) { y = x as Bar; //The conversion is done twice! something(); } EDIT: I guess I hadn't made my question clear. I wouldn't ever write the second snippet as it's of course redundant. I'm claiming that the CIL generated by the

python's `with` statement target is unexpectedly None

被刻印的时光 ゝ 提交于 2019-12-18 19:05:47
问题 seems like I do not understand something with---the python with statement. Consider this class: class test(object): def __enter__(self): pass def __exit__(self, *ignored): pass now, when using it with with , like in with test() as michael: print repr(michael) I would expect some output like <test instance at memore blah> . But I get None . Something wrong here? Any suggestions would help. (I am using Python 2.6.6.) EDIT: Thanks to ephement for pointing me to the documentation. The __enter__

Is there any keyword in Java which is similar to the 'AS' keyword of C# [duplicate]

感情迁移 提交于 2019-12-03 10:57:05
This question already has an answer here: How to emulate C# as-operator in Java 6 answers As we know C# provides an AS keyword which automatically performs a check whether the Object is of a type and if it is, it then casts it to the needed type else gives a null. public class User { } .... Object obj = someObj; User user = obj As User; ... Here in the above example, An Object obj can be of type User or some other type. The user will either get an object of type User or a null. This is because the As keyword of C# first performs a check and if possible then performs a casting of the object to

Why is 'is' implemented as 'as'?

烈酒焚心 提交于 2019-12-03 01:12:06
Given that this is a very natural use case (if you don't know what as actually does), if (x is Bar) { Bar y = x as Bar; something(); } is effectively equivalent (that is, the compiler-generated CIL from the above code will be equivalent) to: Bar y = x as Bar; if (y != null) { y = x as Bar; //The conversion is done twice! something(); } EDIT: I guess I hadn't made my question clear. I wouldn't ever write the second snippet as it's of course redundant. I'm claiming that the CIL generated by the compiler when compiling the first snippet is equivalent to the second snippet, which is redundant.