C# 使ãããè¦ã¦ãããã¾ãã Java8 ã® default å®è£ ã®ä½¿ãæ¹
Java8 ãã追å ãããã¤ã³ã¿ã¼ãã§ã¤ã¹ã® default å®è£ ã§ãããC# ã®æ¡å¼µã¡ã½ããã«ä¼¼ã¦ã¾ãããã å®éããã®ã©ã¡ãããã·ã³ã¤ã³ã¿ã¼ãã§ã¤ã¹ããå®ç¾©ããã ãã§ããªããã¤ã³ã¿ã¼ãã§ã¤ã¹ããæã«å ¥ãã¾ãã
ããããC# ã®æ¡å¼µã¡ã½ãã㨠Java ã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã® default å®è£ ã«ã¯ãããããã®å©ç¹ã¨æ¬ ç¹ãããã¾ãã
æ¡å¼µã¡ã½ããã®å©ç¹
æ¡å¼µã¡ã½ããã®å©ç¹ã¯ãã¤ã³ã¿ã¼ãã§ã¤ã¹ã®å®è£ è ã ãã§ãªãã ã¤ã³ã¿ã¼ãã§ã¤ã¹ã®ä½¿ç¨è ã«å¯¾ãã¦ãã¤ã³ã¿ã¼ãã§ã¤ã¹ã®æ¡å¼µãéããã¦ããç¹ã§ãã æ¢åã®åã§ãããå¾ä»ãã§ã¡ã½ããã追å ãããã¨ãã§ããã¨ãããã¨ã§ãã
using System; public static class StringExtension { // ã¤ã³ã¿ã¼ãã§ã¤ã¹ã§ãªãã¦ããã©ããªåã«å¯¾ãã¦ãæ¡å¼µå¯è½ public static int ToInt(this string self) { return int.Parse(self); } }
var num = "100".ToInt();
ããã«å¯¾ã㦠Java8 ã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã® default å®è£ ã¯ã ããã¾ã§ã¤ã³ã¿ã¼ãã§ã¤ã¹ã®æä¾è ããæä¾ã§ãã¾ããã
ããã¯ãJava8 ã® default å®è£ ã«å¯¾ãããæ¡å¼µã¡ã½ããã®å¤§ããªã¡ãªããã§ãã
default å®è£ ã®å©ç¹
ã§ã¯ default å®è£ ã«ã¯å©ç¹ããªãããã¨ããã¨ãããã§ã¯ããã¾ããã default å®è£ ã¯ããªããã¤ã³ã¿ã¼ãã§ã¤ã¹ãã·ã³ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«å¤æãã¦ãã¾ããã¨ãã§ããã®ã§ãã
import java.util.*; // Appendableã¯3ã¤ã®ã¡ã½ãããæä¾ããã¤ã³ã¿ã¼ãã§ã¤ã¹ã ãã // SamAppendable(Samã¯Single Abstract Method)ã¯ãã®ãã¡2ã¤ãdefaultå®è£ ã¨ãã¦æä¾ãããã¨ã§ã // 1ã¤ã®æ½è±¡ã¡ã½ãããå®è£ ããã°ããããã«ãã¦ãã interface SamAppendable extends Appendable { Appendable append(CharSequence csq, int start, int end); default Appendable append(CharSequence csq) { this.append(csq, 0, csq == null ? 0 : csq.length()); return this; } default Appendable append(char c) { this.append("" + c, 0, 1); return this; } } public class Main { static void appendHoge(SamAppendable a) { a.append("hoge"); } public static void main(String[] args) { // (å®ã¯ãã¾ãããä¾ã§ã¯ãªãã®ã ããã©) // SAMã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ãããããã§ãã©ã ãå¼ã使ããï¼ appendHoge((csq, start, end) -> { System.out.printf("csq: %s, start: %d, end: %d%n", csq, start, end); return null; }); } }
ããã«å¯¾ãã¦ãæ¡å¼µã¡ã½ããã§ã¯ãã®ãããªãã¨ã¯åºæ¥ã¾ããã
ã¸ã§ããªãã¯ãª IEnumerable ã¯éã¸ã§ããªãã¯ãª IEnumerable ãç¶æ¿ãã¦ããããã GetEnumerator ã 2 種é¡å®è£ ããå¿ è¦ããã£ã¦é¢åãªãã§ããã ããããJava 8 ã® default å®è£ ã ã¨ãã®åé¡è§£æ±ºã§ãããããï¼ã ã»ã»ã»ã¨æã£ãã®ããã®ã¨ã³ããªãæ¸ããã¨ã«ãªã£ããã£ããã§ãã ãªãã»ã©ãæ¡å¼µã¡ã½ããã«ããªãã£ãã®ã«ã¯çç±ãããããã§ããã
ãããªãã¨ãã Scala ã§ãã
ãããªæãã§ãã©ã¡ããä¸é·ä¸çã§ãã£ã¦åªå£ã¤ããããæããªã®ã§ããã»ã»ã» ãªãã¨ãScala ã® trait ã¯ãã®ã©ã¡ãã«ã対å¿ãã¦ãã¾ãã
æ¢åã®åã¸ã®ã¡ã³ãã®è¿½å
object Extensions { implicit class StringExtension(val value: String) { def toInteger: Int = value.toInt } }
2.10 ããããªãç°¡åã«æ¸ããããã«ãªãã¾ããã
ãªããã¤ã³ã¿ã¼ãã§ã¤ã¹ã®ã·ã³ã¤ã³ã¿ã¼ãã§ã¤ã¹å
trait SamAppendable extends Appendable { def append(csq: CharSequence, start: Int, end: Int): Appendable def append(csq: CharSequence): Appendable = { this.append(csq, 0, if (csq == null) 0 else csq.length) this } def append(c: Char): Appendable = { this.append("" + c, 0, 1) this } }
Scala ã§ã¯ Appendable ã SAM ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ããæå³ã¯ãªãã§ããã ãªããã§ãã¡ãããªã¤ã³ã¿ã¼ãã§ã¤ã¹ãã·ã³ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ã§ããã®ã¯ã æ¢åã® Java ã³ã¼ãã¨ã®ããåããèããã¨ãããããããããã¾ããã
Java8
Java8 ã«æå¾ ãããããä»ä½¿ãã Scala 使ããï¼ ã§ããdefault å®è£ ãæ¡å¼µã¡ã½ããã®å£åã³ãã¼ã¨ãè¨ã£ã¡ããã¡ï¼