static-typing

Why does foreach skip compile time type checking on interface types?

感情迁移 提交于 2019-12-06 01:41:45
问题 When I use a foreach loop in C#, it appears that no compile time type checking is performed if the item type is an interface type. E.g. class SomeClass {} interface SomeInterface {} IEnumerable<SomeClass> stuff; foreach(SomeInterface obj in stuff) { // This compiles - why!? } This will happily compile and cause an exception at runtime, when it is clear at compile time this makes no sense. If I change the item type from SomeInterface to another class, then compile time type-checking is

Clojure static typing

醉酒当歌 提交于 2019-12-04 16:30:33
问题 I know that this may sound like blasphemy to Lisp aficionados (and other lovers of dynamic languages), but how difficult would it be to enhance the Clojure compiler to support static (compile-time) type checking? Setting aside the arguments for and against static and dynamic typing, is this possible (not "is this advisable")? I was thinking that adding a new reader macro to force a compile-time type (an enhanced version of the #^ macro) and adding the type information to the symbol table

Haskell type signature with multiple class constraints

只愿长相守 提交于 2019-12-04 15:20:27
问题 How can I have multiple class constraints, so if A is an Eq and B is a Num , I could say either f :: Eq a => a -> b` or f :: Num b => a -> b So, how can I have Eq a => and Num b => at the same time? f :: Eq a => Num b => a -> b , f :: Eq a -> Num b => a -> b , and f :: Eq a, Num b => a -> b didn't do what I wanted. 回答1: They're usually called class constraints , as Eq and Num are called type-classes. How about this? f :: (Eq a, Num b) => a -> b The parentheses are significant. 来源: https:/

Enforcing types on untyped data in TypeScript

Deadly 提交于 2019-12-04 11:28:08
I'm wondering if there are any tools or techniques that are being used to do low level validation on object data when using TypeScript. An example would be a JSON body of a POST request on a HTTP service. Typically I've created an interface for the expected data and then cast the data to that interface but I'm aware that this is superficial. Example: router.route('/supercres') .get((req, res, next) => { const typedBody = <SuperCresBody>req.body; }) interface SuperCresBody { name: string, yoyo: boolean, } The problem with enforcing the interface is that TypeScript is just a compile-time concept

Applying Extension method to generic class with generic type

老子叫甜甜 提交于 2019-12-04 11:21:58
问题 I was working with the generic class in vb.net. And it seems extension method cannot be applied to generic class without specifying the type. I have this generic class Public Class MyGeneric(Of T) 'Methods and properties go here ' ' End Class This is Ok <Extension()> _ Public Sub DoSomething(ByVal myGenericDoubleObj As MyGen(Of Double)) End Sub This is NOT Ok(IDE gives me error T as not defined.) <Extension()> _ Public Sub DoSomethingGeneric(ByVal myGenericObj As MyGen(Of T)) End Sub Is this

Is there a compiled* programming language with dynamic, maybe even weak typing?

回眸只為那壹抹淺笑 提交于 2019-12-04 09:53:27
问题 I wondered if there is a programming language which compiles to machine code/binary (not bytecode then executed by a VM, that's something completely different when considering typing) that features dynamic and/or weak typing, e.g: Think of a compiled language where: Variables don't need to be declared Variables can be created during runtime Functions can return values of different types Questions: Is there such a programming language? (Why) not? I think that a dynamically yet strong typed,

Why does foreach skip compile time type checking on interface types?

末鹿安然 提交于 2019-12-04 05:22:25
When I use a foreach loop in C#, it appears that no compile time type checking is performed if the item type is an interface type. E.g. class SomeClass {} interface SomeInterface {} IEnumerable<SomeClass> stuff; foreach(SomeInterface obj in stuff) { // This compiles - why!? } This will happily compile and cause an exception at runtime, when it is clear at compile time this makes no sense. If I change the item type from SomeInterface to another class, then compile time type-checking is restored: IEnumerable<SomeClass> stuff; foreach(Random obj in stuff) { // This doesn't compile - good! } Why

F# and Operator Overloads: (>) and (^)

别等时光非礼了梦想. 提交于 2019-12-04 01:42:43
问题 Ok, so can someone explain to me why F# allows you to overload the > and ^ operators, but doesn't allow you to use them? + (op_Addition): Works just fine. ^ (op_Concatenate): Compiler error in F#. Apparently only strings can be concatenated. > (op_GreaterThan): Runtime Error – Failure during generic comparison: the type Program+OppTest4 does not implement the System.IComparable interface. If I compile my F# code as a library and use those operators from VB, they all work. If I use those

Is there support for static typing in ECMAScript 6 or 7?

一世执手 提交于 2019-12-03 22:11:18
Is there any support for static typing in ECMAScript 6? How about ECMAScript 7? No. But on the ECMA-Script Wikipage there is a paragraph about changes in ECMA-Script 7: The Seventh Edition is in a very early stage of development, but is intended to continue the themes of language reform, code isolation, control of effects and library/tool enabling from ES6. New features proposed include promises/concurrency, number and math enhancements, guards and trademarks (an alternative to static typing) , operator overloading, value types (first-class number-like objects), new record structures (records,

are there statically-typed functional languages?

我怕爱的太早我们不能终老 提交于 2019-12-03 10:59:53
this is a hard thing to google, maybe SO has better answers? i'm coming from a statically-typed background and investigating functional programming, but i'm not entirely excited about dynamic typing. curious what kind of options are out there. one answer to start: ActionScript 3 is statically-typed and offers some functional paradigms, though AS3 programs don't often use them. There are many. Haskell, OCaml and F# come to mind. If you are a Windows developer, F# is very nice and is well supported by Microsoft (along with a very strong StackOverflow community). Lisp family languages (Common