Last active
January 3, 2019 17:11
-
-
Save kmizu/247183a52b6408f39ca706f9c6dfaf83 to your computer and use it in GitHub Desktop.
型なし言語のメリットを一言で書いてみる ref: https://qiita.com/kmizu/items/0892eca588088836967f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed abstract class JsonValue | |
case class JsonObject(val members: Map[String, JsonValue]) extends JsonValue | |
case class JsonNumber(val value: Int) extends JsonValue | |
case class JsonString(val value: String) extends JsonValue | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class JsonValue { | |
data class JsonObject(val members: Map<String, JsonValue>) : JsonValue() | |
data class JsonNumber(val value: Int) : JsonValue() | |
data class JsonString(val value: String) : JsonValue() | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Json { | |
public static abstract class JsonValue {} | |
public static class JsonObject extends JsonValue { | |
public final Map<String,JsonValue> members; | |
public JsonObject(Map<String, JsonValue> members) { | |
this.members = members; | |
} | |
} | |
public static class JsonNumber extends JsonValue { | |
public final int value; | |
public JsonNumber(int value) { | |
this.value = value; | |
} | |
} | |
public static class JsonString extends JsonValue { | |
public final String value; | |
public JsonString(String value) { | |
this.value = value; | |
} | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qualified Data.Map as M | |
data JsonValue = JsonObject { members:: M.Map String JsonValue } | |
| JsonNumber Int | |
| ... | |
| JsonString String deriving Show | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ "name" : "Taro", "age": 30 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val person: JsonObject = ... | |
val name: String = person.members("name").asInstanceOf[JsonString].value | |
val age: Int = person.members("age").asInstanceOf[JsonNumber].value | |
println(name) | |
println(age + 1) | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
println(person.name) | |
println(person.age + 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person.age + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person.age + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(+ (get-age person) 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment