Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoiding types with same name conflict in Kotlin?

i am very new to kotlin, and i am having some trouble with an application i am building from a book. i have two classes in separate packages named Forecast and i am trying to define some functions in one that package that uses the class of same name inside it. the book said to import the Forecast class as ModelForecast and i did but now i am having a problem tracking down the source of this type mismatch error. it seems my convertForecastListToDomain() method is expecting something the other? please help me find the mistake that i am making. i would not be surprised if it is something very simple, but i still can not find it.

MainActivity.kt:

package com.example.zacharymcdaniel.weatherkot

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.example.zacharymcdaniel.weatherkot.domain.Forecast
import org.jetbrains.anko.find

class MainActivity : AppCompatActivity() {
private val url: String = "http://openweathermap.org/"

private val items = listOf(
        "Mon 6/23 - Sunny - 31/17",
        "Tue 6/24 - Foggy - 21/8",
        "Wed 6/25 - Cloudy - 22/17",
        "Thur 6/26 - Rainy - 18/11",
        "Fri 6/27 - Foggy - 21/10",
        "Sat 6/28 - TRAPPED IN WEATHER STATION - 23/18",
        "Sun 6/29 - Sunny - 20/7"
)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val forecastList: RecyclerView = find(R.id.forecast_list)
    forecastList.layoutManager = LinearLayoutManager(this)
    forecastList.adapter = ForecastListAdapter(items)

}

}

data class ForecastResult(val city: City, val list: List<Forecast>)

data class City(val id: Long, val name: String, val coord: Coordinates, val country: String, val population: Int)

data class Coordinates(val ion: Float, val lat: Float)

data class Forecast(val dt: Long, val temp: Tempurature, val pressure: Float,
                val humidity: Int, val weather: List<Weather>,
                val speed: Float, val deg: Int, val clouds: Int,
                val rain: Float) //<----this is supposed to be Forecast in second page

data class Tempurature(val day: Float, val min: Float, val max: Float,
                   val night: Float, val eve: Float, val morn: Float)

data class Weather(val id: Long, val main: String, val desciption: String, val icon: String)

domain.kt in the domain package:

package com.example.zacharymcdaniel.weatherkot.domain

import com.example.zacharymcdaniel.weatherkot.Forecast
import com.example.zacharymcdaniel.weatherkot.ForecastResult
import java.text.DateFormat
import java.util.*
import com.example.zacharymcdaniel.weatherkot.domain.Forecast as 
ModelForecast

public interface Command<T>{
fun execute(): T
}

data class ForecastList(val city: String, val country: String, val dailyForecast: List<ModelForecast>)

data class Forecast(val date: String, val description: String, val high: Int, val low: Int)

public class ForecastdataMapper{

private fun convertFromDataModel(forecast: ForecastResult): ForecastList {
    return ForecastList(forecast.city.name, forecast.city.country, convertForecastListToDomain(forecast.list)) //<---wrong type found here (forecast.list is indicated)
}

private fun convertForecastListToDomain(list: List<Forecast>): List<ModelForecast>{
    return list.map { convertForecastItemToDomain(it) }
}

private fun convertForecastItemToDomain(forecast: Forecast): ModelForecast{
    return ModelForecast(convertDate(forecast.dt),
            forecast.weather[0].desciption,
            forecast.temp.max.toInt(),
            forecast.temp.min.toInt())
}

private fun convertDate(date: Long): String{
    val df = DateFormat.getDateInstance(DateFormat.MEDIUM,
            Locale.getDefault())
    return df.format(date * 1000)
}
}

thanks for the help

like image 956
Mox_z Avatar asked Jul 08 '17 23:07

Mox_z


1 Answers

you should remove the import statement from the first page, which is use the domain.Forecast in the second page rather your own Forecast.

import com.example.zacharymcdaniel.weatherkot.domain.Forecast
// ^--- remove it from your source code

OR using typealias to rename it, for example:

typealias DomainForecast = com.example.zacharymcdaniel.weatherkot.domain.Forecast

OR using alias import statement to rename it, for example:

import com.example.zacharymcdaniel.weatherkot.domain.Forecast as DomainForecast 
like image 62
holi-java Avatar answered Nov 11 '22 17:11

holi-java