in dart 2.0,types are mandatory,will dart 2.0 or 2.x add extension methods like
static int WordCount(this String str){
// custom String method here
}
No, there won't be extension methods in 2.0.
There is a lot of work going on to be able to move faster after 2.0.
I think extension methods is a popular wish, but there is no definitive statement Dart will get them.
Updated answer
As of today, Dart 2.6, added extension methods support.
Related
I currently have two enums
enum UserVisibleSettings {
track,
mark,
}
enum UserVisibleTwo {
thing,
otherthing,
}
And the goal is that I want to make an extension that will allow me to convert an enum into a List<String>
I'm able to make an extension on UserVisibleSettings or UserVisibleTwo but how can I make an extension on all enums?
Not possible in current version of Dart since enums does not have a shared interface in Dart which are specific for enums.
What's the best way to use Dagger to inject dependencies into classes, especially zero-arg constructor classes like Activities, with Dagger? Will Dagger 2 possibly bring improvements to the situation?
Thanks in advance.
Since Kotlin M13 release, a new property has been especially added in order to support dependency injection (like with Dagger 1&2) and other frameworks.
It's called lateinit property. Taken from the documentation:
class Example {
#Inject
lateinit var bar: Bar
}
In a nutshell, bar has no initializers but is declared as a non-null type. If you try to read it before the initialization, an exception is thrown.
Otherwise, once it's initialized using Dagger, it can be used as a normal property.
Everything is well explained in the language doc and you can also check the blog post relative to the M13 release there.
I wasn't updating my answer for a while and it got outdated. Also noticed here new answer from #Ben that works for M13/M14.
Decided it would best if I redirect all of you interested to my template project which I try to keep up to date with latest Kotlin and Dagger 2 versios. Kotlin + Dagger 2 sample
I am describing there how to inject objects, including multiple annotations etc.
Dagger relies on annotation processing, which is not supported yet in Kotlin, as far as I know. But they say, it is planned. And while, you can either extend java classes with Dagger dependencies, or try some reflection based injection framework - Guice, Roboguice
for using dagger annotations in Kotlin
1- // dagger add dependencies in app level
implementation 'com.google.dagger:dagger:2.38.1'
kapt 'com.google.dagger:dagger-compiler:2.38.1'
add in app level/gradle
plugins {
id 'kotlin-kapt'
}
now you can use annotations like #Inject #Component etc
class Test{
#Inject
lateinit var name: String
}
In Swift it's not necessary to prefix classes anymore as their module acts as the namespace.
What about prefixing extension functions? For example:
extension UIImage {
public func hnk_hasAlpha() -> Bool { ... }
}
On one hand Swift is not dynamic so collisions would generate compiler errors.
But what happens if compiled code runs in a future iOS/OS X version in which one of my extension methods is added? Would methods in different modules be considered different symbols even if they have the same signature?
Does it make a difference if the extended class is a NSObject subclass or a pure Swift class?
There's some subtlety here:
Extensions of Objective-C types are implemented as Objective-C categories, with all that implies.
Extensions of Swift types, however, are only in effect where visible due to imports. This means that you can't accidentally stomp on a private system method (whether now or one introduced in the future), and if the system introduces a public method with the same name as yours, you'll get a compile-time failure when you rebuild, but your existing app won't break.
You should check those threads also:
Name collisions for extension methods from different frameworks - quote from thread:
regardless the application code imports which Framework, it seems, the actual called implementation depends on the order in Linked Frameworks and Libraries in "First come, first served" manner. But, as far as I know, this behavior is not guaranteed.
and Swift Extension: same extension function in two Modules which also confirms the same problem.
So based on that for Objective-C objects such as UIImage from your example name collisions are possible and you might see unexpected behaviour if there will be two methods with the same name in two different extensions for Objective-C object.
It's 2013, and a major platform coughUnity3dcough is seemingly still partly stuck on 2.x (latest Unity is newer, but isn't production-quality yet for some features). It seems there's a lot of stuff that was missing from the language until 3.5 - e.g. Func and Action - so I'm trying to find a way to do this without using 3.x.
Here's a great, very neat solution for 3.x: Pass Method as Parameter using C#
I tried Microsoft's site, but it's currently refusing to give out docs for anything earlier than 4.5 (was working a few weeks ago, but now their Javascript always errors out :( ).
...what's the equivalent in 2.x? Or is there no way of doing this (sad face) ?
OR: am I missing something obvious about using Func? I get compiler errors that it's an undefined symbol, and I'm using the same context as I found in all the different online resources / examples :(
It's important to distinguish between language versions and platform versions... and likewise functionality. Currently you're mixing up various aspects.
Func and Action aren't part of the C# language at all - they're just delegate types which are part of the .NET framework. If you're using a version of the framework which doesn't include the delegate types you want, just declare your own. I'd suggest naming them differently to the .NET ones so that it won't matter if you then upgrade - there won't be any naming collisions. So:
public delegate void MyAction();
public delegate void MyAction<T>(T input);
... etc
Then once the relevant types are in place, you can use method group conversions as normal:
public void DoSomething(MyAction<string> action) { ... }
public void Foo(string text) { ... }
...
DoSomething(Foo);
Use delegates! Here are some links
http://msdn.microsoft.com/en-us/library/ms173171(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx
if i use Doctrine_Core::getTable('User')-> i will have no auto completion.
isnt it better to just use User:: for autocompletion?
and of course i have to define the methods static
what is the benefit with using getTable except that i can use a non static method?
Because User:: would need to call a static method, but the methods are all non-static, so that would be invalid code.
Read up on the singleton pattern.
If you just need auto completion, you can try the plugin here: http://www.symfony-project.org/plugins/sfDoctrineTableGetterPlugin
It builds a very light and fast auto-generated class and makes it possible to have code completion in all major IDE's like Eclipse PDT, Zend Studio, Net Beans.