language-design

Why do standard classes sometimes have seemingly unrelated methods?

£可爱£侵袭症+ 提交于 2019-12-02 06:11:09
问题 While studying the standard Java library and its classes, i couldn't help noticing that some of those classes have methods that, in my opinion, have next to no relevance to those classes' cause. The methods i'm talking about are, for example, Integer#getInteger, which retrieves a value of some "system property", or System#arraycopy, whose purpose is well-defined by its name. Still, both of these methods seem kinda out of place, especially the first one, which for some reason binds working

await without declaring function as async

烂漫一生 提交于 2019-12-02 02:53:10
问题 Actually in Dart, in order to use await in function body, one need to declare whole function as async : import "dart:async"; void main() async { var x = await funcTwo(); print(x); } funcTwo() async { return 42; } This code wouldn't work without marking main() as async Error: Unexpected token 'await'. But, doc says "The await expressions evaluates e , and then suspends the currently running function until the result is ready–that is, until the Future has completed" (Dart Language Asynchrony

Has the design of marker interfaces like Java's Serializable or Cloneable evolved in C#?

孤街醉人 提交于 2019-12-02 02:29:17
Java provides java.io.Serializable and java.lang.Cloneable in his standard library (and special support for it in the language and the JVM) for tasks around deserializing/serializing/cloning. Has C# chosen a different path to provide this functionality, how does the implementation and code using it differ from Java and why was it done this way? As an example, why does C# use both an attribute (annotation) and an interface for serialization? .NET doesn't use ISerializable as just a marker interface. It acts not only as a marker, but also allows you to control exactly how .NET will serialize the

Why do standard classes sometimes have seemingly unrelated methods?

99封情书 提交于 2019-12-02 01:45:30
While studying the standard Java library and its classes, i couldn't help noticing that some of those classes have methods that, in my opinion, have next to no relevance to those classes' cause. The methods i'm talking about are, for example, Integer#getInteger , which retrieves a value of some "system property", or System#arraycopy , whose purpose is well-defined by its name. Still, both of these methods seem kinda out of place, especially the first one, which for some reason binds working with system resources to a primitive type wrapper class. From my current point of view, such method

Why did python choose commas over parenthesis in tuple design?

♀尐吖头ヾ 提交于 2019-12-02 01:16:08
问题 From python wiki Multiple Element Tuples In Python, multiple-element tuples look like: 1,2,3 ... but again, it is the commas, not the parentheses, that define the tuple. Oh, really?! Then why: >>> tuple((((((1, 2, 3)))))) # creates a valid tuple # (1, 2, 3) >>> tuple(1, 2, 3, ) # But not here # TypeError: tuple() takes at most 1 argument (3 given) More seriously, I don't get why the parenthesis was not chosen over the commas? Because I think it would create a paradox when: >>> 1, # is a valid

Why can't I add a tuple to a list with the '+' operator in Python?

自古美人都是妖i 提交于 2019-12-01 21:16:51
问题 Python not support adding a tuple to a list: >>> [1,2,3] + (4,5,6) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate list (not "tuple") to list What are the disadvantages for providing such a support in the language? Note that I would expect this to be symmetric: [1, 2] + (3, 4) and (1, 2) + [3, 4] would both evaluate to a brand-new list [1, 2, 3, 4] . My rationale is that once someone applied operator + to a mix of tuples and lists, they

Why did python choose commas over parenthesis in tuple design?

烂漫一生 提交于 2019-12-01 20:50:17
From python wiki Multiple Element Tuples In Python, multiple-element tuples look like: 1,2,3 ... but again, it is the commas, not the parentheses, that define the tuple. Oh, really?! Then why: >>> tuple((((((1, 2, 3)))))) # creates a valid tuple # (1, 2, 3) >>> tuple(1, 2, 3, ) # But not here # TypeError: tuple() takes at most 1 argument (3 given) More seriously, I don't get why the parenthesis was not chosen over the commas? Because I think it would create a paradox when: >>> 1, # is a valid tuple # (1,) >>> tuple([1]) # Or this # (1,) >>> tuple(1) # But not this one # TypeError: 'int' object

Short circuit evaluation using procedures

依然范特西╮ 提交于 2019-12-01 18:56:22
I am currently developing a compiler for a very limited object oriented language. I want to treat all values as objects and operators on those values will be implemented as methods. The compiler transforms the program into assembler for a stack-based virtual machine. During compilation I transform integer literals into objects of a special "Integer" class. Arithmetic operators are implemented as methods of that class (using inline assembler). So that 4 + 5 basically equals to 4.add(5) . The problem I am facing right now is the special case for boolean values. If there is an if statement: if(10

Why can't I use an array index on the return value of a function? [closed]

霸气de小男生 提交于 2019-12-01 18:54:17
Why can't I do this? explode(',','1,2,3', 1)[0] Every other language supports it. Answers I'm looking for: (since people seem to think this is a pointless rant) Is there some sort of a difference between how a variable and a return value behaves that I should be made aware of? Was this a design decision? Were they just lazy? Is there something about the way the language was built that makes this exceedingly difficult to implement? Why can't I do this? Because PHP doesn't currently support this syntax. But you can pass it to a function, e.g.: current(explode(',','1,2,3', 1)); Otherwise, you

Why can't I add a tuple to a list with the '+' operator in Python?

一世执手 提交于 2019-12-01 18:24:02
Python not support adding a tuple to a list: >>> [1,2,3] + (4,5,6) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate list (not "tuple") to list What are the disadvantages for providing such a support in the language? Note that I would expect this to be symmetric: [1, 2] + (3, 4) and (1, 2) + [3, 4] would both evaluate to a brand-new list [1, 2, 3, 4] . My rationale is that once someone applied operator + to a mix of tuples and lists, they are likely to do it again (very possibly in the same expression), so we might as well provide the list